| 1 |
|
| /* modified version of hiredis's libev adapter */ |
| 2 |
|
| #ifndef __HIREDIS_LIBEV_H__ |
| 3 |
|
| #define __HIREDIS_LIBEV_H__ |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include "EVAPI.h" |
| 10 |
|
| |
| 11 |
|
| #include <poll.h> |
| 12 |
|
| |
| 13 |
|
| #include "hiredis.h" |
| 14 |
|
| #include "async.h" |
| 15 |
|
| #include "net.h" |
| 16 |
|
| #include <errno.h> |
| 17 |
|
| |
| 18 |
|
| /* hiredis.c, not in its public headers */ |
| 19 |
|
| void __redisSetError(redisContext *c, int type, const char *str); |
| 20 |
|
| |
| 21 |
|
| typedef struct redisLibevEvents { |
| 22 |
|
| redisAsyncContext *context; |
| 23 |
|
| struct ev_loop *loop; |
| 24 |
|
| int reading, writing, timing; |
| 25 |
|
| int timing_connected; /* the armed timer belongs to the connected phase */ |
| 26 |
|
| void *armed_for; /* the reply record it was armed for; the adapter itself |
| 27 |
|
| * once a read pushed it for the whole queue */ |
| 28 |
|
| int *gone; /* set by cleanup, for a caller that must not touch e after */ |
| 29 |
|
| int sending; /* the last write left part of the output unsent */ |
| 30 |
|
| int parked_read, parked_write; /* stopped while a callback of it runs */ |
| 31 |
|
| size_t obuf_sent; /* written head of obuf that hiredis has not dropped yet */ |
| 32 |
|
| unsigned int fork_gen; |
| 33 |
|
| HV *unsubs[2]; /* overlapping channel/pattern unsubscribe confirmations */ |
| 34 |
|
| ev_io rev, wev; |
| 35 |
|
| ev_timer timer; |
| 36 |
|
| } redisLibevEvents; |
| 37 |
|
| |
| 38 |
|
| /* bumped in each forked child: an inherited context would take the parent's replies */ |
| 39 |
|
| static unsigned int redisLibevForkGen; |
| 40 |
|
| |
| 41 |
19 |
| static void redisLibevAtforkChild(void) { |
| 42 |
19 |
| redisLibevForkGen++; |
| 43 |
19 |
| } |
| 44 |
|
| |
| 45 |
|
| /* an inherited context fails as a lost connection without reading or writing: |
| 46 |
|
| * hiredis checks c->err before any read. The context may be gone after. */ |
| 47 |
42590 |
| static int redisLibevInherited(redisLibevEvents *e) { |
| 48 |
42590 |
| redisAsyncContext *ac = e->context; |
| 49 |
42590 |
▶ | if (e->fork_gen == redisLibevForkGen) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->fork_gen == redisLibevForkGen) return 0; |
42590 |
0 |
42590 |
|
| 50 |
0 |
▶ | if (!ac->c.err) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ac->c.err) { |
0 |
0 |
0 |
|
| 51 |
0 |
| __redisSetError(&ac->c, REDIS_ERR_IO, |
| 52 |
|
| "connection inherited from the parent process"); |
| 53 |
|
| } |
| 54 |
0 |
| ac->err = ac->c.err; |
| 55 |
0 |
| ac->errstr = ac->c.errstr; |
| 56 |
|
| /* still connecting: redisAsyncHandleRead would call connect() on the |
| 57 |
|
| * shared socket and wait for it; the timeout path fails it untouched */ |
| 58 |
0 |
▶ | if (!(ac->c.flags & REDIS_CONNECTED)) redisAsyncHandleTimeout(ac); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(ac->c.flags & REDIS_CONNECTED)) redisAsyncHandleTimeout(ac); |
0 |
0 |
0 |
|
| 59 |
0 |
| else redisAsyncHandleRead(ac); |
| 60 |
0 |
| return 1; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
|
| /* A nested loop in a callback would trip assert(!REDIS_IN_CALLBACK): the io |
| 64 |
|
| * watchers, level-triggered, are parked until it returns (redisLibevResumeParked) |
| 65 |
|
| * or the loop would spin; the repeating timer just fires again later. */ |
| 66 |
|
| #define REDIS_LIBEV_IN_CALLBACK(e) ((e)->context->c.flags & REDIS_IN_CALLBACK) |
| 67 |
|
| |
| 68 |
|
| static void redisLibevAddRead(void *privdata); |
| 69 |
|
| static void redisLibevDelRead(void *privdata); |
| 70 |
|
| static void redisLibevAddWrite(void *privdata); |
| 71 |
|
| static void redisLibevDelWrite(void *privdata); |
| 72 |
|
| |
| 73 |
|
| #ifdef EV_REDIS_SSL |
| 74 |
|
| #include <openssl/ssl.h> |
| 75 |
|
| #include <openssl/err.h> |
| 76 |
|
| /* OpenSSL judges an I/O result by its per-thread error queue, which hiredis |
| 77 |
|
| * empties only when connecting: a stale error would fail the next read or write */ |
| 78 |
|
| #define REDIS_LIBEV_BEFORE_IO(e) \ |
| 79 |
|
| do { if (NULL != (e)->context->c.privctx) ERR_clear_error(); } while (0) |
| 80 |
|
| |
| 81 |
|
| /* Once a read found no data, hiredis parks unfinished writes until the next |
| 82 |
|
| * read, also when SSL_write only filled the socket: a large command would stall. |
| 83 |
|
| * Its private struct behind privctx (deps/hiredis/ssl.c) starts with the SSL*. */ |
| 84 |
42527 |
| static void redisLibevResumeWrite(redisLibevEvents *e) { |
| 85 |
42527 |
| redisContext *c = &e->context->c; |
| 86 |
42527 |
▶ | if (NULL != c->privctx && !e->writing && sdslen(c->obuf) > 0 |
Branch: 3 branches
| Branch | True | False | Total |
| if (NULL != c->privctx && !e->writing && sdslen(c->obuf) > 0 |
5 |
42522 |
42527 |
| if (NULL != c->privctx && !e->writing && sdslen(c->obuf) > 0 |
5 |
0 |
5 |
| if (NULL != c->privctx && !e->writing && sdslen(c->obuf) > 0 |
4 |
1 |
5 |
|
| 87 |
4 |
▶ | && SSL_READING != SSL_want(*(SSL **)c->privctx)) { |
Branch: 1 branch
| Branch | True | False | Total |
| && SSL_READING != SSL_want(*(SSL **)c->privctx)) { |
0 |
4 |
4 |
|
| 88 |
0 |
| redisLibevAddWrite(e); |
| 89 |
|
| } |
| 90 |
42527 |
| } |
| 91 |
|
| #else |
| 92 |
|
| #define REDIS_LIBEV_BEFORE_IO(e) |
| 93 |
|
| #define redisLibevResumeWrite(e) |
| 94 |
|
| #endif |
| 95 |
|
| |
| 96 |
41 |
| static void redisLibevRead(redisLibevEvents *e) { |
| 97 |
|
| /* incoming data is progress: push the command timeout */ |
| 98 |
41 |
▶ | if (e->timing && e->timing_connected) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->timing && e->timing_connected) { |
10 |
31 |
41 |
| if (e->timing && e->timing_connected) { |
9 |
1 |
10 |
|
| 99 |
9 |
| e->armed_for = (void*)e; |
| 100 |
9 |
| ev_now_update(e->loop); |
| 101 |
9 |
| ev_timer_again(e->loop, &e->timer); |
| 102 |
|
| } |
| 103 |
41 |
▶ | REDIS_LIBEV_BEFORE_IO(e); |
Branch: 1 branch
| Branch | True | False | Total |
| REDIS_LIBEV_BEFORE_IO(e); |
0 |
41 |
41 |
|
| 104 |
41 |
| redisAsyncHandleRead(e->context); |
| 105 |
41 |
| } |
| 106 |
|
| |
| 107 |
39 |
| static void redisLibevReadEvent(EV_P_ ev_io *watcher, int revents) { |
| 108 |
|
| #if EV_MULTIPLICITY |
| 109 |
|
| ((void)loop); |
| 110 |
|
| #endif |
| 111 |
|
| ((void)revents); |
| 112 |
|
| |
| 113 |
39 |
| redisLibevEvents *e = (redisLibevEvents*)watcher->data; |
| 114 |
39 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
39 |
0 |
39 |
| if (e == NULL || e->context == NULL) return; |
0 |
39 |
39 |
|
| 115 |
39 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) { |
0 |
39 |
39 |
|
| 116 |
0 |
| redisLibevDelRead(e); |
| 117 |
0 |
| e->parked_read = 1; |
| 118 |
0 |
| return; |
| 119 |
|
| } |
| 120 |
39 |
▶ | if (redisLibevInherited(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (redisLibevInherited(e)) return; |
0 |
39 |
39 |
|
| 121 |
39 |
| redisLibevRead(e); |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
42532 |
| static void redisLibevWriteEvent(EV_P_ ev_io *watcher, int revents) { |
| 125 |
|
| #if EV_MULTIPLICITY |
| 126 |
|
| ((void)loop); |
| 127 |
|
| #endif |
| 128 |
|
| ((void)revents); |
| 129 |
|
| |
| 130 |
42532 |
| redisLibevEvents *e = (redisLibevEvents*)watcher->data; |
| 131 |
|
| size_t before, after; |
| 132 |
42532 |
| int gone = 0; |
| 133 |
42540 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
42532 |
0 |
42532 |
| if (e == NULL || e->context == NULL) return; |
0 |
42532 |
42532 |
|
| 134 |
42532 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) { |
0 |
42532 |
42532 |
|
| 135 |
0 |
| redisLibevDelWrite(e); |
| 136 |
0 |
| e->parked_write = 1; |
| 137 |
0 |
| return; |
| 138 |
|
| } |
| 139 |
42532 |
▶ | if (redisLibevInherited(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (redisLibevInherited(e)) return; |
0 |
42532 |
42532 |
|
| 140 |
42532 |
| before = sdslen(e->context->c.obuf) - e->obuf_sent; |
| 141 |
42532 |
| e->gone = &gone; |
| 142 |
42532 |
▶ | REDIS_LIBEV_BEFORE_IO(e); |
Branch: 1 branch
| Branch | True | False | Total |
| REDIS_LIBEV_BEFORE_IO(e); |
5 |
42527 |
42532 |
|
| 143 |
42532 |
| redisAsyncHandleWrite(e->context); |
| 144 |
42532 |
▶ | if (gone) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gone) return; |
8 |
42524 |
42532 |
|
| 145 |
42524 |
| e->gone = NULL; |
| 146 |
42524 |
| redisLibevResumeWrite(e); |
| 147 |
|
| /* progress is output draining over several writes, not one write sending it |
| 148 |
|
| * all; TLS keeps the whole buffer until it is out, so there it is a write |
| 149 |
|
| * event with output pending, unless parked until a read (watcher stopped) */ |
| 150 |
42524 |
| after = sdslen(e->context->c.obuf) - e->obuf_sent; |
| 151 |
42524 |
▶ | if (e->timing && e->timing_connected |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->timing && e->timing_connected |
42494 |
30 |
42524 |
| if (e->timing && e->timing_connected |
76 |
42418 |
42494 |
|
| 152 |
92 |
▶ | && (after < before ? (after > 0 || e->sending) |
Branch: 4 branches
| Branch | True | False | Total |
| && (after < before ? (after > 0 || e->sending) |
60 |
16 |
76 |
| && (after < before ? (after > 0 || e->sending) |
30 |
30 |
60 |
| && (after < before ? (after > 0 || e->sending) |
1 |
29 |
30 |
| && (after < before ? (after > 0 || e->sending) |
31 |
45 |
76 |
|
| 153 |
16 |
▶ | : (after > 0 && e->writing && NULL != e->context->c.privctx))) { |
Branch: 3 branches
| Branch | True | False | Total |
| : (after > 0 && e->writing && NULL != e->context->c.privctx))) { |
4 |
12 |
16 |
| : (after > 0 && e->writing && NULL != e->context->c.privctx))) { |
0 |
4 |
4 |
| : (after > 0 && e->writing && NULL != e->context->c.privctx))) { |
0 |
0 |
0 |
|
| 154 |
31 |
| e->armed_for = (void*)e; |
| 155 |
31 |
| ev_now_update(e->loop); |
| 156 |
31 |
| ev_timer_again(e->loop, &e->timer); |
| 157 |
|
| } |
| 158 |
42524 |
| e->sending = after > 0; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
134 |
| static void redisLibevAddRead(void *privdata) { |
| 162 |
134 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 163 |
|
| struct ev_loop *loop; |
| 164 |
134 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
134 |
134 |
|
| 165 |
134 |
| loop = e->loop; |
| 166 |
134 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
134 |
134 |
|
| 167 |
134 |
▶ | if (!e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->reading) { |
49 |
85 |
134 |
|
| 168 |
49 |
| e->reading = 1; |
| 169 |
49 |
| ev_io_start(loop, &e->rev); |
| 170 |
|
| } |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
0 |
| static void redisLibevDelRead(void *privdata) { |
| 174 |
0 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 175 |
|
| struct ev_loop *loop; |
| 176 |
0 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
0 |
0 |
|
| 177 |
0 |
| loop = e->loop; |
| 178 |
0 |
▶ | if (e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->reading) { |
0 |
0 |
0 |
|
| 179 |
0 |
| e->reading = 0; |
| 180 |
0 |
▶ | if (loop != NULL) ev_io_stop(loop, &e->rev); |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) ev_io_stop(loop, &e->rev); |
0 |
0 |
0 |
|
| 181 |
|
| } |
| 182 |
|
| } |
| 183 |
|
| |
| 184 |
161 |
| static void redisLibevAddWrite(void *privdata) { |
| 185 |
161 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 186 |
|
| struct ev_loop *loop; |
| 187 |
161 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
161 |
161 |
|
| 188 |
161 |
| loop = e->loop; |
| 189 |
161 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
161 |
161 |
|
| 190 |
161 |
▶ | if (!e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->writing) { |
89 |
72 |
161 |
|
| 191 |
89 |
| e->writing = 1; |
| 192 |
89 |
| ev_io_start(loop, &e->wev); |
| 193 |
|
| } |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
79 |
| static void redisLibevDelWrite(void *privdata) { |
| 197 |
79 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 198 |
|
| struct ev_loop *loop; |
| 199 |
79 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
79 |
79 |
|
| 200 |
79 |
| loop = e->loop; |
| 201 |
79 |
▶ | if (e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->writing) { |
79 |
0 |
79 |
|
| 202 |
79 |
| e->writing = 0; |
| 203 |
79 |
▶ | if (loop != NULL) ev_io_stop(loop, &e->wev); |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) ev_io_stop(loop, &e->wev); |
79 |
0 |
79 |
|
| 204 |
|
| } |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| /* Call once a hiredis callback of ac has returned: hiredis runs them all |
| 208 |
|
| * before its cleanup, so ev.data is still this adapter or NULL. */ |
| 209 |
137 |
| static void redisLibevResumeParked(redisAsyncContext *ac) { |
| 210 |
137 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 211 |
137 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
31 |
106 |
137 |
|
| 212 |
106 |
▶ | if (e->parked_read) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->parked_read) { |
0 |
106 |
106 |
|
| 213 |
0 |
| e->parked_read = 0; |
| 214 |
0 |
| redisLibevAddRead(e); |
| 215 |
|
| } |
| 216 |
106 |
▶ | if (e->parked_write) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->parked_write) { |
0 |
106 |
106 |
|
| 217 |
0 |
| e->parked_write = 0; |
| 218 |
0 |
| redisLibevAddWrite(e); |
| 219 |
|
| } |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
59 |
| static void redisLibevCleanup(void *privdata) { |
| 223 |
59 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 224 |
|
| redisAsyncContext *ctx; |
| 225 |
|
| struct ev_loop *loop; |
| 226 |
59 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
59 |
59 |
|
| 227 |
|
| |
| 228 |
|
| /* hiredis keeps the other hooks installed: they must find no adapter */ |
| 229 |
59 |
| ctx = e->context; |
| 230 |
59 |
▶ | if (ctx != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx != NULL) { |
59 |
0 |
59 |
|
| 231 |
59 |
| ctx->ev.data = NULL; |
| 232 |
|
| } |
| 233 |
|
| |
| 234 |
59 |
| loop = e->loop; |
| 235 |
59 |
| e->loop = NULL; |
| 236 |
59 |
| e->context = NULL; |
| 237 |
|
| |
| 238 |
59 |
| e->rev.data = NULL; |
| 239 |
59 |
| e->wev.data = NULL; |
| 240 |
59 |
| e->timer.data = NULL; |
| 241 |
59 |
▶ | if (e->gone != NULL) *e->gone = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->gone != NULL) *e->gone = 1; |
8 |
51 |
59 |
|
| 242 |
|
| |
| 243 |
59 |
▶ | if (loop != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop != NULL) { |
59 |
0 |
59 |
|
| 244 |
59 |
| ev_io_stop(loop, &e->rev); |
| 245 |
59 |
| ev_io_stop(loop, &e->wev); |
| 246 |
59 |
| ev_timer_stop(loop, &e->timer); |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
|
| /* Global destruction frees Perl's arenas, possibly before this context. */ |
| 250 |
59 |
▶ | if (!PL_dirty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) { |
59 |
0 |
59 |
|
| 251 |
59 |
▶ | if (e->unsubs[0] != NULL) SvREFCNT_dec((SV*)e->unsubs[0]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->unsubs[0] != NULL) SvREFCNT_dec((SV*)e->unsubs[0]); |
0 |
59 |
59 |
|
| 252 |
59 |
▶ | if (e->unsubs[1] != NULL) SvREFCNT_dec((SV*)e->unsubs[1]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->unsubs[1] != NULL) SvREFCNT_dec((SV*)e->unsubs[1]); |
0 |
59 |
59 |
|
| 253 |
|
| } |
| 254 |
59 |
| Safefree(e); |
| 255 |
|
| } |
| 256 |
|
| |
| 257 |
19 |
| static void redisLibevTimeout(EV_P_ ev_timer *timer, int revents) { |
| 258 |
|
| #if EV_MULTIPLICITY |
| 259 |
|
| ((void)loop); |
| 260 |
|
| #endif |
| 261 |
|
| ((void)revents); |
| 262 |
|
| |
| 263 |
19 |
| redisLibevEvents *e = (redisLibevEvents*)timer->data; |
| 264 |
|
| redisContext *c; |
| 265 |
|
| struct pollfd pfd; |
| 266 |
24 |
▶ | if (e == NULL || e->context == NULL) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == NULL || e->context == NULL) return; |
19 |
0 |
19 |
| if (e == NULL || e->context == NULL) return; |
0 |
19 |
19 |
|
| 267 |
19 |
▶ | if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (REDIS_LIBEV_IN_CALLBACK(e)) return; |
0 |
19 |
19 |
|
| 268 |
19 |
▶ | if (redisLibevInherited(e)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (redisLibevInherited(e)) return; |
0 |
19 |
19 |
|
| 269 |
|
| |
| 270 |
|
| /* libev runs an expired timer ahead of I/O that became ready in the same |
| 271 |
|
| * iteration: after the loop was blocked, the answer may be waiting */ |
| 272 |
19 |
| c = &e->context->c; |
| 273 |
19 |
▶ | if (c->fd != REDIS_INVALID_FD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->fd != REDIS_INVALID_FD) { |
19 |
0 |
19 |
|
| 274 |
19 |
| pfd.fd = c->fd; |
| 275 |
19 |
▶ | pfd.events = (c->flags & REDIS_CONNECTED) ? POLLIN : POLLOUT; |
Branch: 1 branch
| Branch | True | False | Total |
| pfd.events = (c->flags & REDIS_CONNECTED) ? POLLIN : POLLOUT; |
15 |
4 |
19 |
|
| 276 |
19 |
| pfd.revents = 0; |
| 277 |
19 |
▶ | if (poll(&pfd, 1, 0) > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (poll(&pfd, 1, 0) > 0) { |
5 |
14 |
19 |
|
| 278 |
5 |
| int gone = 0; |
| 279 |
5 |
▶ | if (c->flags & REDIS_CONNECTED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->flags & REDIS_CONNECTED) { |
2 |
3 |
5 |
|
| 280 |
2 |
| redisLibevRead(e); |
| 281 |
4 |
| return; |
| 282 |
|
| } |
| 283 |
|
| /* let hiredis finish the connect; one still in progress (a unix |
| 284 |
|
| * socket refused by a full backlog stays writable) times out */ |
| 285 |
3 |
| e->gone = &gone; |
| 286 |
3 |
▶ | REDIS_LIBEV_BEFORE_IO(e); |
Branch: 1 branch
| Branch | True | False | Total |
| REDIS_LIBEV_BEFORE_IO(e); |
0 |
3 |
3 |
|
| 287 |
3 |
| redisAsyncHandleWrite(e->context); |
| 288 |
3 |
▶ | if (gone) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (gone) return; |
0 |
3 |
3 |
|
| 289 |
3 |
| e->gone = NULL; |
| 290 |
3 |
| redisLibevResumeWrite(e); |
| 291 |
3 |
▶ | if (c->flags & REDIS_CONNECTED) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->flags & REDIS_CONNECTED) return; |
2 |
1 |
3 |
|
| 292 |
|
| } |
| 293 |
|
| } |
| 294 |
|
| /* connected with nothing to time: stop repeating, the next command re-arms */ |
| 295 |
15 |
▶ | if ((c->flags & REDIS_CONNECTED) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((c->flags & REDIS_CONNECTED) |
13 |
2 |
15 |
|
| 296 |
13 |
▶ | && ((NULL == e->context->replies.head && NULL == e->context->sub.replies.head) |
Branch: 2 branches
| Branch | True | False | Total |
| && ((NULL == e->context->replies.head && NULL == e->context->sub.replies.head) |
2 |
11 |
13 |
| && ((NULL == e->context->replies.head && NULL == e->context->sub.replies.head) |
1 |
1 |
2 |
|
| 297 |
12 |
▶ | || NULL == c->command_timeout |
Branch: 1 branch
| Branch | True | False | Total |
| || NULL == c->command_timeout |
12 |
0 |
12 |
|
| 298 |
12 |
▶ | || (0 == c->command_timeout->tv_sec && 0 == c->command_timeout->tv_usec))) { |
Branch: 2 branches
| Branch | True | False | Total |
| || (0 == c->command_timeout->tv_sec && 0 == c->command_timeout->tv_usec))) { |
10 |
2 |
12 |
| || (0 == c->command_timeout->tv_sec && 0 == c->command_timeout->tv_usec))) { |
0 |
10 |
10 |
|
| 299 |
1 |
| e->timing = 0; |
| 300 |
1 |
| ev_timer_stop(e->loop, &e->timer); |
| 301 |
1 |
| return; |
| 302 |
|
| } |
| 303 |
14 |
| redisAsyncHandleTimeout(e->context); |
| 304 |
|
| } |
| 305 |
|
| |
| 306 |
|
| /* both lists: once subscribed, hiredis queues command replies in sub.replies */ |
| 307 |
135 |
| static int redisLibevSeveralOutstanding(redisAsyncContext *ac) { |
| 308 |
135 |
| return ac->replies.head != ac->replies.tail |
| 309 |
111 |
▶ | || ac->sub.replies.head != ac->sub.replies.tail |
Branch: 1 branch
| Branch | True | False | Total |
| || ac->sub.replies.head != ac->sub.replies.tail |
99 |
12 |
111 |
|
| 310 |
246 |
▶ | || (ac->replies.head && ac->sub.replies.head); |
Branch: 3 branches
| Branch | True | False | Total |
| || (ac->replies.head && ac->sub.replies.head); |
111 |
24 |
135 |
| || (ac->replies.head && ac->sub.replies.head); |
96 |
3 |
99 |
| || (ac->replies.head && ac->sub.replies.head); |
0 |
96 |
96 |
|
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| /* (P)(UN)SUBSCRIBE add no record, so they cannot pass for a new reply */ |
| 314 |
210 |
| static void *redisLibevFirstOutstanding(redisAsyncContext *ac) { |
| 315 |
210 |
▶ | return ac->replies.head ? (void*)ac->replies.head : (void*)ac->sub.replies.head; |
Branch: 1 branch
| Branch | True | False | Total |
| return ac->replies.head ? (void*)ac->replies.head : (void*)ac->sub.replies.head; |
148 |
62 |
210 |
|
| 316 |
|
| } |
| 317 |
|
| |
| 318 |
|
| /* before a command with its own reply record: an idle connection gets a fresh deadline */ |
| 319 |
57 |
| static void redisLibevExpectNewReply(redisAsyncContext *ac) { |
| 320 |
57 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 321 |
57 |
▶ | if (e != NULL && redisLibevFirstOutstanding(ac) == NULL) e->armed_for = NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e != NULL && redisLibevFirstOutstanding(ac) == NULL) e->armed_for = NULL; |
57 |
0 |
57 |
| if (e != NULL && redisLibevFirstOutstanding(ac) == NULL) e->armed_for = NULL; |
35 |
22 |
57 |
|
| 322 |
57 |
| } |
| 323 |
|
| |
| 324 |
|
| /* hiredis calls this on every command, read and write. Connecting, keep the |
| 325 |
|
| * connect's deadline; connected, re-arm only for a new first outstanding reply: |
| 326 |
|
| * incoming data pushes it (redisLibevRead), new commands must not. */ |
| 327 |
162 |
| static void redisLibevSetTimeout(void *privdata, struct timeval tv) { |
| 328 |
162 |
| redisLibevEvents *e = (redisLibevEvents*)privdata; |
| 329 |
|
| struct ev_loop *loop; |
| 330 |
|
| void *first; |
| 331 |
|
| int connected; |
| 332 |
162 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
162 |
162 |
|
| 333 |
162 |
| loop = e->loop; |
| 334 |
162 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
162 |
162 |
|
| 335 |
|
| |
| 336 |
162 |
| connected = (e->context->c.flags & REDIS_CONNECTED) ? 1 : 0; |
| 337 |
162 |
▶ | first = connected ? redisLibevFirstOutstanding(e->context) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| first = connected ? redisLibevFirstOutstanding(e->context) : NULL; |
152 |
10 |
162 |
|
| 338 |
162 |
▶ | if (e->timing && e->timing_connected == connected |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->timing && e->timing_connected == connected |
140 |
22 |
162 |
| if (e->timing && e->timing_connected == connected |
140 |
0 |
140 |
|
| 339 |
140 |
▶ | && (!connected || redisLibevSeveralOutstanding(e->context) |
Branch: 2 branches
| Branch | True | False | Total |
| && (!connected || redisLibevSeveralOutstanding(e->context) |
135 |
5 |
140 |
| && (!connected || redisLibevSeveralOutstanding(e->context) |
99 |
36 |
135 |
|
| 340 |
99 |
▶ | || first == e->armed_for || e->armed_for == (void*)e)) { |
Branch: 2 branches
| Branch | True | False | Total |
| || first == e->armed_for || e->armed_for == (void*)e)) { |
73 |
26 |
99 |
| || first == e->armed_for || e->armed_for == (void*)e)) { |
67 |
6 |
73 |
|
| 341 |
134 |
| return; |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
28 |
| e->timing = 1; |
| 345 |
28 |
| e->timing_connected = connected; |
| 346 |
28 |
| e->armed_for = first; |
| 347 |
28 |
| e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00; |
| 348 |
|
| /* a connect outside ev_run or a long callback leaves ev_now stale */ |
| 349 |
28 |
| ev_now_update(loop); |
| 350 |
28 |
| ev_timer_again(loop, &e->timer); |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| /* hiredis moves the unsent rest of obuf to its head after each partial write, |
| 354 |
|
| * quadratic in a large buffer: report a write only once it covers what is left */ |
| 355 |
89 |
| static ssize_t redisLibevNetWrite(redisContext *c) { |
| 356 |
89 |
| redisLibevEvents *e = (redisLibevEvents*)((redisAsyncContext*)c)->ev.data; |
| 357 |
89 |
| size_t len = sdslen(c->obuf); |
| 358 |
89 |
▶ | size_t off = (e != NULL && e->obuf_sent < len) ? e->obuf_sent : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| size_t off = (e != NULL && e->obuf_sent < len) ? e->obuf_sent : 0; |
89 |
0 |
89 |
| size_t off = (e != NULL && e->obuf_sent < len) ? e->obuf_sent : 0; |
89 |
0 |
89 |
|
| 359 |
89 |
| ssize_t n = send(c->fd, c->obuf + off, len - off, 0); |
| 360 |
|
| |
| 361 |
89 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
89 |
89 |
|
| 362 |
0 |
▶ | if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) return 0; |
0 |
0 |
0 |
| if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) return 0; |
0 |
0 |
0 |
| if (errno == EWOULDBLOCK || errno == EAGAIN || errno == EINTR) return 0; |
0 |
0 |
0 |
|
| 363 |
0 |
▶ | if (e != NULL) e->obuf_sent = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != NULL) e->obuf_sent = 0; |
0 |
0 |
0 |
|
| 364 |
0 |
| __redisSetError(c, REDIS_ERR_IO, strerror(errno)); |
| 365 |
0 |
| return -1; |
| 366 |
|
| } |
| 367 |
89 |
| off += (size_t)n; |
| 368 |
89 |
▶ | if (e == NULL || off == len || off >= len - off) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (e == NULL || off == len || off >= len - off) { |
89 |
0 |
89 |
| if (e == NULL || off == len || off >= len - off) { |
30 |
59 |
89 |
| if (e == NULL || off == len || off >= len - off) { |
5 |
25 |
30 |
|
| 369 |
64 |
▶ | if (e != NULL) e->obuf_sent = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e != NULL) e->obuf_sent = 0; |
64 |
0 |
64 |
|
| 370 |
64 |
| return (ssize_t)off; |
| 371 |
|
| } |
| 372 |
25 |
| e->obuf_sent = off; |
| 373 |
25 |
| return 0; |
| 374 |
|
| } |
| 375 |
|
| |
| 376 |
|
| static redisContextFuncs redisLibevNetFuncs; |
| 377 |
|
| |
| 378 |
|
| /* plain TCP and unix only: TLS keeps the whole buffer until it is all out */ |
| 379 |
59 |
| static void redisLibevUseNetWrite(redisContext *c) { |
| 380 |
59 |
▶ | if (c->funcs->write != redisNetWrite) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->funcs->write != redisNetWrite) return; |
1 |
58 |
59 |
|
| 381 |
58 |
▶ | if (redisLibevNetFuncs.write == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (redisLibevNetFuncs.write == NULL) { |
5 |
53 |
58 |
|
| 382 |
5 |
| redisLibevNetFuncs = *c->funcs; |
| 383 |
5 |
| redisLibevNetFuncs.write = redisLibevNetWrite; |
| 384 |
|
| } |
| 385 |
58 |
| c->funcs = &redisLibevNetFuncs; |
| 386 |
|
| } |
| 387 |
|
| |
| 388 |
59 |
| static int redisLibevAttach(EV_P_ redisAsyncContext *ac) { |
| 389 |
59 |
| redisContext *c = &(ac->c); |
| 390 |
|
| redisLibevEvents *e; |
| 391 |
|
| |
| 392 |
59 |
▶ | if (ac->ev.data != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ac->ev.data != NULL) |
0 |
59 |
59 |
|
| 393 |
0 |
| return REDIS_ERR; |
| 394 |
|
| |
| 395 |
59 |
| Newx(e, 1, redisLibevEvents); |
| 396 |
59 |
| e->context = ac; |
| 397 |
|
| #if EV_MULTIPLICITY |
| 398 |
59 |
| e->loop = loop; |
| 399 |
|
| #else |
| 400 |
|
| #error "EV_MULTIPLICITY is required for EV::Redis libev adapter" |
| 401 |
|
| #endif |
| 402 |
59 |
| e->reading = e->writing = e->timing = e->timing_connected = 0; |
| 403 |
59 |
| e->armed_for = NULL; |
| 404 |
59 |
| e->gone = NULL; |
| 405 |
59 |
| e->sending = 0; |
| 406 |
59 |
| e->parked_read = e->parked_write = 0; |
| 407 |
59 |
| e->obuf_sent = 0; |
| 408 |
59 |
| e->fork_gen = redisLibevForkGen; |
| 409 |
59 |
| e->unsubs[0] = e->unsubs[1] = NULL; |
| 410 |
59 |
| e->rev.data = (void*)e; |
| 411 |
59 |
| e->wev.data = (void*)e; |
| 412 |
|
| |
| 413 |
59 |
| ac->ev.addRead = redisLibevAddRead; |
| 414 |
59 |
| ac->ev.delRead = redisLibevDelRead; |
| 415 |
59 |
| ac->ev.addWrite = redisLibevAddWrite; |
| 416 |
59 |
| ac->ev.delWrite = redisLibevDelWrite; |
| 417 |
59 |
| ac->ev.cleanup = redisLibevCleanup; |
| 418 |
59 |
| ac->ev.scheduleTimer = redisLibevSetTimeout; |
| 419 |
59 |
| ac->ev.data = e; |
| 420 |
|
| |
| 421 |
59 |
| ev_io_init(&e->rev, redisLibevReadEvent, c->fd, EV_READ); |
| 422 |
59 |
| ev_io_init(&e->wev, redisLibevWriteEvent, c->fd, EV_WRITE); |
| 423 |
|
| |
| 424 |
|
| /* init now: redisLibevSetPriority may run before the first schedule */ |
| 425 |
59 |
| ev_init(&e->timer, redisLibevTimeout); |
| 426 |
59 |
| e->timer.data = (void*)e; |
| 427 |
|
| |
| 428 |
59 |
| return REDIS_OK; |
| 429 |
|
| } |
| 430 |
|
| |
| 431 |
|
| /* redisAsyncSetTimeout leaves an armed timer on its old deadline until the next I/O */ |
| 432 |
1 |
| static void redisLibevRefreshTimeout(redisAsyncContext *ac, struct timeval tv) { |
| 433 |
1 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 434 |
|
| struct ev_loop *loop; |
| 435 |
1 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
1 |
1 |
|
| 436 |
1 |
| loop = e->loop; |
| 437 |
1 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
1 |
1 |
|
| 438 |
|
| |
| 439 |
1 |
▶ | if (tv.tv_sec || tv.tv_usec) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tv.tv_sec || tv.tv_usec) { |
1 |
0 |
1 |
| if (tv.tv_sec || tv.tv_usec) { |
1 |
0 |
1 |
|
| 440 |
1 |
| e->timing = 1; |
| 441 |
1 |
| e->timing_connected = (ac->c.flags & REDIS_CONNECTED) ? 1 : 0; |
| 442 |
1 |
| e->armed_for = redisLibevFirstOutstanding(ac); |
| 443 |
1 |
| e->timer.repeat = tv.tv_sec + tv.tv_usec / 1000000.00; |
| 444 |
1 |
| ev_now_update(loop); |
| 445 |
1 |
| ev_timer_again(loop, &e->timer); |
| 446 |
|
| } |
| 447 |
|
| else { |
| 448 |
0 |
| e->timing = 0; |
| 449 |
0 |
| e->timer.repeat = 0.; |
| 450 |
0 |
| ev_timer_stop(loop, &e->timer); |
| 451 |
|
| } |
| 452 |
|
| } |
| 453 |
|
| |
| 454 |
2 |
| static void redisLibevSetPriority(redisAsyncContext *ac, int priority) { |
| 455 |
2 |
| redisLibevEvents *e = (redisLibevEvents*)ac->ev.data; |
| 456 |
|
| struct ev_loop *loop; |
| 457 |
2 |
▶ | if (e == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == NULL) return; |
0 |
2 |
2 |
|
| 458 |
|
| |
| 459 |
2 |
| loop = e->loop; |
| 460 |
2 |
▶ | if (loop == NULL) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loop == NULL) return; |
0 |
2 |
2 |
|
| 461 |
|
| |
| 462 |
|
| /* ev_set_priority is only valid on an inactive watcher */ |
| 463 |
2 |
▶ | if (e->reading) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->reading) { |
1 |
1 |
2 |
|
| 464 |
1 |
| ev_io_stop(loop, &e->rev); |
| 465 |
1 |
| ev_set_priority(&e->rev, priority); |
| 466 |
1 |
| ev_io_start(loop, &e->rev); |
| 467 |
|
| } else { |
| 468 |
1 |
| ev_set_priority(&e->rev, priority); |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
2 |
▶ | if (e->writing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->writing) { |
0 |
2 |
2 |
|
| 472 |
0 |
| ev_io_stop(loop, &e->wev); |
| 473 |
0 |
| ev_set_priority(&e->wev, priority); |
| 474 |
0 |
| ev_io_start(loop, &e->wev); |
| 475 |
|
| } else { |
| 476 |
2 |
| ev_set_priority(&e->wev, priority); |
| 477 |
|
| } |
| 478 |
|
| |
| 479 |
2 |
▶ | if (e->timing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->timing) { |
1 |
1 |
2 |
|
| 480 |
|
| /* keep the deadline: a priority change is not progress; an expired |
| 481 |
|
| * timer is already pending and re-armed, so fire it right away */ |
| 482 |
1 |
▶ | ev_tstamp left = ev_is_pending(&e->timer) ? 0. : ev_timer_remaining(loop, &e->timer); |
Branch: 1 branch
| Branch | True | False | Total |
| ev_tstamp left = ev_is_pending(&e->timer) ? 0. : ev_timer_remaining(loop, &e->timer); |
0 |
1 |
1 |
|
| 483 |
1 |
| ev_timer_stop(loop, &e->timer); |
| 484 |
1 |
| ev_set_priority(&e->timer, priority); |
| 485 |
1 |
| ev_timer_set(&e->timer, left, e->timer.repeat); |
| 486 |
1 |
| ev_timer_start(loop, &e->timer); |
| 487 |
|
| } else { |
| 488 |
1 |
| ev_set_priority(&e->timer, priority); |
| 489 |
|
| } |
| 490 |
|
| } |
| 491 |
|
| |
| 492 |
|
| #endif |