Skip to content

Commit 978c307

Browse files
committed
Refactor redis_session
Use `redis_sock` to store `auth` and `prefix` for session. Use `redis_sock_auth` insted of `redis_pool_member_auth`.
1 parent 4c61678 commit 978c307

File tree

1 file changed

+22
-50
lines changed

1 file changed

+22
-50
lines changed

redis_session.c

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ typedef struct redis_pool_member_ {
7171
RedisSock *redis_sock;
7272
int weight;
7373
int database;
74-
zend_string *prefix;
75-
zend_string *auth;
7674
struct redis_pool_member_ *next;
7775

7876
} redis_pool_member;
@@ -88,17 +86,13 @@ typedef struct {
8886
} redis_pool;
8987

9088
PHP_REDIS_API void
91-
redis_pool_add(redis_pool *pool, RedisSock *redis_sock, int weight,
92-
int database, zend_string *prefix, zend_string *auth) {
93-
89+
redis_pool_add(redis_pool *pool, RedisSock *redis_sock, int weight, int database)
90+
{
9491
redis_pool_member *rpm = ecalloc(1, sizeof(redis_pool_member));
9592
rpm->redis_sock = redis_sock;
9693
rpm->weight = weight;
9794
rpm->database = database;
9895

99-
rpm->prefix = prefix;
100-
rpm->auth = auth;
101-
10296
rpm->next = pool->head;
10397
pool->head = rpm;
10498

@@ -114,8 +108,6 @@ redis_pool_free(redis_pool *pool) {
114108
next = rpm->next;
115109
redis_sock_disconnect(rpm->redis_sock, 0);
116110
redis_free_socket(rpm->redis_sock);
117-
if (rpm->prefix) zend_string_release(rpm->prefix);
118-
if (rpm->auth) zend_string_release(rpm->auth);
119111
efree(rpm);
120112
rpm = next;
121113
}
@@ -143,26 +135,6 @@ static int redis_simple_cmd(RedisSock *redis_sock, char *cmd, int cmdlen,
143135
return len_written;
144136
}
145137

146-
static void
147-
redis_pool_member_auth(redis_pool_member *rpm) {
148-
RedisSock *redis_sock = rpm->redis_sock;
149-
char *response, *cmd;
150-
int response_len, cmd_len;
151-
152-
/* Short circuit if we don't have a password */
153-
if (!rpm->auth) {
154-
return;
155-
}
156-
157-
cmd_len = REDIS_SPPRINTF(&cmd, "AUTH", "S", rpm->auth);
158-
if (redis_sock_write(redis_sock, cmd, cmd_len) >= 0) {
159-
if ((response = redis_sock_read(redis_sock, &response_len))) {
160-
efree(response);
161-
}
162-
}
163-
efree(cmd);
164-
}
165-
166138
static void
167139
redis_pool_member_select(redis_pool_member *rpm) {
168140
RedisSock *redis_sock = rpm->redis_sock;
@@ -190,12 +162,12 @@ redis_pool_get_sock(redis_pool *pool, const char *key) {
190162
for(i = 0; i < pool->totalWeight;) {
191163
if (pos >= i && pos < i + rpm->weight) {
192164
int needs_auth = 0;
193-
if (rpm->auth && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
165+
if (rpm->redis_sock->auth && rpm->redis_sock->status != REDIS_SOCK_STATUS_CONNECTED) {
194166
needs_auth = 1;
195167
}
196168
if (redis_sock_server_open(rpm->redis_sock) == 0) {
197169
if (needs_auth) {
198-
redis_pool_member_auth(rpm);
170+
redis_sock_auth(rpm->redis_sock);
199171
}
200172
if (rpm->database >= 0) { /* default is -1 which leaves the choice to redis. */
201173
redis_pool_member_select(rpm);
@@ -514,7 +486,9 @@ PS_OPEN_FUNC(redis)
514486
redis_sock = redis_sock_create(ZSTR_VAL(url->path), ZSTR_LEN(url->path), 0, timeout, read_timeout, persistent, persistent_id, retry_interval);
515487
#endif
516488
}
517-
redis_pool_add(pool, redis_sock, weight, database, prefix, auth);
489+
redis_pool_add(pool, redis_sock, weight, database);
490+
redis_sock->prefix = prefix;
491+
redis_sock->auth = auth;
518492

519493
php_url_free(url);
520494
}
@@ -554,16 +528,16 @@ PS_CLOSE_FUNC(redis)
554528
/* }}} */
555529

556530
static zend_string *
557-
redis_session_key(redis_pool_member *rpm, const char *key, int key_len)
531+
redis_session_key(RedisSock *redis_sock, const char *key, int key_len)
558532
{
559533
zend_string *session;
560534
char default_prefix[] = "PHPREDIS_SESSION:";
561535
char *prefix = default_prefix;
562536
size_t prefix_len = sizeof(default_prefix)-1;
563537

564-
if (rpm->prefix) {
565-
prefix = ZSTR_VAL(rpm->prefix);
566-
prefix_len = ZSTR_LEN(rpm->prefix);
538+
if (redis_sock->prefix) {
539+
prefix = ZSTR_VAL(redis_sock->prefix);
540+
prefix_len = ZSTR_LEN(redis_sock->prefix);
567541
}
568542

569543
/* build session key */
@@ -589,9 +563,9 @@ PS_CREATE_SID_FUNC(redis)
589563
zend_string* sid = php_session_create_id((void **) &pool);
590564
redis_pool_member *rpm = redis_pool_get_sock(pool, ZSTR_VAL(sid));
591565

592-
RedisSock *redis_sock = rpm?rpm->redis_sock:NULL;
566+
RedisSock *redis_sock = rpm ? rpm->redis_sock : NULL;
593567

594-
if (!rpm || !redis_sock) {
568+
if (!redis_sock) {
595569
php_error_docref(NULL, E_NOTICE,
596570
"Redis not available while creating session_id");
597571

@@ -600,7 +574,7 @@ PS_CREATE_SID_FUNC(redis)
600574
}
601575

602576
if (pool->lock_status.session_key) zend_string_release(pool->lock_status.session_key);
603-
pool->lock_status.session_key = redis_session_key(rpm, ZSTR_VAL(sid), ZSTR_LEN(sid));
577+
pool->lock_status.session_key = redis_session_key(redis_sock, ZSTR_VAL(sid), ZSTR_LEN(sid));
604578

605579
if (lock_acquire(redis_sock, &pool->lock_status) == SUCCESS) {
606580
return sid;
@@ -639,7 +613,7 @@ PS_VALIDATE_SID_FUNC(redis)
639613
}
640614

641615
/* send EXISTS command */
642-
zend_string *session = redis_session_key(rpm, skey, skeylen);
616+
zend_string *session = redis_session_key(redis_sock, skey, skeylen);
643617
cmd_len = REDIS_SPPRINTF(&cmd, "EXISTS", "S", session);
644618
zend_string_release(session);
645619
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {
@@ -683,7 +657,7 @@ PS_UPDATE_TIMESTAMP_FUNC(redis)
683657
}
684658

685659
/* send EXPIRE command */
686-
zend_string *session = redis_session_key(rpm, skey, skeylen);
660+
zend_string *session = redis_session_key(redis_sock, skey, skeylen);
687661
cmd_len = REDIS_SPPRINTF(&cmd, "EXPIRE", "Sd", session, INI_INT("session.gc_maxlifetime"));
688662
zend_string_release(session);
689663

@@ -721,14 +695,14 @@ PS_READ_FUNC(redis)
721695

722696
redis_pool *pool = PS_GET_MOD_DATA();
723697
redis_pool_member *rpm = redis_pool_get_sock(pool, skey);
724-
RedisSock *redis_sock = rpm?rpm->redis_sock:NULL;
725-
if (!rpm || !redis_sock){
698+
RedisSock *redis_sock = rpm ? rpm->redis_sock : NULL;
699+
if (!redis_sock) {
726700
return FAILURE;
727701
}
728702

729703
/* send GET command */
730704
if (pool->lock_status.session_key) zend_string_release(pool->lock_status.session_key);
731-
pool->lock_status.session_key = redis_session_key(rpm, skey, skeylen);
705+
pool->lock_status.session_key = redis_session_key(redis_sock, skey, skeylen);
732706
cmd_len = REDIS_SPPRINTF(&cmd, "GET", "S", pool->lock_status.session_key);
733707

734708
if (lock_acquire(redis_sock, &pool->lock_status) != SUCCESS) {
@@ -779,7 +753,7 @@ PS_WRITE_FUNC(redis)
779753
}
780754

781755
/* send SET command */
782-
zend_string *session = redis_session_key(rpm, skey, skeylen);
756+
zend_string *session = redis_session_key(redis_sock, skey, skeylen);
783757

784758
cmd_len = REDIS_SPPRINTF(&cmd, "SETEX", "Sds", session, INI_INT("session.gc_maxlifetime"), sval, svallen);
785759
zend_string_release(session);
@@ -822,12 +796,10 @@ PS_DESTROY_FUNC(redis)
822796
}
823797

824798
/* Release lock */
825-
if (redis_sock) {
826-
lock_release(redis_sock, &pool->lock_status);
827-
}
799+
lock_release(redis_sock, &pool->lock_status);
828800

829801
/* send DEL command */
830-
zend_string *session = redis_session_key(rpm, skey, skeylen);
802+
zend_string *session = redis_session_key(redis_sock, skey, skeylen);
831803
cmd_len = REDIS_SPPRINTF(&cmd, "DEL", "S", session);
832804
zend_string_release(session);
833805
if (redis_sock_write(redis_sock, cmd, cmd_len) < 0) {

0 commit comments

Comments
 (0)