8000 Use zend_hrtime instead of time(NULL) since it's monotonic · phpredis/phpredis@dc6e530 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc6e530

Browse files
Use zend_hrtime instead of time(NULL) since it's monotonic
`time(NULL)` isn't monotonic and can run backwards so switch to `zend_hrtime` which uses `clock_gettime(CLOCK_MONOTONIC)` on Linux and `QueryPerformanceCounter`on Windows.
1 parent fead6c7 commit dc6e530

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cluster_library.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,14 +891,18 @@ cluster_free(redisCluster *c, int free_ctx)
891891
if (free_ctx) efree(c) 8000 ;
892892
}
893893

894+
static inline uint64_t redis_time(void) {
895+
return zend_hrtime() / ZEND_NANO_IN_SEC;
896+
}
897+
894898
static zend_long cluster_cache_expiry(void) {
895899
zend_long expiry;
896900

897901
expiry = INI_INT("redis.clusters.slot_cache_expiry");
898902
if (expiry <= 0)
899903
return 0;
900904

901-
return time(NULL) + expiry;
905+
return redis_time() + expiry;
902906
}
903907

904908
#ifdef HAVE_REDIS_ATOMICS_MMAP
@@ -3172,7 +3176,7 @@ PHP_REDIS_API redisCachedCluster *cluster_cache_load(zend_string *hash) {
31723176

31733177
cc = le->ptr;
31743178
/* Short circuit if it should be expired */
3175-
if (cc->expiry != 5004 0 && cc->expiry <= time(NULL))
3179+
if (cc->expiry != 0 && cc->expiry <= redis_time())
31763180
return NULL;
31773181

31783182
#ifdef HAVE_REDIS_ATOMICS_MMAP

0 commit comments

Comments
 (0)
0