Skip to content

Commit f771ea1

Browse files
authored
1 parent bee44fb commit f771ea1

File tree

4 files changed

+52
-44
lines changed

4 files changed

+52
-44
lines changed

cluster_library.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,12 @@ cluster_node_create(redisCluster *c, char *host, size_t host_len,
634634
zend_llist_init(&node->slots, sizeof(redisSlotRange), NULL, 0);
635635

636636
// Attach socket
637-
node->sock = redis_sock_create(host, host_len, port, c->timeout,
638-
c->read_timeout, c->persistent, NULL, 0);
637+
node->sock = redis_sock_create(host, host_len, port,
638+
c->flags->timeout, c->flags->read_timeout,
639+
c->flags->persistent, NULL, 0);
640+
641+
/* Stream context */
642+
node->sock->stream_ctx = c->flags->stream_ctx;
639643

640644
redis_sock_set_auth(node->sock, c->flags->user, c->flags->pass);
641645

@@ -818,12 +822,12 @@ PHP_REDIS_API redisCluster *cluster_create(double timeout, double read_timeout,
818822

819823
/* Initialize flags and settings */
820824
c->flags = ecalloc(1, sizeof(RedisSock));
825+
c->flags->timeout = timeout;
826+
c->flags->read_timeout = read_timeout;
827+
c->flags->persistent = persistent;
821828
c->subscribed_slot = -1;
822829
c->clusterdown = 0;
823-
c->timeout = timeout;
824-
c->read_timeout = read_timeout;
825830
c->failover = failover;
826-
c->persistent = persistent;
827831
c->err = NULL;
828832

829833
/* Set up our waitms based on timeout */
@@ -993,9 +997,12 @@ void cluster_init_cache(redisCluster *c, redisCachedCluster *cc) {
993997

994998
/* Create socket */
995999
sock = redis_sock_create(ZSTR_VAL(cm->host.addr), ZSTR_LEN(cm->host.addr), cm->host.port,
996-
c->timeout, c->read_timeout, c->persistent,
1000+
c->flags->timeout, c->flags->read_timeout, c->flags->persistent,
9971001
NULL, 0);
9981002

1003+
/* Stream context */
1004+
sock->stream_ctx = c->flags->stream_ctx;
1005+
9991006
/* Add to seed nodes */
10001007
zend_hash_str_update_ptr(c->seeds, key, keylen, sock);
10011008

@@ -1027,7 +1034,8 @@ void cluster_init_cache(redisCluster *c, redisCachedCluster *cc) {
10271034
* seeds array and know we have a non-empty array of strings all in
10281035
* host:port format. */
10291036
PHP_REDIS_API void
1030-
cluster_init_seeds(redisCluster *cluster, zend_string **seeds, uint32_t nseeds) {
1037+
cluster_init_seeds(redisCluster *c, zend_string **seeds, uint32_t nseeds)
1038+
{
10311039
RedisSock *sock;
10321040
char *seed, *sep, key[1024];
10331041
int key_len, i, *map;
@@ -1044,19 +1052,22 @@ cluster_init_seeds(redisCluster *cluster, zend_string **seeds, uint32_t nseeds)
10441052
ZEND_ASSERT(sep != NULL);
10451053

10461054
// Allocate a structure for this seed
1047-
sock = redis_sock_create(seed, sep - seed,
1048-
(unsigned short)atoi(sep+1), cluster->timeout,
1049-
cluster->read_timeout, cluster->persistent, NULL, 0);
1055+
sock = redis_sock_create(seed, sep - seed, atoi(sep + 1),
1056+
c->flags->timeout, c->flags->read_timeout,
1057+
c->flags->persistent, NULL, 0);
1058+
1059+
/* Stream context */
1060+
sock->stream_ctx = c->flags->stream_ctx;
10501061

10511062
/* Credentials */
1052-
redis_sock_set_auth(sock, cluster->flags->user, cluster->flags->pass);
1063+
redis_sock_set_auth(sock, c->flags->user, c->flags->pass);
10531064

10541065
// Index this seed by host/port
10551066
key_len = snprintf(key, sizeof(key), "%s:%u", ZSTR_VAL(sock->host),
10561067
sock->port);
10571068

10581069
// Add to our seed HashTable
1059-
zend_hash_str_update_ptr(cluster->seeds, key, key_len, sock);
1070+
zend_hash_str_update_ptr(c->seeds, key, key_len, sock);
10601071
}
10611072

10621073
efree(map);

cluster_library.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,8 @@ typedef struct clusterFoldItem clusterFoldItem;
186186
/* RedisCluster implementation structure */
187187
typedef struct redisCluster {
188188

189-
/* Timeout and read timeout (for normal operations) */
190-
double timeout;
191-
double read_timeout;
192-
193-
/* Are we using persistent connections */
194-
int persistent;
189+
/* One RedisSock struct for serialization and prefix information */
190+
RedisSock *flags;
195191

196192
/* How long in milliseconds should we wait when being bounced around */
197193
long waitms;
@@ -241,9 +237,6 @@ typedef struct redisCluster {
241237
/* The slot where we're subscribed */
242238
short subscribed_slot;
243239

244-
/* One RedisSock struct for serialization and prefix information */
245-
RedisSock *flags;
246-
247240
/* The first line of our last reply, not including our reply type byte
248241
* or the trailing \r\n */
249242
char line_reply[1024];

library.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,19 +2165,22 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
21652165
{
21662166
struct timeval tv, read_tv, *tv_ptr = NULL;
21672167
zend_string *persistent_id = NULL, *estr = NULL;
2168-
char host[1024], *pos, *address, *schema = NULL;
2168+
char host[1024], *pos, *address, *scheme = NULL;
21692169
const char *fmtstr = "%s://%s:%d";
2170-
int host_len, usocket = 0, err = 0, tcp_flag = 1;
2170+
int host_len, usocket = 0, err = 0, tcp_flag = 1, scheme_free = 0;
21712171
ConnectionPool *p = NULL;
21722172

21732173
if (redis_sock->stream != NULL) {
21742174
redis_sock_disconnect(redis_sock, 0);
21752175
}
21762176

21772177
address = ZSTR_VAL(redis_sock->host);
2178-
if ((pos = strstr(address, "://")) != NULL) {
2179-
schema = estrndup(address, pos - address);
2178+
if ((pos = strstr(address, "://")) == NULL) {
2179+
scheme = redis_sock->stream_ctx ? "ssl" : "tcp";
2180+
} else {
2181+
scheme = estrndup(address, pos - address);
21802182
address = pos + sizeof("://") - 1;
2183+
scheme_free = 1;
21812184
}
21822185
if (address[0] == '/' && redis_sock->port < 1) {
21832186
host_len = snprintf(host, sizeof(host), "unix://%s", address);
@@ -2193,9 +2196,9 @@ PHP_REDIS_API int redis_sock_connect(RedisSock *redis_sock)
21932196
fmtstr = "%s://[%s]:%d";
21942197
}
21952198
#endif
2196-
host_len = snprintf(host, sizeof(host), fmtstr, schema ? schema : "tcp", address, redis_sock->port);
2197-
if (schema) efree(schema);
2199+
host_len = snprintf(host, sizeof(host), fmtstr, scheme, address, redis_sock->port);
21982200
}
2201+
if (scheme_free) efree(scheme);
21992202

22002203
if (redis_sock->persistent) {
22012204
if (INI_INT("redis.pconnect.pooling_enabled")) {

redis_cluster.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ void free_cluster_context(zend_object *object) {
351351
/* Attempt to connect to a Redis cluster provided seeds and timeout options */
352352
static void redis_cluster_init(redisCluster *c, HashTable *ht_seeds, double timeout,
353353
double read_timeout, int persistent, zend_string *user,
354-
zend_string *pass)
354+
zend_string *pass, zval *context)
355355
{
356356
zend_string *hash = NULL, **seeds;
357357
redisCachedCluster *cc;
@@ -369,10 +369,13 @@ static void redis_cluster_init(redisCluster *c, HashTable *ht_seeds, double time
369369
c->flags->user = zend_string_copy(user);
370370
if (pass && ZSTR_LEN(pass))
371371
c->flags->pass = zend_string_copy(pass);
372+
if (context) {
373+
redis_sock_set_stream_context(c->flags, context);
374+
}
372375

373-
c->timeout = timeout;
374-
c->read_timeout = read_timeout;
375-
c->persistent = persistent;
376+
c->flags->timeout = timeout;
377+
c->flags->read_timeout = read_timeout;
378+
c->flags->persistent = persistent;
376379
c->waitms = timeout * 1000L;
377380

378381
/* Attempt to load slots from cache if caching is enabled */
@@ -450,7 +453,7 @@ void redis_cluster_load(redisCluster *c, char *name, int name_len) {
450453
}
451454

452455
/* Attempt to create/connect to the cluster */
453-
redis_cluster_init(c, ht_seeds, timeout, read_timeout, persistent, user, pass);
456+
redis_cluster_init(c, ht_seeds, timeout, read_timeout, persistent, user, pass, NULL);
454457

455458
/* Clean up */
456459
zval_dtor(&z_seeds);
@@ -464,38 +467,36 @@ void redis_cluster_load(redisCluster *c, char *name, int name_len) {
464467

465468
/* Create a RedisCluster Object */
466469
PHP_METHOD(RedisCluster, __construct) {
467-
zval *object, *z_seeds = NULL, *z_auth = NULL;
470+
zval *object, *z_seeds = NULL, *z_auth = NULL, *context = NULL;
468471
zend_string *user = NULL, *pass = NULL;
469472
double timeout = 0.0, read_timeout = 0.0;
470473
size_t name_len;
471474
zend_bool persistent = 0;
472-
redisCluster *context = GET_CONTEXT();
475+
redisCluster *c = GET_CONTEXT();
473476
char *name;
474477

475478
// Parse arguments
476479
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
477-
"Os!|addbz", &object, redis_cluster_ce, &name,
480+
"Os!|addbza", &object, redis_cluster_ce, &name,
478481
&name_len, &z_seeds, &timeout, &read_timeout,
479-
&persistent, &z_auth) == FAILURE)
482+
&persistent, &z_auth, &context) == FAILURE)
480483
{
481484
RETURN_FALSE;
482485
}
483486

484-
// Require a name
485-
if (name_len == 0 && ZEND_NUM_ARGS() < 2) {
486-
CLUSTER_THROW_EXCEPTION("You must specify a name or pass seeds!", 0);
487-
}
488-
489487
/* If we've got a string try to load from INI */
490488
if (ZEND_NUM_ARGS() < 2) {
491-
redis_cluster_load(context, name, name_len);
489+
if (name_len == 0) { // Require a name
490+
CLUSTER_THROW_EXCEPTION("You must specify a name or pass seeds!", 0);
491+
}
492+
redis_cluster_load(c, name, name_len);
492493
return;
493494
}
494495

495496
/* The normal case, loading from arguments */
496497
redis_extract_auth_info(z_auth, &user, &pass);
497-
redis_cluster_init(context, Z_ARRVAL_P(z_seeds), timeout, read_timeout,
498-
persistent, user, pass);
498+
redis_cluster_init(c, Z_ARRVAL_P(z_seeds), timeout, read_timeout,
499+
persistent, user, pass, context);
499500

500501
if (user) zend_string_release(user);
501502
if (pass) zend_string_release(pass);

0 commit comments

Comments
 (0)