lib/Redis/Fast.xs

filter: Statementstmt 66.7% 532 / 797 Branchbran 45.5% 317 / 696 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 56.8% 849 / 1493 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Redis/Fast/List.pm ↑ Summary
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7 #include "hiredis.h"
8 #include "hiredis_ssl.h"
9 #include "async.h"
10  
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <stdio.h>
15 #include <poll.h>
16 #include <sys/socket.h>
17  
18 #define MAX_ERROR_SIZE 256
19  
20 #define WAIT_FOR_EVENT_OK 0
21 #define WAIT_FOR_EVENT_READ_TIMEOUT 1
22 #define WAIT_FOR_EVENT_WRITE_TIMEOUT 2
23 #define WAIT_FOR_EVENT_EXCEPTION 3
24  
25 #define FLAG_INSIDE_TRANSACTION 0x01
26 #define FLAG_INSIDE_WATCH 0x02
27  
28 #define DEBUG_MSG(fmt, ...) \
29 if (self->debug) { \
30 fprintf(stderr, "[%d][%d][%s:%d:%s]: ", getpid(), getppid(), __FILE__, __LINE__, __func__); \
31 fprintf(stderr, fmt, __VA_ARGS__); \
32 fprintf(stderr, "\n"); \
33 }
34  
35 #define EQUALS_COMMAND(len, cmd, expected) ((len) == sizeof(expected) - 1 && memcmp(cmd, expected, sizeof(expected) - 1) == 0)
36  
37 typedef struct redis_fast_s {
38 redisAsyncContext* ac;
39 char* hostname;
40 int port;
41 char* path;
42 char* error;
43 double reconnect;
44 int every;
45 int debug;
46 double cnx_timeout;
47 double read_timeout;
48 double write_timeout;
49 int current_database;
50 int need_reconnect;
51 int is_connected;
52 SV* on_connect;
53 SV* on_build_sock;
54 SV* data;
55 SV* reconnect_on_error;
56 SV* subscription_cb;
57 double next_reconnect_on_error_at;
58 int proccess_sub_count;
59 int is_subscriber;
60 int expected_subs;
61 pid_t pid;
62 int ssl;
63 int ssl_verify_mode;
64 int flags;
65 } redis_fast_t, *Redis__Fast;
66  
67 typedef struct redis_fast_reply_s {
68 SV* result;
69 SV* error;
70 } redis_fast_reply_t;
71  
72 typedef redis_fast_reply_t (*CUSTOM_DECODE)(pTHX_ Redis__Fast self, redisReply* reply, int collect_errors);
73  
74 typedef struct redis_fast_sync_cb_s {
75 redis_fast_reply_t ret;
76 int collect_errors;
77 CUSTOM_DECODE custom_decode;
78 int on_flags;
79 int off_flags;
80 } redis_fast_sync_cb_t;
81  
82 typedef struct redis_fast_async_cb_s {
83 SV* cb;
84 int collect_errors;
85 CUSTOM_DECODE custom_decode;
86 int on_flags;
87 int off_flags;
88 const void* command_name;
89 STRLEN command_length;
90 } redis_fast_async_cb_t;
91  
92  
93 #define WAIT_FOR_READ 0x01
94 #define WAIT_FOR_WRITE 0x02
95 typedef struct redis_fast_event_s {
96 int flags;
97 Redis__Fast self;
98 } redis_fast_event_t;
99  
100  
101 248 static void AddRead(void *privdata) {
102 248 redis_fast_event_t *e = (redis_fast_event_t*)privdata;
103 248 Redis__Fast self = e->self;
104 248 e->flags |= WAIT_FOR_READ;
105 248 ▶ DEBUG_MSG("flags = %x", e->flags);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("flags = %x", e->flags); 0 248 248
106 248 }
107  
108 0 static void DelRead(void *privdata) {
109 0 redis_fast_event_t *e = (redis_fast_event_t*)privdata;
110 0 Redis__Fast self = e->self;
111 0 e->flags &= ~WAIT_FOR_READ;
112 0 ▶ DEBUG_MSG("flags = %x", e->flags);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("flags = %x", e->flags); 0 0 0
113 0 }
114  
115 164 static void AddWrite(void *privdata) {
116 164 redis_fast_event_t *e = (redis_fast_event_t*)privdata;
117 164 Redis__Fast self = e->self;
118 164 e->flags |= WAIT_FOR_WRITE;
119 164 ▶ DEBUG_MSG("flags = %x", e->flags);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("flags = %x", e->flags); 0 164 164
120 164 }
121  
122 164 static void DelWrite(void *privdata) {
123 164 redis_fast_event_t *e = (redis_fast_event_t*)privdata;
124 164 Redis__Fast self = e->self;
125 164 e->flags &= ~WAIT_FOR_WRITE;
126 164 ▶ DEBUG_MSG("flags = %x", e->flags);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("flags = %x", e->flags); 0 164 164
127 164 }
128  
129 80 static void Cleanup(void *privdata) {
130 80 free(privdata);
131 80 }
132  
133 80 static int Attach(redisAsyncContext *ac) {
134 80 Redis__Fast self = (Redis__Fast)ac->data;
135 redis_fast_event_t *e;
136  
137 /* Nothing should be attached when something is already attached */
138 80 ▶ if (ac->ev.data != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ac->ev.data != NULL) 0 80 80
139 0 return REDIS_ERR;
140  
141 /* Create container for context and r/w events */
142 80 e = (redis_fast_event_t*)malloc(sizeof(*e));
143 80 e->flags = 0;
144 80 e->self = self;
145  
146 /* Register functions to start/stop listening for events */
147 80 ac->ev.addRead = AddRead;
148 80 ac->ev.delRead = DelRead;
149 80 ac->ev.addWrite = AddWrite;
150 80 ac->ev.delWrite = DelWrite;
151 80 ac->ev.cleanup = Cleanup;
152 80 ac->ev.data = e;
153  
154 80 return REDIS_OK;
155 }
156  
157 75314 static int wait_for_event(pTHX_ Redis__Fast self, double read_timeout, double write_timeout) {
158 redisContext *c;
159 int fd;
160 redis_fast_event_t *e;
161 struct pollfd pollfd;
162 int rc;
163 75314 double timeout = -1;
164 75314 int timeout_mode = WAIT_FOR_EVENT_WRITE_TIMEOUT;
165 int ms;
166  
167 75314 ▶ if(self==NULL) return WAIT_FOR_EVENT_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
if(self==NULL) return WAIT_FOR_EVENT_EXCEPTION; 0 75314 75314
168 75314 ▶ if(self->ac==NULL) return WAIT_FOR_EVENT_EXCEPTION;
Branch: 1 branch
BranchTrueFalseTotal
if(self->ac==NULL) return WAIT_FOR_EVENT_EXCEPTION; 0 75314 75314
169  
170 75314 c = &(self->ac->c);
171 75314 fd = c->fd;
172 75314 e = (redis_fast_event_t*)self->ac->ev.data;
173 75314 ▶ if(e==NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if(e==NULL) return 0; 0 75314 75314
174  
175 75314 ▶ if((e->flags & (WAIT_FOR_READ|WAIT_FOR_WRITE)) == (WAIT_FOR_READ|WAIT_FOR_WRITE)) {
Branch: 1 branch
BranchTrueFalseTotal
if((e->flags & (WAIT_FOR_READ|WAIT_FOR_WRITE)) == (WAIT_FOR_READ|WAIT_FOR_WRITE)) { 84 75230 75314
176 84 ▶ DEBUG_MSG("set READ and WRITE, compare read_timeout = %f and write_timeout = %f",
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("set READ and WRITE, compare read_timeout = %f and write_timeout = %f", 0 84 84
177 read_timeout, write_timeout);
178 84 ▶ if(read_timeout < 0 && write_timeout < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(read_timeout < 0 && write_timeout < 0) { 82 2 84
if(read_timeout < 0 && write_timeout < 0) { 82 0 82
179 82 timeout = -1;
180 82 timeout_mode = WAIT_FOR_EVENT_WRITE_TIMEOUT;
181 2 ▶ } else if(read_timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(read_timeout < 0) { 0 2 2
182 0 timeout = write_timeout;
183 0 timeout_mode = WAIT_FOR_EVENT_WRITE_TIMEOUT;
184 2 ▶ } else if(write_timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(write_timeout < 0) { 2 0 2
185 2 timeout = read_timeout;
186 2 timeout_mode = WAIT_FOR_EVENT_READ_TIMEOUT;
187 0 ▶ } else if(read_timeout < write_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(read_timeout < write_timeout) { 0 0 0
188 0 timeout = read_timeout;
189 0 timeout_mode = WAIT_FOR_EVENT_READ_TIMEOUT;
190 } else {
191 0 timeout = write_timeout;
192 0 timeout_mode = WAIT_FOR_EVENT_WRITE_TIMEOUT;
193 }
194 75230 ▶ } else if(e->flags & WAIT_FOR_READ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(e->flags & WAIT_FOR_READ) { 90 75140 75230
195 90 ▶ DEBUG_MSG("set READ, read_timeout = %f", read_timeout);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("set READ, read_timeout = %f", read_timeout); 0 90 90
196 90 timeout = read_timeout;
197 90 timeout_mode = WAIT_FOR_EVENT_READ_TIMEOUT;
198 75140 ▶ } else if(e->flags & WAIT_FOR_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(e->flags & WAIT_FOR_WRITE) { 0 75140 75140
199 75140 ▶ DEBUG_MSG("set WRITE, write_timeout = %f", write_timeout);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("set WRITE, write_timeout = %f", write_timeout); 0 75140 75140
200 75140 timeout = write_timeout;
201 75140 timeout_mode = WAIT_FOR_EVENT_WRITE_TIMEOUT;
202 }
203  
204 75314 START_POLL:
205 75314 ▶ if (timeout < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (timeout < 0) { 75310 4 75314
206 75310 ms = -1;
207 } else {
208 4 ms = (int)(timeout * 1000 + 0.999);
209 }
210 75314 ▶ DEBUG_MSG("select start, timeout is %f", timeout);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("select start, timeout is %f", timeout); 0 75314 75314
211 75314 pollfd.fd = fd;
212 75314 pollfd.events = 0;
213 75314 pollfd.revents = 0;
214 75314 ▶ if(e->flags & WAIT_FOR_READ) { pollfd.events |= POLLIN; }
Branch: 1 branch
BranchTrueFalseTotal
if(e->flags & WAIT_FOR_READ) { pollfd.events |= POLLIN; } 174 75140 75314
215 75314 ▶ if(e->flags & WAIT_FOR_WRITE) { pollfd.events |= POLLOUT; }
Branch: 1 branch
BranchTrueFalseTotal
if(e->flags & WAIT_FOR_WRITE) { pollfd.events |= POLLOUT; } 75224 90 75314
216 75314 rc = poll(&pollfd, 1, ms);
217 75314 ▶ DEBUG_MSG("poll returns %d", rc);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("poll returns %d", rc); 0 75314 75314
218 75314 ▶ if(rc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(rc == 0) { 1 75313 75314
219 1 ▶ DEBUG_MSG("%s", "timeout");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "timeout"); 0 1 1
220 1 return timeout_mode;
221 }
222  
223 75313 ▶ if(rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(rc < 0) { 0 75313 75313
224 0 ▶ DEBUG_MSG("exception: %s", strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("exception: %s", strerror(errno)); 0 0 0
225 0 ▶ if( errno == EINTR ) {
Branch: 1 branch
BranchTrueFalseTotal
if( errno == EINTR ) { 0 0 0
226 0 ▶ PERL_ASYNC_CHECK();
Branch: 1 branch
BranchTrueFalseTotal
PERL_ASYNC_CHECK(); 0 0 0
227 0 ▶ DEBUG_MSG("%s", "recieved interrupt. retry wait_for_event");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "recieved interrupt. retry wait_for_event"); 0 0 0
228 0 goto START_POLL;
229 }
230 0 return WAIT_FOR_EVENT_EXCEPTION;
231 }
232 75313 ▶ if(self->ac && (pollfd.revents & POLLIN) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(self->ac && (pollfd.revents & POLLIN) != 0) { 75313 0 75313
if(self->ac && (pollfd.revents & POLLIN) != 0) { 89 75224 75313
233 89 ▶ DEBUG_MSG("ready to %s", "read");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("ready to %s", "read"); 0 89 89
234 89 redisAsyncHandleRead(self->ac);
235 }
236 75313 ▶ if(self->ac && (pollfd.revents & (POLLOUT|POLLHUP)) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(self->ac && (pollfd.revents & (POLLOUT|POLLHUP)) != 0) { 75302 11 75313
if(self->ac && (pollfd.revents & (POLLOUT|POLLHUP)) != 0) { 75224 78 75302
237 75224 ▶ DEBUG_MSG("ready to %s", "write");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("ready to %s", "write"); 0 75224 75224
238 75224 redisAsyncHandleWrite(self->ac);
239 }
240 75313 ▶ if((pollfd.revents & (POLLERR|POLLNVAL)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if((pollfd.revents & (POLLERR|POLLNVAL)) != 0) { 0 75313 75313
241 0 ▶ DEBUG_MSG(
Branch: 3 branches
BranchTrueFalseTotal
DEBUG_MSG( 0 0 0
DEBUG_MSG( 0 0 0
DEBUG_MSG( 0 0 0
242 "exception: %s%s",
243 (pollfd.revents & POLLERR) ? "POLLERR " : "",
244 (pollfd.revents & POLLNVAL) ? "POLLNVAL " : "");
245 0 return WAIT_FOR_EVENT_EXCEPTION;
246 }
247  
248 75313 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 75313 75313
249 75313 return WAIT_FOR_EVENT_OK;
250 }
251  
252  
253 156 static int _wait_all_responses(pTHX_ Redis__Fast self) {
254 156 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 156 156
255 323 ▶ while(self->ac && self->ac->replies.tail) {
Branch: 2 branches
BranchTrueFalseTotal
while(self->ac && self->ac->replies.tail) { 243 80 323
while(self->ac && self->ac->replies.tail) { 168 75 243
256 168 int res = wait_for_event(aTHX_ self, self->read_timeout, self->write_timeout);
257 168 ▶ if (res != WAIT_FOR_EVENT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (res != WAIT_FOR_EVENT_OK) { 1 167 168
258 1 ▶ DEBUG_MSG("error: %d", res);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %d", res); 0 1 1
259 1 return res;
260 }
261 }
262 155 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 155 155
263 155 return WAIT_FOR_EVENT_OK;
264 }
265  
266  
267 80 static void Redis__Fast_connect_cb(redisAsyncContext* c, int status) {
268 80 Redis__Fast self = (Redis__Fast)c->data;
269 80 ▶ DEBUG_MSG("connected status = %d", status);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("connected status = %d", status); 0 80 80
270 80 ▶ if(status != REDIS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if(status != REDIS_OK) { 0 80 80
271 // Connection Error!!
272 // Redis context will close automatically
273 0 self->ac = NULL;
274 } else {
275 80 self->is_connected = 1;
276 }
277 80 }
278  
279 80 static void Redis__Fast_disconnect_cb(redisAsyncContext* c, int status) {
280 80 Redis__Fast self = (Redis__Fast)c->data;
281 PERL_UNUSED_VAR(status);
282 80 ▶ DEBUG_MSG("disconnected status = %d", status);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("disconnected status = %d", status); 0 80 80
283 80 self->ac = NULL;
284 80 }
285  
286 81 static redisAsyncContext* __build_sock(pTHX_ Redis__Fast self)
287 {
288 redisAsyncContext *ac;
289 double timeout;
290 int res;
291  
292 81 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 81 81
293  
294 81 ▶ if(self->on_build_sock) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->on_build_sock) { 0 81 81
295 0 dSP;
296  
297 0 ENTER;
298 0 SAVETMPS;
299  
300 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
301 0 call_sv(self->on_build_sock, G_DISCARD | G_NOARGS);
302  
303 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
304 0 LEAVE;
305 }
306  
307 81 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 66 15 81
308 66 ac = redisAsyncConnectUnix(self->path);
309 } else {
310 15 ac = redisAsyncConnect(self->hostname, self->port);
311 }
312  
313 81 ▶ if(ac == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(ac == NULL) { 0 81 81
314 0 ▶ DEBUG_MSG("%s", "allocation error");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "allocation error"); 0 0 0
315 0 return NULL;
316 }
317 81 ▶ if(ac->err) {
Branch: 1 branch
BranchTrueFalseTotal
if(ac->err) { 0 81 81
318 0 ▶ DEBUG_MSG("connection error: %s", ac->errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("connection error: %s", ac->errstr); 0 0 0
319 0 redisAsyncFree(ac);
320 0 return NULL;
321 }
322  
323 81 ▶ if(self->ssl){
Branch: 1 branch
BranchTrueFalseTotal
if(self->ssl){ 9 72 81
324 redisSSLContext* ssl_context;
325 9 redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
326 18 redisSSLOptions options = {
327 .cacert_filename = NULL,
328 .capath = NULL,
329 .cert_filename = NULL,
330 .private_key_filename = NULL,
331 9 .server_name = self->hostname,
332 9 .verify_mode = self->ssl_verify_mode,
333 /* server_name is only used for SNI; verify_name makes the peer certificate match the hostname */
334 9 ▶ .verify_name = (self->ssl_verify_mode & REDIS_SSL_VERIFY_PEER) ? self->hostname : NULL,
Branch: 1 branch
BranchTrueFalseTotal
.verify_name = (self->ssl_verify_mode & REDIS_SSL_VERIFY_PEER) ? self->hostname : NULL, 6 3 9
335 };
336 9 ssl_context = redisCreateSSLContextWithOptions(&options, &ssl_error);
337  
338 9 ▶ if(ssl_context == NULL || ssl_error != REDIS_SSL_CTX_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if(ssl_context == NULL || ssl_error != REDIS_SSL_CTX_NONE) { 9 0 9
if(ssl_context == NULL || ssl_error != REDIS_SSL_CTX_NONE) { 0 9 9
339 0 ▶ DEBUG_MSG("ssl context error: %s", redisSSLContextGetError(ssl_error));
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("ssl context error: %s", redisSSLContextGetError(ssl_error)); 0 0 0
340 0 redisAsyncFree(ac);
341 0 redisFreeSSLContext(ssl_context);
342 1 return NULL;
343 }
344  
345 9 ▶ if (redisInitiateSSLWithContext(&ac->c, ssl_context) != REDIS_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (redisInitiateSSLWithContext(&ac->c, ssl_context) != REDIS_OK) { 1 8 9
346 1 ▶ DEBUG_MSG("ssl connection error: %s", ac->c.errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("ssl connection error: %s", ac->c.errstr); 0 1 1
347 1 redisAsyncFree(ac);
348 1 redisFreeSSLContext(ssl_context);
349 1 return NULL;
350 }
351 8 ▶ DEBUG_MSG("%s", "ssl connection setup");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "ssl connection setup"); 0 8 8
352 }
353  
354 80 ac->data = (void*)self;
355 80 self->ac = ac;
356 80 self->is_connected = 0;
357  
358 80 Attach(ac);
359 80 redisAsyncSetConnectCallback(ac, (redisConnectCallback*)Redis__Fast_connect_cb);
360 80 redisAsyncSetDisconnectCallback(ac, (redisDisconnectCallback*)Redis__Fast_disconnect_cb);
361  
362 // wait to connect...
363 80 timeout = -1;
364 80 ▶ if(self->cnx_timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->cnx_timeout) { 80 0 80
365 80 timeout = self->cnx_timeout;
366 }
367 75220 ▶ while(!self->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
while(!self->is_connected) { 75140 80 75220
368 75140 res = wait_for_event(aTHX_ self, timeout, timeout);
369 75140 ▶ if(self->ac == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->ac == NULL) { 0 75140 75140
370 // set is_connected flag to reconnect.
371 // see https://github.com/shogo82148/Redis-Fast/issues/73
372 0 self->is_connected = 1;
373  
374 0 return NULL;
375 }
376 75140 ▶ if(res != WAIT_FOR_EVENT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != WAIT_FOR_EVENT_OK) { 0 75140 75140
377 0 ▶ DEBUG_MSG("error: %d", res);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %d", res); 0 0 0
378  
379 // free the redis context
380 0 redisAsyncFree(self->ac);
381 0 _wait_all_responses(aTHX_ self);
382 0 self->ac = NULL;
383  
384 // set is_connected flag to reconnect.
385 // see https://github.com/shogo82148/Redis-Fast/issues/73
386 0 self->is_connected = 1;
387  
388 0 return NULL;
389 }
390 }
391 80 ▶ if(self->on_connect){
Branch: 1 branch
BranchTrueFalseTotal
if(self->on_connect){ 80 0 80
392 80 dSP;
393 80 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 80 80
394 80 call_sv(self->on_connect, G_DISCARD | G_NOARGS);
395 }
396  
397 80 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 80 80
398 80 return self->ac;
399 }
400  
401  
402 81 static void Redis__Fast_connect(pTHX_ Redis__Fast self) {
403 struct timeval start, end;
404  
405 81 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 81 81
406  
407 81 self->flags = 0;
408  
409 //$self->{queue} = [];
410 81 self->pid = getpid();
411  
412 81 ▶ if(self->reconnect == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->reconnect == 0) { 15 66 81
413 15 ▶ if(! __build_sock(aTHX_ self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(! __build_sock(aTHX_ self)) { 1 14 15
414 1 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 0 1 1
415 0 snprintf(self->error, MAX_ERROR_SIZE, "Could not connect to Redis server at %s", self->path);
416 } else {
417 1 snprintf(self->error, MAX_ERROR_SIZE, "Could not connect to Redis server at %s:%d", self->hostname, self->port);
418 }
419 1 croak("%s", self->error);
420 }
421 80 return ;
422 }
423  
424 // Reconnect...
425 66 gettimeofday(&start, NULL);
426 0 while (1) {
427 double elapsed_time;
428 66 ▶ if(__build_sock(aTHX_ self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(__build_sock(aTHX_ self)) { 66 0 66
429 // Connected!
430 66 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 66 66
431 66 return;
432 }
433 0 gettimeofday(&end, NULL);
434 0 elapsed_time = (end.tv_sec-start.tv_sec) + 1E-6 * (end.tv_usec-start.tv_usec);
435 0 ▶ DEBUG_MSG("elapsed time:%f, reconnect:%lf", elapsed_time, self->reconnect);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("elapsed time:%f, reconnect:%lf", elapsed_time, self->reconnect); 0 0 0
436 0 ▶ if( elapsed_time > self->reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if( elapsed_time > self->reconnect) { 0 0 0
437 0 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 0 0 0
438 0 snprintf(self->error, MAX_ERROR_SIZE, "Could not connect to Redis server at %s", self->path);
439 } else {
440 0 snprintf(self->error, MAX_ERROR_SIZE, "Could not connect to Redis server at %s:%d", self->hostname, self->port);
441 }
442 0 ▶ DEBUG_MSG("%s", "timed out");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "timed out"); 0 0 0
443 0 croak("%s", self->error);
444 return;
445 }
446 0 ▶ DEBUG_MSG("%s", "failed to connect. wait...");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "failed to connect. wait..."); 0 0 0
447 0 usleep(self->every);
448 }
449 DEBUG_MSG("%s", "finish");
450 }
451  
452 // reconnect if the current connection is closed.
453 // the caller must check self->ac != 0 to continue.
454 93 static void Redis__Fast_reconnect(pTHX_ Redis__Fast self) {
455 93 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 93 93
456 93 ▶ if(self->is_connected && !self->ac && self->reconnect > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if(self->is_connected && !self->ac && self->reconnect > 0) { 93 0 93
if(self->is_connected && !self->ac && self->reconnect > 0) { 8 85 93
if(self->is_connected && !self->ac && self->reconnect > 0) { 0 8 8
457 0 ▶ DEBUG_MSG("%s", "connection not found. reconnect");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "connection not found. reconnect"); 0 0 0
458 0 Redis__Fast_connect(aTHX_ self);
459 }
460 93 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 8 85 93
461 8 ▶ DEBUG_MSG("%s", "Not connected to any server");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "Not connected to any server"); 0 8 8
462 }
463 93 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 93 93
464 93 }
465  
466 191 static redis_fast_reply_t Redis__Fast_decode_reply(pTHX_ Redis__Fast self, redisReply* reply, int collect_errors) {
467 191 redis_fast_reply_t res = {NULL, NULL};
468  
469 191 switch (reply->type) {
470 14 case REDIS_REPLY_ERROR:
471 14 res.error = sv_2mortal(newSVpvn(reply->str, reply->len));
472 14 break;
473 85 case REDIS_REPLY_STRING:
474 case REDIS_REPLY_STATUS:
475 85 res.result = sv_2mortal(newSVpvn(reply->str, reply->len));
476 85 break;
477  
478 42 case REDIS_REPLY_INTEGER:
479 42 res.result = sv_2mortal(newSViv(reply->integer));
480 42 break;
481 18 case REDIS_REPLY_NIL:
482 18 res.result = &PL_sv_undef;
483 18 break;
484  
485 32 case REDIS_REPLY_ARRAY:
486 case REDIS_REPLY_PUSH: {
487 32 AV* av = newAV();
488 size_t i;
489 32 res.result = sv_2mortal(newRV_noinc((SV*)av));
490  
491 141 ▶ for (i = 0; i < reply->elements; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reply->elements; i++) { 109 32 141
492 109 redis_fast_reply_t elem = Redis__Fast_decode_reply(aTHX_ self, reply->element[i], collect_errors);
493 109 ▶ if(collect_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if(collect_errors) { 3 106 109
494 3 AV* elem_av = (AV*)sv_2mortal((SV*)newAV());
495 3 ▶ if(elem.result) {
Branch: 1 branch
BranchTrueFalseTotal
if(elem.result) { 2 1 3
496 2 av_push(elem_av, SvREFCNT_inc(elem.result));
497 } else {
498 1 av_push(elem_av, newSV(0));
499 }
500 3 ▶ if(elem.error) {
Branch: 1 branch
BranchTrueFalseTotal
if(elem.error) { 1 2 3
501 1 av_push(elem_av, SvREFCNT_inc(elem.error));
502 } else {
503 2 av_push(elem_av, newSV(0));
504 }
505 3 av_push(av, newRV_inc((SV*)elem_av));
506 } else {
507 106 ▶ if(elem.result) {
Branch: 1 branch
BranchTrueFalseTotal
if(elem.result) { 104 2 106
508 104 av_push(av, SvREFCNT_inc(elem.result));
509 } else {
510 2 av_push(av, newSV(0));
511 }
512 106 ▶ if(elem.error && !res.error) {
Branch: 2 branches
BranchTrueFalseTotal
if(elem.error && !res.error) { 2 104 106
if(elem.error && !res.error) { 2 0 2
513 2 res.error = elem.error;
514 }
515 }
516 }
517 32 break;
518 }
519 }
520  
521 191 return res;
522 }
523  
524 65 static int Redis__Fast_call_reconnect_on_error(pTHX_ Redis__Fast self, redis_fast_reply_t ret, const void *command_name, STRLEN command_length) {
525 65 int _need_reconnect = 0;
526 struct timeval current;
527 double current_sec;
528 SV* sv_ret;
529 SV* sv_err;
530 SV* sv_cmd;
531 int count;
532  
533 65 ▶ if (ret.error == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret.error == NULL) { 52 13 65
534 52 return _need_reconnect;
535 }
536 13 ▶ if (self->reconnect_on_error == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->reconnect_on_error == NULL) { 13 0 13
537 13 return _need_reconnect;
538 }
539  
540 0 gettimeofday(&current, NULL);
541 0 current_sec = current.tv_sec + 1E-6 * current.tv_usec;
542 0 ▶ if( self->next_reconnect_on_error_at < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if( self->next_reconnect_on_error_at < 0 || 0 0 0
543 0 ▶ self->next_reconnect_on_error_at < current_sec) {
Branch: 1 branch
BranchTrueFalseTotal
self->next_reconnect_on_error_at < current_sec) { 0 0 0
544 0 dSP;
545 0 ENTER;
546 0 SAVETMPS;
547  
548 0 ▶ sv_ret = ret.result ? ret.result : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
sv_ret = ret.result ? ret.result : &PL_sv_undef; 0 0 0
549 0 sv_err = ret.error;
550 0 sv_cmd = sv_2mortal(newSVpvn((const char*)command_name, command_length));
551  
552 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
553 0 ▶ XPUSHs(sv_err);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_err); 0 0 0
554 0 ▶ XPUSHs(sv_ret);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_ret); 0 0 0
555 0 ▶ XPUSHs(sv_cmd);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_cmd); 0 0 0
556 0 PUTBACK;
557  
558 0 count = call_sv(self->reconnect_on_error, G_SCALAR);
559  
560 0 SPAGAIN;
561  
562 0 ▶ if (count != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) { 0 0 0
563 0 croak("[BUG] retval count should be 1\n");
564 }
565 0 _need_reconnect = POPi;
566  
567 0 PUTBACK;
568 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
569 0 LEAVE;
570 }
571  
572 0 return _need_reconnect;
573 }
574  
575 80 static void Redis__Fast_sync_reply_cb(redisAsyncContext* c, void* reply, void* privdata) {
576 dTHX;
577  
578 80 Redis__Fast self = (Redis__Fast)c->data;
579 80 redis_fast_sync_cb_t *cbt = (redis_fast_sync_cb_t*)privdata;
580 80 ▶ DEBUG_MSG("%p", (void*)privdata);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%p", (void*)privdata); 0 80 80
581 80 ▶ if(reply) {
Branch: 1 branch
BranchTrueFalseTotal
if(reply) { 74 6 80
582 74 self->flags = (self->flags | cbt->on_flags) & cbt->off_flags;
583 74 ▶ if(cbt->custom_decode) {
Branch: 1 branch
BranchTrueFalseTotal
if(cbt->custom_decode) { 5 69 74
584 5 cbt->ret = (cbt->custom_decode)(aTHX_ self, (redisReply*)reply, cbt->collect_errors);
585 } else {
586 69 cbt->ret = Redis__Fast_decode_reply(aTHX_ self, (redisReply*)reply, cbt->collect_errors);
587 }
588 6 ▶ } else if(c->c.flags & REDIS_FREEING) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(c->c.flags & REDIS_FREEING) { 1 5 6
589 1 ▶ DEBUG_MSG("%s", "redis freeing");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "redis freeing"); 0 1 1
590 1 Safefree(cbt);
591 } else {
592 5 ▶ DEBUG_MSG("connect error: %s", c->errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("connect error: %s", c->errstr); 0 5 5
593 5 self->need_reconnect = 1;
594 5 cbt->ret.result = NULL;
595 5 cbt->ret.error = sv_2mortal( newSVpvn(c->errstr, strlen(c->errstr)) );
596 }
597 80 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 80 80
598 80 }
599  
600 1 static void Redis__Fast_async_reply_cb(redisAsyncContext* c, void* reply, void* privdata) {
601 dTHX;
602  
603 1 Redis__Fast self = (Redis__Fast)c->data;
604 1 redis_fast_async_cb_t *cbt = (redis_fast_async_cb_t*)privdata;
605 1 ▶ DEBUG_MSG("%p, %p", reply, privdata);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%p, %p", reply, privdata); 0 1 1
606 1 ▶ if (reply) {
Branch: 1 branch
BranchTrueFalseTotal
if (reply) { 1 0 1
607 1 self->flags = (self->flags | cbt->on_flags) & cbt->off_flags;
608  
609 {
610 redis_fast_reply_t result;
611  
612 1 dSP;
613  
614 1 ENTER;
615 1 SAVETMPS;
616  
617 1 ▶ if(cbt->custom_decode) {
Branch: 1 branch
BranchTrueFalseTotal
if(cbt->custom_decode) { 0 1 1
618 0 result = (cbt->custom_decode)(aTHX_ self, (redisReply*)reply, cbt->collect_errors);
619 } else {
620 1 result = Redis__Fast_decode_reply(aTHX_ self, (redisReply*)reply, cbt->collect_errors);
621 }
622  
623 1 ▶ if(result.result == NULL) result.result = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if(result.result == NULL) result.result = &PL_sv_undef; 0 1 1
624 1 ▶ if(result.error == NULL) result.error = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if(result.error == NULL) result.error = &PL_sv_undef; 1 0 1
625  
626 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
627 1 ▶ XPUSHs(result.result);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result.result); 0 1 1
628 1 ▶ XPUSHs(result.error);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result.error); 0 1 1
629 1 PUTBACK;
630  
631 1 call_sv(cbt->cb, G_DISCARD);
632  
633 1 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1 0 1
634 1 LEAVE;
635 }
636  
637 {
638 1 ▶ if (0 < self->reconnect && !self->need_reconnect
Branch: 2 branches
BranchTrueFalseTotal
if (0 < self->reconnect && !self->need_reconnect 1 0 1
if (0 < self->reconnect && !self->need_reconnect 1 0 1
639 // Avoid useless cost when reconnect_on_error is not set.
640 1 ▶ && self->reconnect_on_error != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
&& self->reconnect_on_error != NULL) { 0 1 1
641 redis_fast_reply_t result;
642 0 ▶ if(cbt->custom_decode) {
Branch: 1 branch
BranchTrueFalseTotal
if(cbt->custom_decode) { 0 0 0
643 0 result = (cbt->custom_decode)(
644 aTHX_ self, (redisReply*)reply, cbt->collect_errors
645 );
646 } else {
647 0 result = Redis__Fast_decode_reply(
648 aTHX_ self, (redisReply*)reply, cbt->collect_errors
649 );
650 }
651 0 self->need_reconnect = Redis__Fast_call_reconnect_on_error(
652 aTHX_ self, result, cbt->command_name, cbt->command_length
653 );
654 }
655 }
656 } else {
657 0 ▶ if (c->c.flags & REDIS_FREEING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->c.flags & REDIS_FREEING) { 0 0 0
658 0 ▶ DEBUG_MSG("%s", "redis freeing");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "redis freeing"); 0 0 0
659 } else {
660 0 ▶ DEBUG_MSG("connect error: %s", c->errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("connect error: %s", c->errstr); 0 0 0
661 }
662  
663 {
664 redis_fast_reply_t result;
665 const char *msg;
666  
667 0 dSP;
668  
669 0 ENTER;
670 0 SAVETMPS;
671  
672 0 result.result = &PL_sv_undef;
673 0 ▶ if (c->c.flags & REDIS_FREEING) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->c.flags & REDIS_FREEING) { 0 0 0
674 0 msg = "redis freeing";
675 } else {
676 0 msg = c->errstr;
677 }
678 0 ▶ DEBUG_MSG("error: %s", msg);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %s", msg); 0 0 0
679 0 result.error = sv_2mortal(newSVpvn(msg, strlen(msg)));
680  
681 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
682 0 ▶ XPUSHs(result.result);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result.result); 0 0 0
683 0 ▶ XPUSHs(result.error);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(result.error); 0 0 0
684 0 PUTBACK;
685  
686 0 call_sv(cbt->cb, G_DISCARD);
687  
688 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
689 0 LEAVE;
690 }
691 }
692  
693 1 SvREFCNT_dec(cbt->cb);
694 1 Safefree(cbt);
695 1 }
696  
697 /*
698 * The callback for subscriptions doesn't own any private data.
699 * hiredis keeps the callback of a channel until all of its pending subscriptions are confirmed,
700 * and the server decides when that happens, so we can't tell when hiredis drops it.
701 * The Perl callback is held by self->subscription_cb instead.
702 */
703 15 static void Redis__Fast_subscribe_cb(redisAsyncContext* c, void* reply, void* privdata) {
704 dTHX;
705  
706 15 Redis__Fast self = (Redis__Fast)c->data;
707 15 redisReply* r = (redisReply*)reply;
708 PERL_UNUSED_VAR(privdata);
709  
710 15 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 15 15
711 15 ▶ if (!r) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { 3 12 15
712 3 ▶ DEBUG_MSG("connect error: %s", c->errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("connect error: %s", c->errstr); 0 3 3
713 3 return;
714 }
715 /* subscription replies are arrays in RESP2 and pushes in RESP3 */
716 12 ▶ if ((r->type != REDIS_REPLY_ARRAY && r->type != REDIS_REPLY_PUSH) || r->elements < 3 || r->element[0]->type != REDIS_REPLY_STRING) {
Branch: 4 branches
BranchTrueFalseTotal
if ((r->type != REDIS_REPLY_ARRAY && r->type != REDIS_REPLY_PUSH) || r->elements < 3 || r->element[0]->type != REDIS_REPLY_STRING) { 2 10 12
if ((r->type != REDIS_REPLY_ARRAY && r->type != REDIS_REPLY_PUSH) || r->elements < 3 || r->element[0]->type != REDIS_REPLY_STRING) { 2 0 2
if ((r->type != REDIS_REPLY_ARRAY && r->type != REDIS_REPLY_PUSH) || r->elements < 3 || r->element[0]->type != REDIS_REPLY_STRING) { 12 0 12
if ((r->type != REDIS_REPLY_ARRAY && r->type != REDIS_REPLY_PUSH) || r->elements < 3 || r->element[0]->type != REDIS_REPLY_STRING) { 0 12 12
717 0 ▶ DEBUG_MSG("%s", "unexpected reply");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "unexpected reply"); 0 0 0
718 0 return;
719 }
720  
721 {
722 12 char* stype = r->element[0]->str;
723 12 int pvariant = (tolower(stype[0]) == 'p') ? 1 : 0;
724 redis_fast_reply_t res;
725  
726 12 dSP;
727 12 ENTER;
728 12 SAVETMPS;
729  
730 12 res = Redis__Fast_decode_reply(aTHX_ self, r, 0);
731  
732 12 ▶ if (strcasecmp(stype+pvariant,"subscribe") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(stype+pvariant,"subscribe") == 0) { 3 9 12
733 3 ▶ DEBUG_MSG("%s %s %lld", r->element[0]->str, r->element[1]->str, r->element[2]->integer);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s %s %lld", r->element[0]->str, r->element[1]->str, r->element[2]->integer); 0 3 3
734 3 self->is_subscriber = r->element[2]->integer;
735 3 self->expected_subs--;
736 9 ▶ } else if (strcasecmp(stype+pvariant,"unsubscribe") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcasecmp(stype+pvariant,"unsubscribe") == 0) { 2 7 9
737 2 ▶ DEBUG_MSG("%s %s %lld", r->element[0]->str, r->element[1]->str, r->element[2]->integer);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s %s %lld", r->element[0]->str, r->element[1]->str, r->element[2]->integer); 0 2 2
738 2 self->is_subscriber = r->element[2]->integer;
739 2 self->expected_subs--;
740 } else {
741 7 ▶ DEBUG_MSG("%s %s", r->element[0]->str, r->element[1]->str);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s %s", r->element[0]->str, r->element[1]->str); 0 7 7
742 7 self->proccess_sub_count++;
743 }
744  
745 12 ▶ if(res.result == NULL) res.result = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if(res.result == NULL) res.result = &PL_sv_undef; 0 12 12
746 12 ▶ if(res.error == NULL) res.error = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if(res.error == NULL) res.error = &PL_sv_undef; 12 0 12
747  
748 12 ▶ if (self->subscription_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->subscription_cb) { 12 0 12
749 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
750 12 ▶ XPUSHs(res.result);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(res.result); 0 12 12
751 12 ▶ XPUSHs(res.error);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(res.error); 0 12 12
752 12 PUTBACK;
753  
754 12 call_sv(self->subscription_cb, G_DISCARD);
755 }
756  
757 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 12 0 12
758 12 LEAVE;
759 }
760 12 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 12 12
761 }
762  
763 0 static void Redis__Fast_quit(pTHX_ Redis__Fast self) {
764 redis_fast_sync_cb_t *cbt;
765  
766 0 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 0 0
767 0 return;
768 }
769  
770 0 Newx(cbt, sizeof(redis_fast_sync_cb_t), redis_fast_sync_cb_t);
771 0 cbt->ret.result = NULL;
772 0 cbt->ret.error = NULL;
773 0 cbt->custom_decode = NULL;
774  
775 // initialize, or self->flags will be corrupted.
776 0 cbt->on_flags = 0;
777 0 cbt->off_flags = 0;
778  
779 0 redisAsyncCommand(
780 self->ac, Redis__Fast_sync_reply_cb, cbt, "QUIT"
781 );
782 0 redisAsyncDisconnect(self->ac);
783 0 ▶ if(_wait_all_responses(aTHX_ self) == WAIT_FOR_EVENT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if(_wait_all_responses(aTHX_ self) == WAIT_FOR_EVENT_OK) { 0 0 0
784 0 ▶ DEBUG_MSG("%s", "wait_all_responses ok");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "wait_all_responses ok"); 0 0 0
785 0 ▶ if(cbt->ret.result || cbt->ret.error) Safefree(cbt);
Branch: 2 branches
BranchTrueFalseTotal
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 0 0 0
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 0 0 0
786 } else {
787 0 ▶ DEBUG_MSG("%s", "wait_all_responses not ok");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "wait_all_responses not ok"); 0 0 0
788 0 ▶ if(cbt->ret.result || cbt->ret.error) Safefree(cbt);
Branch: 2 branches
BranchTrueFalseTotal
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 0 0 0
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 0 0 0
789 }
790 0 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 0 0
791 }
792  
793 81 static redis_fast_reply_t Redis__Fast_run_cmd(pTHX_ Redis__Fast self, int collect_errors, CUSTOM_DECODE custom_decode, SV* cb, int argc, const char** argv, size_t* argvlen) {
794 81 redis_fast_reply_t ret = {NULL, NULL};
795 81 int on_flags = 0, off_flags = ~0;
796  
797 81 ▶ DEBUG_MSG("start %s", argv[0]);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("start %s", argv[0]); 0 81 81
798  
799 81 ▶ DEBUG_MSG("pid check: previous pid is %d, now %d", self->pid, getpid());
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("pid check: previous pid is %d, now %d", self->pid, getpid()); 0 81 81
800 81 ▶ if(self->pid != getpid()) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->pid != getpid()) { 0 81 81
801 0 ▶ DEBUG_MSG("%s", "pid changed. create new connection..");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "pid changed. create new connection.."); 0 0 0
802 0 Redis__Fast_connect(aTHX_ self);
803 }
804  
805 81 ▶ if(EQUALS_COMMAND(argvlen[0], argv[0], "MULTI")) {
Branch: 2 branches
BranchTrueFalseTotal
if(EQUALS_COMMAND(argvlen[0], argv[0], "MULTI")) { 0 81 81
if(EQUALS_COMMAND(argvlen[0], argv[0], "MULTI")) { 0 0 0
806 0 on_flags = FLAG_INSIDE_TRANSACTION;
807 81 ▶ } else if(EQUALS_COMMAND(argvlen[0], argv[0], "EXEC") ||
Branch: 2 branches
BranchTrueFalseTotal
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "EXEC") || 14 67 81
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "EXEC") || 13 1 14
808 80 ▶ EQUALS_COMMAND(argvlen[0], argv[0], "DISCARD")) {
Branch: 2 branches
BranchTrueFalseTotal
EQUALS_COMMAND(argvlen[0], argv[0], "DISCARD")) { 0 80 80
EQUALS_COMMAND(argvlen[0], argv[0], "DISCARD")) { 0 0 0
809 1 off_flags = ~(FLAG_INSIDE_TRANSACTION | FLAG_INSIDE_WATCH);
810 80 ▶ } else if(EQUALS_COMMAND(argvlen[0], argv[0], "WATCH")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "WATCH")) { 0 80 80
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "WATCH")) { 0 0 0
811 0 on_flags = FLAG_INSIDE_WATCH;
812 80 ▶ } else if(EQUALS_COMMAND(argvlen[0], argv[0], "UNWATCH")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "UNWATCH")) { 0 80 80
} else if(EQUALS_COMMAND(argvlen[0], argv[0], "UNWATCH")) { 0 0 0
813 0 off_flags = ~FLAG_INSIDE_WATCH;
814 }
815  
816 81 ▶ if(cb) {
Branch: 1 branch
BranchTrueFalseTotal
if(cb) { 1 80 81
817 redis_fast_async_cb_t *cbt;
818 1 Newx(cbt, sizeof(redis_fast_async_cb_t), redis_fast_async_cb_t);
819 1 cbt->cb = SvREFCNT_inc(cb);
820 1 cbt->custom_decode = custom_decode;
821 1 cbt->collect_errors = collect_errors;
822 1 cbt->on_flags = on_flags;
823 1 cbt->off_flags = off_flags;
824 1 cbt->command_name = argv[0];
825 1 cbt->command_length = argvlen[0];
826 1 redisAsyncCommandArgv(
827 self->ac, Redis__Fast_async_reply_cb, cbt,
828 argc, argv, argvlen
829 );
830 1 ret.result = sv_2mortal(newSViv(1));
831 } else {
832 redis_fast_sync_cb_t *cbt;
833 80 ▶ int i, cnt = (self->reconnect == 0 ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
int i, cnt = (self->reconnect == 0 ? 1 : 2); 15 65 80
834 80 int res = WAIT_FOR_EVENT_OK;
835 80 ▶ for(i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < cnt; i++) { 80 0 80
836 80 Newx(cbt, sizeof(redis_fast_sync_cb_t), redis_fast_sync_cb_t);
837 80 self->need_reconnect = 0;
838 80 cbt->ret.result = NULL;
839 80 cbt->ret.error = NULL;
840 80 cbt->custom_decode = custom_decode;
841 80 cbt->collect_errors = collect_errors;
842 80 cbt->on_flags = on_flags;
843 80 cbt->off_flags = off_flags;
844 80 ▶ DEBUG_MSG("%s", "send command in sync mode");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "send command in sync mode"); 0 80 80
845 80 redisAsyncCommandArgv(
846 self->ac, Redis__Fast_sync_reply_cb, cbt,
847 argc, argv, argvlen
848 );
849 80 ▶ DEBUG_MSG("%s", "waiting response");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "waiting response"); 0 80 80
850 80 res = _wait_all_responses(aTHX_ self);
851 80 ▶ if(res == WAIT_FOR_EVENT_OK && self->need_reconnect == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(res == WAIT_FOR_EVENT_OK && self->need_reconnect == 0) { 79 1 80
if(res == WAIT_FOR_EVENT_OK && self->need_reconnect == 0) { 74 5 79
852 74 int _need_reconnect = 0;
853 74 ▶ if (1 < cnt - i) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < cnt - i) { 65 9 74
854 65 _need_reconnect = Redis__Fast_call_reconnect_on_error(
855 aTHX_ self, cbt->ret, argv[0], argvlen[0]
856 );
857 // Should be quit before reconnect
858 65 ▶ if (_need_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (_need_reconnect) { 0 65 65
859 0 Redis__Fast_quit(aTHX_ self);
860 }
861 }
862 74 ▶ if (!_need_reconnect) {
Branch: 1 branch
BranchTrueFalseTotal
if (!_need_reconnect) { 74 0 74
863 74 ret = cbt->ret;
864 74 ▶ if(cbt->ret.result || cbt->ret.error) Safefree(cbt);
Branch: 2 branches
BranchTrueFalseTotal
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 11 63 74
if(cbt->ret.result || cbt->ret.error) Safefree(cbt); 11 0 11
865 74 ▶ DEBUG_MSG("finish %s", argv[0]);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("finish %s", argv[0]); 0 74 74
866 74 return ret;
867 }
868 }
869  
870 6 ▶ if( res == WAIT_FOR_EVENT_READ_TIMEOUT ) break;
Branch: 1 branch
BranchTrueFalseTotal
if( res == WAIT_FOR_EVENT_READ_TIMEOUT ) break; 1 5 6
871  
872 5 ▶ if(self->flags & (FLAG_INSIDE_TRANSACTION | FLAG_INSIDE_WATCH)) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->flags & (FLAG_INSIDE_TRANSACTION | FLAG_INSIDE_WATCH)) { 0 5 5
873 0 char *msg = "reconnect disabled inside transaction or watch";
874 0 ▶ DEBUG_MSG("error: %s", msg);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %s", msg); 0 0 0
875 0 ret.error = sv_2mortal(newSVpvn(msg, strlen(msg)));
876 0 return ret;
877 }
878  
879 5 Redis__Fast_reconnect(aTHX_ self);
880 5 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 5 0 5
881 5 char *msg = "Not connected to any server";
882 5 ▶ DEBUG_MSG("error: %s", msg);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %s", msg); 0 5 5
883 5 ret.error = sv_2mortal(newSVpvn(msg, strlen(msg)));
884 5 return ret;
885 }
886 }
887  
888 1 ▶ if( res == WAIT_FOR_EVENT_OK && (cbt->ret.result || cbt->ret.error) ) Safefree(cbt);
Branch: 3 branches
BranchTrueFalseTotal
if( res == WAIT_FOR_EVENT_OK && (cbt->ret.result || cbt->ret.error) ) Safefree(cbt); 0 1 1
if( res == WAIT_FOR_EVENT_OK && (cbt->ret.result || cbt->ret.error) ) Safefree(cbt); 0 0 0
if( res == WAIT_FOR_EVENT_OK && (cbt->ret.result || cbt->ret.error) ) Safefree(cbt); 0 0 0
889 // else destructor will release cbt
890  
891 1 ▶ if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) {
Branch: 2 branches
BranchTrueFalseTotal
if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) { 0 1 1
if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) { 0 0 0
892 1 snprintf(self->error, MAX_ERROR_SIZE, "Error while reading from Redis server: %s", strerror(EAGAIN));
893 1 errno = EAGAIN;
894 1 ▶ DEBUG_MSG("error: %s", self->error);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %s", self->error); 0 1 1
895 1 ret.error = sv_2mortal(newSVpvn(self->error, strlen(self->error)));
896 1 return ret;
897 }
898 0 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 0 0
899 0 char *msg = "Not connected to any server";
900 0 ▶ DEBUG_MSG("error: %s", msg);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("error: %s", msg); 0 0 0
901 0 ret.error = sv_2mortal(newSVpvn(msg, strlen(msg)));
902 0 return ret;
903 }
904 }
905 1 ▶ DEBUG_MSG("Finish %s", argv[0]);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("Finish %s", argv[0]); 0 1 1
906 1 return ret;
907 }
908  
909 0 static redis_fast_reply_t Redis__Fast_keys_custom_decode(pTHX_ Redis__Fast self, redisReply* reply, int collect_errors) {
910 // TODO: Support redis <= 1.2.6
911 0 return Redis__Fast_decode_reply(aTHX_ self, reply, collect_errors);
912 }
913  
914 5 static redis_fast_reply_t Redis__Fast_info_custom_decode(pTHX_ Redis__Fast self, redisReply* reply, int collect_errors) {
915 5 redis_fast_reply_t res = {NULL, NULL};
916  
917 5 ▶ if(reply->type == REDIS_REPLY_STRING ||
Branch: 1 branch
BranchTrueFalseTotal
if(reply->type == REDIS_REPLY_STRING || 0 5 5
918 5 ▶ reply->type == REDIS_REPLY_STATUS) {
Branch: 1 branch
BranchTrueFalseTotal
reply->type == REDIS_REPLY_STATUS) { 0 0 0
919  
920 5 HV* hv = newHV();
921 5 char* str = reply->str;
922 5 size_t len = reply->len;
923 5 res.result = sv_2mortal(newRV_noinc((SV*)hv));
924  
925 12 ▶ while(len != 0) {
Branch: 1 branch
BranchTrueFalseTotal
while(len != 0) { 8 4 12
926 8 const char* line = (char*)memchr(str, '\r', len);
927 const char* sep;
928 size_t linelen;
929 8 ▶ if(line == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(line == NULL) { 1 7 8
930 1 linelen = len;
931 } else {
932 7 linelen = line - str;
933 }
934 8 sep = (char*)memchr(str, ':', linelen);
935 8 ▶ if(str[0] != '#' && sep != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if(str[0] != '#' && sep != NULL) { 8 0 8
if(str[0] != '#' && sep != NULL) { 7 1 8
936 SV* val;
937 SV** ret;
938 size_t keylen;
939 7 keylen = sep - str;
940 7 val = sv_2mortal(newSVpvn(sep + 1, linelen - keylen - 1));
941 7 ret = hv_store(hv, str, keylen, SvREFCNT_inc(val), 0);
942 7 ▶ if (ret == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == NULL) { 0 7 7
943 0 SvREFCNT_dec(val);
944 0 croak("failed to hv_store");
945 }
946 }
947 8 ▶ if(line == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(line == NULL) { 1 7 8
948 1 break;
949 } else {
950 // skip "\r" and the following "\n", without going past the end of the reply
951 7 size_t skip = linelen + 1;
952 7 ▶ if(skip < len && str[skip] == '\n') skip++;
Branch: 2 branches
BranchTrueFalseTotal
if(skip < len && str[skip] == '\n') skip++; 4 3 7
if(skip < len && str[skip] == '\n') skip++; 3 1 4
953 7 len -= skip;
954 7 str += skip;
955 }
956 }
957 } else {
958 0 res = Redis__Fast_decode_reply(aTHX_ self, reply, collect_errors);
959 }
960  
961 5 return res;
962 }
963  
964 MODULE = Redis::Fast PACKAGE = Redis::Fast
965  
966 SV*
967 _new(char* cls);
968 PREINIT:
969 redis_fast_t* self;
970 CODE:
971 {
972 87 redisInitOpenSSL();
973 87 Newxz(self, sizeof(redis_fast_t), redis_fast_t);
974 87 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 87 87
975 87 self->error = (char*)malloc(MAX_ERROR_SIZE);
976 87 self->reconnect_on_error = NULL;
977 87 self->next_reconnect_on_error_at = -1;
978 87 self->is_connected = 1;
979 87 self->ssl_verify_mode = REDIS_SSL_VERIFY_PEER;
980 87 ST(0) = sv_newmortal();
981 87 sv_setref_pv(ST(0), cls, (void*)self);
982 87 ▶ DEBUG_MSG("return %p", ST(0));
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("return %p", ST(0)); 0 87 87
983 87 XSRETURN(1);
984 }
985 OUTPUT:
986 RETVAL
987  
988 double
989 __set_reconnect(Redis::Fast self, double val)
990 CODE:
991 {
992 247 ▶ RETVAL = self->reconnect = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect = val; 68 179 247
993 }
994 OUTPUT:
995 RETVAL
996  
997  
998 double
999 __get_reconnect(Redis::Fast self)
1000 CODE:
1001 {
1002 80 ▶ RETVAL = self->reconnect;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->reconnect; 0 80 80
1003 }
1004 OUTPUT:
1005 RETVAL
1006  
1007  
1008 int
1009 __set_every(Redis::Fast self, int val)
1010 CODE:
1011 {
1012 87 ▶ RETVAL = self->every = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->every = val; 68 19 87
1013 }
1014 OUTPUT:
1015 RETVAL
1016  
1017  
1018 int
1019 __get_every(Redis::Fast self)
1020 CODE:
1021 {
1022 0 ▶ RETVAL = self->every;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->every; 0 0 0
1023 }
1024 OUTPUT:
1025 RETVAL
1026  
1027 int
1028 __set_debug(Redis::Fast self, int val)
1029 CODE:
1030 {
1031 87 ▶ RETVAL = self->debug = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->debug = val; 68 19 87
1032 }
1033 OUTPUT:
1034 RETVAL
1035  
1036 double
1037 __set_cnx_timeout(Redis::Fast self, double val)
1038 CODE:
1039 {
1040 87 ▶ RETVAL = self->cnx_timeout = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->cnx_timeout = val; 68 19 87
1041 }
1042 OUTPUT:
1043 RETVAL
1044  
1045 double
1046 __get_cnx_timeout(Redis::Fast self)
1047 CODE:
1048 {
1049 0 ▶ RETVAL = self->cnx_timeout;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->cnx_timeout; 0 0 0
1050 }
1051 OUTPUT:
1052 RETVAL
1053  
1054  
1055 double
1056 __set_read_timeout(Redis::Fast self, double val)
1057 CODE:
1058 {
1059 87 ▶ RETVAL = self->read_timeout = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->read_timeout = val; 68 19 87
1060 }
1061 OUTPUT:
1062 RETVAL
1063  
1064 double
1065 __get_read_timeout(Redis::Fast self)
1066 CODE:
1067 {
1068 0 ▶ RETVAL = self->read_timeout;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->read_timeout; 0 0 0
1069 }
1070 OUTPUT:
1071 RETVAL
1072  
1073  
1074 double
1075 __set_write_timeout(Redis::Fast self, double val)
1076 CODE:
1077 {
1078 87 ▶ RETVAL = self->write_timeout = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->write_timeout = val; 68 19 87
1079 }
1080 OUTPUT:
1081 RETVAL
1082  
1083 double
1084 __get_write_timeout(Redis::Fast self)
1085 CODE:
1086 {
1087 0 ▶ RETVAL = self->write_timeout;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->write_timeout; 0 0 0
1088 }
1089 OUTPUT:
1090 RETVAL
1091  
1092  
1093 int
1094 __set_current_database(Redis::Fast self, int val)
1095 CODE:
1096 {
1097 0 ▶ RETVAL = self->current_database = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->current_database = val; 0 0 0
1098 }
1099 OUTPUT:
1100 RETVAL
1101  
1102  
1103 int
1104 __get_current_database(Redis::Fast self)
1105 CODE:
1106 {
1107 0 ▶ RETVAL = self->current_database;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->current_database; 0 0 0
1108 }
1109 OUTPUT:
1110 RETVAL
1111  
1112  
1113 int
1114 __sock(Redis::Fast self)
1115 CODE:
1116 {
1117 8 ▶ RETVAL = self->ac ? self->ac->c.fd : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = self->ac ? self->ac->c.fd : 0; 8 0 8
RETVAL = self->ac ? self->ac->c.fd : 0; 5 3 8
1118 }
1119 OUTPUT:
1120 RETVAL
1121  
1122  
1123 int
1124 __get_port(Redis::Fast self)
1125 CODE:
1126 {
1127 struct sockaddr_in addr;
1128 socklen_t len;
1129 0 len = sizeof( addr );
1130 0 getsockname( self->ac->c.fd, ( struct sockaddr *)&addr, &len );
1131 0 ▶ RETVAL = addr.sin_port;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = addr.sin_port; 0 0 0
1132 }
1133 OUTPUT:
1134 RETVAL
1135  
1136  
1137 void
1138 __set_on_connect(Redis::Fast self, SV* func)
1139 CODE:
1140 {
1141 87 self->on_connect = SvREFCNT_inc(func);
1142 }
1143  
1144 void
1145 __set_on_build_sock(Redis::Fast self, SV* func)
1146 CODE:
1147 {
1148 0 self->on_build_sock = SvREFCNT_inc(func);
1149 }
1150  
1151 void
1152 __set_data(Redis::Fast self, SV* data)
1153 CODE:
1154 {
1155 87 self->data = SvREFCNT_inc(data);
1156 }
1157  
1158 void
1159 __get_data(Redis::Fast self)
1160 CODE:
1161 {
1162 362 ST(0) = self->data;
1163 362 XSRETURN(1);
1164 }
1165  
1166 void
1167 __set_reconnect_on_error(Redis::Fast self, SV* func)
1168 CODE:
1169 {
1170 0 self->reconnect_on_error = SvREFCNT_inc(func);
1171 }
1172  
1173 double
1174 __set_next_reconnect_on_error_at(Redis::Fast self, double val)
1175 CODE:
1176 {
1177 struct timeval current;
1178 double current_sec;
1179  
1180 0 ▶ if ( -1 < val ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( -1 < val ) { 0 0 0
1181 0 gettimeofday(&current, NULL);
1182 0 current_sec = current.tv_sec + 1E-6 * current.tv_usec;
1183 0 val += current_sec;
1184 }
1185  
1186 0 ▶ RETVAL = self->next_reconnect_on_error_at = val;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->next_reconnect_on_error_at = val; 0 0 0
1187 }
1188 OUTPUT:
1189 RETVAL
1190  
1191 int
1192 __set_ssl(Redis::Fast self, int ssl)
1193 CODE:
1194 {
1195 87 ▶ RETVAL = self->ssl = ssl;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->ssl = ssl; 68 19 87
1196 }
1197 OUTPUT:
1198 RETVAL
1199  
1200 int
1201 __set_ssl_verify_mode(Redis::Fast self, int verify_mode)
1202 CODE:
1203 {
1204 /* REDIS_SSL_VERIFY_* have the same values as OpenSSL's SSL_VERIFY_* (and IO::Socket::SSL's constants) */
1205 6 ▶ DEBUG_MSG("SSL verify mode: %d", verify_mode);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("SSL verify mode: %d", verify_mode); 0 6 6
1206 6 ▶ RETVAL = self->ssl_verify_mode = verify_mode;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = self->ssl_verify_mode = verify_mode; 3 3 6
1207 }
1208 OUTPUT:
1209 RETVAL
1210  
1211 void
1212 is_subscriber(Redis::Fast self)
1213 CODE:
1214 {
1215 164 ST(0) = sv_2mortal(newSViv(self->is_subscriber));
1216 164 XSRETURN(1);
1217 }
1218  
1219  
1220 void
1221 DESTROY(Redis::Fast self);
1222 CODE:
1223 {
1224 87 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 87 87
1225 87 ▶ if (self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->ac) { 69 18 87
1226 69 ▶ DEBUG_MSG("%s", "free ac");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free ac"); 0 69 69
1227 69 redisAsyncFree(self->ac);
1228 69 _wait_all_responses(aTHX_ self);
1229 69 self->ac = NULL;
1230 }
1231  
1232 87 ▶ if(self->hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->hostname) { 21 66 87
1233 21 ▶ DEBUG_MSG("%s", "free hostname");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free hostname"); 0 21 21
1234 21 free(self->hostname);
1235 21 self->hostname = NULL;
1236 }
1237  
1238 87 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 66 21 87
1239 66 ▶ DEBUG_MSG("%s", "free path");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free path"); 0 66 66
1240 66 free(self->path);
1241 66 self->path = NULL;
1242 }
1243  
1244 87 ▶ if(self->error) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->error) { 87 0 87
1245 87 ▶ DEBUG_MSG("%s", "free error");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free error"); 0 87 87
1246 87 free(self->error);
1247 87 self->error = NULL;
1248 }
1249  
1250 87 ▶ if(self->on_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->on_connect) { 87 0 87
1251 87 ▶ DEBUG_MSG("%s", "free on_connect");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free on_connect"); 0 87 87
1252 87 SvREFCNT_dec(self->on_connect);
1253 87 self->on_connect = NULL;
1254 }
1255  
1256 87 ▶ if(self->subscription_cb) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->subscription_cb) { 3 84 87
1257 3 ▶ DEBUG_MSG("%s", "free subscription_cb");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free subscription_cb"); 0 3 3
1258 3 SvREFCNT_dec(self->subscription_cb);
1259 3 self->subscription_cb = NULL;
1260 }
1261  
1262 87 ▶ if(self->on_build_sock) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->on_build_sock) { 0 87 87
1263 0 ▶ DEBUG_MSG("%s", "free on_build_sock");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free on_build_sock"); 0 0 0
1264 0 SvREFCNT_dec(self->on_build_sock);
1265 0 self->on_build_sock = NULL;
1266 }
1267  
1268 87 ▶ if(self->data) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->data) { 87 0 87
1269 87 ▶ DEBUG_MSG("%s", "free data");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free data"); 0 87 87
1270 87 SvREFCNT_dec(self->data);
1271 87 self->data = NULL;
1272 }
1273  
1274 87 ▶ if(self->reconnect_on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->reconnect_on_error) { 0 87 87
1275 0 ▶ DEBUG_MSG("%s", "free reconnect_on_error");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "free reconnect_on_error"); 0 0 0
1276 0 SvREFCNT_dec(self->reconnect_on_error);
1277 0 self->reconnect_on_error = NULL;
1278 }
1279  
1280 87 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 87 87
1281 87 Safefree(self);
1282 }
1283  
1284  
1285 void
1286 __connection_info(Redis::Fast self, char* hostname, int port = 6379)
1287 CODE:
1288 {
1289 21 ▶ if(self->hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->hostname) { 0 21 21
1290 0 free(self->hostname);
1291 0 self->hostname = NULL;
1292 }
1293  
1294 21 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 0 21 21
1295 0 free(self->path);
1296 0 self->path = NULL;
1297 }
1298  
1299 21 ▶ if(hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if(hostname) { 21 0 21
1300 21 self->hostname = (char*)malloc(strlen(hostname) + 1);
1301 21 strcpy(self->hostname, hostname);
1302 }
1303  
1304 21 self->port = port;
1305 }
1306  
1307 void
1308 __connection_info_unix(Redis::Fast self, char* path)
1309 CODE:
1310 {
1311 66 ▶ if(self->hostname) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->hostname) { 0 66 66
1312 0 free(self->hostname);
1313 0 self->hostname = NULL;
1314 }
1315  
1316 66 ▶ if(self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->path) { 0 66 66
1317 0 free(self->path);
1318 0 self->path = NULL;
1319 }
1320  
1321 66 ▶ if(path) {
Branch: 1 branch
BranchTrueFalseTotal
if(path) { 66 0 66
1322 66 self->path = (char*)malloc(strlen(path) + 1);
1323 66 strcpy(self->path, path);
1324 }
1325 }
1326  
1327  
1328 void
1329 connect(Redis::Fast self)
1330 CODE:
1331 {
1332 81 Redis__Fast_connect(aTHX_ self);
1333 }
1334  
1335 void
1336 wait_all_responses(Redis::Fast self)
1337 CODE:
1338 {
1339 4 int res = _wait_all_responses(aTHX_ self);
1340 4 ▶ if(res != WAIT_FOR_EVENT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != WAIT_FOR_EVENT_OK) { 0 4 4
1341 0 croak("Error while reading from Redis server");
1342 }
1343  
1344 4 ▶ if (0 < self->reconnect && self->need_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < self->reconnect && self->need_reconnect) { 1 3 4
if (0 < self->reconnect && self->need_reconnect) { 0 1 1
1345 // Should be quit before reconnect
1346 0 Redis__Fast_quit(aTHX_ self);
1347 0 Redis__Fast_reconnect(aTHX_ self);
1348 0 self->need_reconnect = 0;
1349 }
1350 }
1351  
1352 void
1353 wait_one_response(Redis::Fast self)
1354 CODE:
1355 {
1356 0 int res = _wait_all_responses(aTHX_ self);
1357 0 ▶ if(res != WAIT_FOR_EVENT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if(res != WAIT_FOR_EVENT_OK) { 0 0 0
1358 0 croak("Error while reading from Redis server");
1359 }
1360  
1361 0 ▶ if (0 < self->reconnect && self->need_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 < self->reconnect && self->need_reconnect) { 0 0 0
if (0 < self->reconnect && self->need_reconnect) { 0 0 0
1362 // Should be quit before reconnect
1363 0 Redis__Fast_quit(aTHX_ self);
1364 0 Redis__Fast_reconnect(aTHX_ self);
1365 0 self->need_reconnect = 0;
1366 }
1367 }
1368  
1369 void
1370 __std_cmd(Redis::Fast self, ...)
1371 PREINIT:
1372 redis_fast_reply_t ret;
1373 SV* cb;
1374 char** argv;
1375 size_t* argvlen;
1376 STRLEN len;
1377 int argc, i, collect_errors;
1378 CODE:
1379 {
1380 76 Redis__Fast_reconnect(aTHX_ self);
1381 76 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 76 76
1382 0 croak("Not connected to any server");
1383 }
1384  
1385 76 cb = ST(items - 1);
1386 76 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 1 75 76
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 1 0 1
1387 1 argc = items - 2;
1388 } else {
1389 75 cb = NULL;
1390 75 argc = items - 1;
1391 }
1392 76 ▶ Newx(argv, sizeof(char*) * argc, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, sizeof(char*) * argc, char*); 0 76 76
1393 76 ▶ Newx(argvlen, sizeof(size_t) * argc, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argvlen, sizeof(size_t) * argc, size_t); 0 76 76
1394  
1395 220 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 144 76 220
1396 144 ▶ if(!sv_utf8_downgrade(ST(i + 1), 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_utf8_downgrade(ST(i + 1), 1)) { 0 144 144
1397 0 croak("command sent is not an octet sequence in the native encoding (Latin-1). Consider using debug mode to see the command itself.");
1398 }
1399 144 argv[i] = SvPV(ST(i + 1), len);
1400 144 argvlen[i] = len;
1401 }
1402  
1403 76 collect_errors = 0;
1404 76 ▶ if(cb && EQUALS_COMMAND(argvlen[0], argv[0], "EXEC"))
Branch: 3 branches
BranchTrueFalseTotal
if(cb && EQUALS_COMMAND(argvlen[0], argv[0], "EXEC")) 1 75 76
if(cb && EQUALS_COMMAND(argvlen[0], argv[0], "EXEC")) 1 0 1
if(cb && EQUALS_COMMAND(argvlen[0], argv[0], "EXEC")) 1 0 1
1405 1 collect_errors = 1;
1406  
1407 76 ret = Redis__Fast_run_cmd(aTHX_ self, collect_errors, NULL, cb, argc, (const char**)argv, argvlen);
1408  
1409 76 Safefree(argv);
1410 76 Safefree(argvlen);
1411  
1412 76 ▶ ST(0) = ret.result ? ret.result : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = ret.result ? ret.result : &PL_sv_undef; 59 17 76
1413 76 ▶ ST(1) = ret.error ? ret.error : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(1) = ret.error ? ret.error : &PL_sv_undef; 19 57 76
1414 76 XSRETURN(2);
1415 }
1416  
1417  
1418 void
1419 __quit(Redis::Fast self)
1420 CODE:
1421 {
1422 0 ▶ DEBUG_MSG("%s", "start QUIT");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start QUIT"); 0 0 0
1423 0 ▶ if(self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->ac) { 0 0 0
1424 0 Redis__Fast_quit(aTHX_ self);
1425 0 ST(0) = sv_2mortal(newSViv(1));
1426 0 XSRETURN(1);
1427 } else {
1428 0 ▶ DEBUG_MSG("%s", "finish. there is no connection.");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish. there is no connection."); 0 0 0
1429 0 XSRETURN(0);
1430 }
1431 }
1432  
1433  
1434 void
1435 __shutdown(Redis::Fast self)
1436 CODE:
1437 {
1438 0 ▶ DEBUG_MSG("%s", "start SHUTDOWN");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start SHUTDOWN"); 0 0 0
1439 0 ▶ if(self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(self->ac) { 0 0 0
1440 0 redisAsyncCommand(
1441 self->ac, NULL, NULL, "SHUTDOWN"
1442 );
1443 0 redisAsyncDisconnect(self->ac);
1444 0 _wait_all_responses(aTHX_ self);
1445 0 self->is_connected = 0;
1446 0 ST(0) = sv_2mortal(newSViv(1));
1447 0 XSRETURN(1);
1448 } else {
1449 0 ▶ DEBUG_MSG("%s", "redis server has alread shutdown");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "redis server has alread shutdown"); 0 0 0
1450 0 XSRETURN(0);
1451 }
1452 }
1453  
1454  
1455 void
1456 __keys(Redis::Fast self, ...)
1457 PREINIT:
1458 redis_fast_reply_t ret;
1459 SV* cb;
1460 char** argv;
1461 size_t* argvlen;
1462 STRLEN len;
1463 int argc, i;
1464 CODE:
1465 {
1466 0 Redis__Fast_reconnect(aTHX_ self);
1467 0 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 0 0
1468 0 croak("Not connected to any server");
1469 }
1470  
1471 0 cb = ST(items - 1);
1472 0 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 0 0 0
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 0 0 0
1473 0 argc = items - 1;
1474 } else {
1475 0 cb = NULL;
1476 0 argc = items;
1477 }
1478 0 ▶ Newx(argv, sizeof(char*) * argc, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, sizeof(char*) * argc, char*); 0 0 0
1479 0 ▶ Newx(argvlen, sizeof(size_t) * argc, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argvlen, sizeof(size_t) * argc, size_t); 0 0 0
1480  
1481 0 argv[0] = "KEYS";
1482 0 argvlen[0] = 4;
1483 0 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 0 0 0
1484 0 argv[i] = SvPV(ST(i), len);
1485 0 argvlen[i] = len;
1486 }
1487  
1488 0 ret = Redis__Fast_run_cmd(aTHX_ self, 0, Redis__Fast_keys_custom_decode, cb, argc, (const char**)argv, argvlen);
1489 0 Safefree(argv);
1490 0 Safefree(argvlen);
1491  
1492 0 ▶ ST(0) = ret.result ? ret.result : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = ret.result ? ret.result : &PL_sv_undef; 0 0 0
1493 0 ▶ ST(1) = ret.error ? ret.error : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(1) = ret.error ? ret.error : &PL_sv_undef; 0 0 0
1494 0 XSRETURN(2);
1495 }
1496  
1497  
1498 void
1499 __info(Redis::Fast self, ...)
1500 PREINIT:
1501 redis_fast_reply_t ret;
1502 SV* cb;
1503 char** argv;
1504 size_t* argvlen;
1505 STRLEN len;
1506 int argc, i;
1507 CODE:
1508 {
1509 8 Redis__Fast_reconnect(aTHX_ self);
1510 8 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 3 5 8
1511 3 char *msg = "Not connected to any server";
1512 3 ST(0) = &PL_sv_undef;
1513 3 ST(1) = sv_2mortal(newSVpvn(msg, strlen(msg)));
1514 3 XSRETURN(2);
1515 }
1516  
1517 5 cb = ST(items - 1);
1518 5 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 5 0 5
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 0 5 5
1519 0 argc = items - 1;
1520 } else {
1521 5 cb = NULL;
1522 5 argc = items;
1523 }
1524 5 ▶ Newx(argv, sizeof(char*) * argc, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, sizeof(char*) * argc, char*); 0 5 5
1525 5 ▶ Newx(argvlen, sizeof(size_t) * argc, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argvlen, sizeof(size_t) * argc, size_t); 0 5 5
1526  
1527 5 argv[0] = "INFO";
1528 5 argvlen[0] = 4;
1529 5 ▶ for (i = 1; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < argc; i++) { 0 5 5
1530 0 argv[i] = SvPV(ST(i), len);
1531 0 argvlen[i] = len;
1532 }
1533  
1534 5 ret = Redis__Fast_run_cmd(aTHX_ self, 0, Redis__Fast_info_custom_decode, cb, argc, (const char**)argv, argvlen);
1535 5 Safefree(argv);
1536 5 Safefree(argvlen);
1537  
1538 5 ▶ ST(0) = ret.result ? ret.result : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = ret.result ? ret.result : &PL_sv_undef; 5 0 5
1539 5 ▶ ST(1) = ret.error ? ret.error : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
ST(1) = ret.error ? ret.error : &PL_sv_undef; 0 5 5
1540 5 XSRETURN(2);
1541 }
1542  
1543  
1544 void
1545 __send_subscription_cmd(Redis::Fast self, ...)
1546 PREINIT:
1547 SV* cb;
1548 char** argv;
1549 size_t* argvlen;
1550 STRLEN len;
1551 int argc, i;
1552 int pvariant, is_unsubscribe;
1553 CODE:
1554 {
1555 3 ▶ int cnt = (self->reconnect == 0 ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
int cnt = (self->reconnect == 0 ? 1 : 2); 3 0 3
1556  
1557 3 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 3 3
1558  
1559 3 Redis__Fast_reconnect(aTHX_ self);
1560 3 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 3 3
1561 0 croak("Not connected to any server");
1562 }
1563  
1564 3 ▶ if(!self->is_subscriber) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->is_subscriber) { 3 0 3
1565 3 _wait_all_responses(aTHX_ self);
1566 }
1567 3 cb = ST(items - 1);
1568 3 ▶ if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 3 0 3
if (SvROK(cb) && SvTYPE(SvRV(cb)) == SVt_PVCV) { 3 0 3
1569 3 argc = items - 2;
1570 } else {
1571 0 cb = NULL;
1572 0 argc = items - 1;
1573 }
1574 3 ▶ Newx(argv, sizeof(char*) * argc, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argv, sizeof(char*) * argc, char*); 0 3 3
1575 3 ▶ Newx(argvlen, sizeof(size_t) * argc, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newx(argvlen, sizeof(size_t) * argc, size_t); 0 3 3
1576  
1577 9 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 6 3 9
1578 6 argv[i] = SvPV(ST(i+1), len);
1579 6 argvlen[i] = len;
1580 6 ▶ DEBUG_MSG("argv[%d] = %s", i, argv[i]);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("argv[%d] = %s", i, argv[i]); 0 6 6
1581 }
1582  
1583 3 pvariant = tolower(argv[0][0]) == 'p';
1584 3 is_unsubscribe = strcasecmp(argv[0]+pvariant,"unsubscribe") == 0;
1585 3 ▶ if (!is_unsubscribe && cb) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_unsubscribe && cb) { 3 0 3
if (!is_unsubscribe && cb) { 3 0 3
1586 3 SV* old = self->subscription_cb;
1587 3 self->subscription_cb = SvREFCNT_inc(cb);
1588 3 ▶ if (old) SvREFCNT_dec(old);
Branch: 1 branch
BranchTrueFalseTotal
if (old) SvREFCNT_dec(old); 0 3 3
1589 }
1590  
1591 4 ▶ for(i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < cnt; i++) { 3 1 4
1592 3 ▶ redisAsyncCommandArgv(
Branch: 1 branch
BranchTrueFalseTotal
redisAsyncCommandArgv( 3 0 3
1593 self->ac, is_unsubscribe ? NULL : Redis__Fast_subscribe_cb, NULL,
1594 argc, (const char**)argv, argvlen
1595 );
1596 3 self->expected_subs = argc - 1;
1597 9 ▶ while(self->expected_subs > 0 && wait_for_event(aTHX_ self, self->read_timeout, self->write_timeout) == WAIT_FOR_EVENT_OK) ;
Branch: 2 branches
BranchTrueFalseTotal
while(self->expected_subs > 0 && wait_for_event(aTHX_ self, self->read_timeout, self->write_timeout) == WAIT_FOR_EVENT_OK) ; 6 3 9
while(self->expected_subs > 0 && wait_for_event(aTHX_ self, self->read_timeout, self->write_timeout) == WAIT_FOR_EVENT_OK) ; 6 0 6
1598 3 ▶ if(self->expected_subs == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if(self->expected_subs == 0) break; 2 1 3
1599 1 Redis__Fast_reconnect(aTHX_ self);
1600 1 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 1 1
1601 0 Safefree(argv);
1602 0 Safefree(argvlen);
1603 0 croak("Not connected to any server");
1604 }
1605 }
1606  
1607 3 Safefree(argv);
1608 3 Safefree(argvlen);
1609 3 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 3 3
1610 3 XSRETURN(0);
1611 }
1612  
1613 void
1614 wait_for_messages(Redis::Fast self, double timeout = -1)
1615 CODE:
1616 {
1617 0 ▶ int i, cnt = (self->reconnect == 0 ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
int i, cnt = (self->reconnect == 0 ? 1 : 2); 0 0 0
1618 0 int res = WAIT_FOR_EVENT_OK;
1619 0 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 0 0
1620 0 self->proccess_sub_count = 0;
1621 0 ▶ for(i = 0; i < cnt; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < cnt; i++) { 0 0 0
1622 0 ▶ while((res = wait_for_event(aTHX_ self, timeout, timeout)) == WAIT_FOR_EVENT_OK) ;
Branch: 1 branch
BranchTrueFalseTotal
while((res = wait_for_event(aTHX_ self, timeout, timeout)) == WAIT_FOR_EVENT_OK) ; 0 0 0
1623 0 ▶ if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) break;
Branch: 2 branches
BranchTrueFalseTotal
if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) break; 0 0 0
if(res == WAIT_FOR_EVENT_READ_TIMEOUT || res == WAIT_FOR_EVENT_WRITE_TIMEOUT) break; 0 0 0
1624 0 Redis__Fast_reconnect(aTHX_ self);
1625 0 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 0 0
1626 0 croak("Not connected to any server");
1627 }
1628 }
1629 0 ▶ if(res == WAIT_FOR_EVENT_EXCEPTION) {
Branch: 1 branch
BranchTrueFalseTotal
if(res == WAIT_FOR_EVENT_EXCEPTION) { 0 0 0
1630 0 ▶ if(!self->ac) {
Branch: 1 branch
BranchTrueFalseTotal
if(!self->ac) { 0 0 0
1631 0 ▶ DEBUG_MSG("%s", "Connection not found");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "Connection not found"); 0 0 0
1632 0 croak("EOF from server");
1633 0 ▶ } else if(self->ac->c.err == REDIS_ERR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(self->ac->c.err == REDIS_ERR_EOF) { 0 0 0
1634 0 ▶ DEBUG_MSG("hiredis returns error: %s", self->ac->c.errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("hiredis returns error: %s", self->ac->c.errstr); 0 0 0
1635 0 croak("EOF from server");
1636 } else {
1637 0 ▶ DEBUG_MSG("hiredis returns error: %s", self->ac->c.errstr);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("hiredis returns error: %s", self->ac->c.errstr); 0 0 0
1638 0 snprintf(self->error, MAX_ERROR_SIZE, "[WAIT_FOR_MESSAGES] %s", self->ac->c.errstr);
1639 0 croak("%s", self->error);
1640 }
1641 }
1642 0 ST(0) = sv_2mortal(newSViv(self->proccess_sub_count));
1643 0 ▶ DEBUG_MSG("finish with %d", res);
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("finish with %d", res); 0 0 0
1644 0 XSRETURN(1);
1645 }
1646  
1647 void
1648 __wait_for_event(Redis::Fast self, double timeout = -1)
1649 CODE:
1650 {
1651 0 ▶ DEBUG_MSG("%s", "start");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "start"); 0 0 0
1652 0 wait_for_event(aTHX_ self, timeout, timeout);
1653 0 ▶ DEBUG_MSG("%s", "finish");
Branch: 1 branch
BranchTrueFalseTotal
DEBUG_MSG("%s", "finish"); 0 0 0
1654 0 XSRETURN(0);
1655 }
« blib/lib/Redis/Fast/List.pm ↑ Summary