Created
October 23, 2015 22:54
-
-
Save michael-grunder/828de9e93cdbdf3525bd to your computer and use it in GitHub Desktop.
command timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <hiredis/hiredis.h> | |
| #include <stdio.h> | |
| #include <time.h> | |
| static void timeoutToTimeval(struct timeval *tv, double timeout) { | |
| tv->tv_sec = (time_t)timeout; | |
| tv->tv_usec = (int)((timeout - tv->tv_sec) * 1000000); | |
| } | |
| int main(int argc, const char **argv) { | |
| struct timeval tv; | |
| double timeout; | |
| time_t tm1, tm2; | |
| redisContext *c; | |
| redisReply *r; | |
| /* Connect with a 100s timeout */ | |
| timeoutToTimeval(&tv, 20); | |
| c = redisConnectUnixWithTimeout("/tmp/redis.sock", tv); | |
| if (!c || c->err) { | |
| fprintf(stderr, "Error: Can't connect!\n"); | |
| return -1; | |
| } | |
| /* Set a 2 second communication timeout */ | |
| if (argc > 1) { | |
| timeoutToTimeval(&tv, 2.0); | |
| redisSetTimeout(c, tv); | |
| } | |
| tm1 = time(NULL); | |
| r = redisCommand(c, "DEBUG SLEEP 5"); | |
| tm2 = time(NULL); | |
| if (!r || c->err) { | |
| printf("Errored out after ~%lds\n", tm2-tm1); | |
| } else { | |
| printf("No error after ~%lds\n", tm2-tm1); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment