Skip to content

Commit 5f0cff8

Browse files
author
Oliver Stark
committed
Use proper redundancy options to make a failover seamless
1 parent 2ada3b7 commit 5f0cff8

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fortrabbit/yii-memcached",
33
"description": "Memcached Session & Cache for Craft3 / Yii2 on fortrabbit",
4-
"version": "1.0.0",
4+
"version": "1.1.0",
55
"type": "yii2-extension",
66

77
"keywords": ["yii2", "craftcms", "caching", "cache", "memcached", "memcache", "session"],

src/Bootstrap.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
class Bootstrap implements BootstrapInterface
1616
{
1717

18+
const TIMEOUT_IN_MILLISECONDS = 50;
19+
1820
/**
1921
* Bootstrapper
2022
*
@@ -31,6 +33,7 @@ public function bootstrap($app)
3133
'class' => MemCache::class,
3234
'persistentId' => getenv('MEMCACHE_PERSISTENT') ? 'cache' : null,
3335
'servers' => $this->getMemcachedServers(),
36+
'options' => $this->getOptions(),
3437
'useMemcached' => true
3538
]);
3639
} catch (InvalidConfigException $e) {
@@ -51,13 +54,46 @@ protected function getMemcachedServers(): array
5154
$servers[] = [
5255
'host' => getenv('MEMCACHE_HOST' . $num),
5356
'port' => getenv('MEMCACHE_PORT' . $num),
54-
'retryInterval' => 2,
55-
'status' => true,
56-
'timeout' => 2,
5757
'weight' => 1,
5858
];
5959
}
6060

6161
return $servers;
6262
}
63+
64+
/**
65+
* @return array
66+
*/
67+
protected function getOptions(): array
68+
{
69+
if (!extension_loaded('memcached')) {
70+
return [];
71+
}
72+
73+
return [
74+
75+
// Assure that dead servers are properly removed and ...
76+
\Memcached::OPT_REMOVE_FAILED_SERVERS => true,
77+
78+
// ... retried after a short while (here: 2 seconds)
79+
\Memcached::OPT_RETRY_TIMEOUT => 2,
80+
81+
// KETAMA must be enabled so that replication can be used
82+
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
83+
84+
// Replicate the data, i.e. write it to both memcached servers
85+
\Memcached::OPT_NUMBER_OF_REPLICAS => 1,
86+
87+
// Those values assure that a dead (due to increased latency or
88+
// really unresponsive) memcached server increased dropped fast
89+
// and the other is used.
90+
\Memcached::OPT_POLL_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS, // milliseconds
91+
\Memcached::OPT_SEND_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS * 1000, // microseconds
92+
\Memcached::OPT_RECV_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS * 1000, // microseconds
93+
\Memcached::OPT_CONNECT_TIMEOUT => self::TIMEOUT_IN_MILLISECONDS, // milliseconds
94+
95+
// Further performance tuning
96+
\Memcached::OPT_NO_BLOCK => true,
97+
];
98+
}
6399
}

src/init.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
// session config
1313
ini_set('session.save_handler', 'memcached');
1414
ini_set('session.save_path', implode(',', $handlers));
15+
ini_set('memcached.sess_locking', 0);
1516

1617
if (getenv('MEMCACHE_COUNT') == 2) {
1718
ini_set('memcached.sess_number_of_replicas', 1);
1819
ini_set('memcached.sess_consistent_hash', 1);
1920
ini_set('memcached.sess_binary', 1);
21+
ini_set('memcached.sess_remove_failed_servers', 1);
2022
}
2123
}
2224

0 commit comments

Comments
 (0)