Skip to content

Commit

Permalink
ping all the hosts every 60 seconds to keep the hash ring fresh
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbetz committed Feb 10, 2024
1 parent e26b8a5 commit 93741ab
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@joshbetz/memcached",
"version": "1.2.3",
"version": "1.3.0",
"description": "Memcached client for modern Node JS",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/hashpool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const HashRing = require( 'hashring' );

export type HashPoolOptions = {
retry: ( retries: number ) => number;
pingInterval: number;
} & PoolOptions;

type PoolNode = {
Expand Down Expand Up @@ -32,6 +33,7 @@ export default class HashPool extends EventEmitter {
// exponential backoff up to 30 seconds

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

You have a misspelled word: backoff on Comment

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build (14.x)

You have a misspelled word: backoff on Comment

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

You have a misspelled word: backoff on Comment

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build (21.x)

You have a misspelled word: backoff on Comment

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

You have a misspelled word: backoff on Comment

Check warning on line 33 in src/hashpool.ts

View workflow job for this annotation

GitHub Actions / build

You have a misspelled word: backoff on Comment
return Math.min( exp, 30000 );
},
pingInterval: 60_000,

// Pool options
max: 10,
Expand All @@ -51,6 +53,12 @@ export default class HashPool extends EventEmitter {
for ( const node of nodes ) {
this.connect( node );
}

if ( this.opts.pingInterval > 0 ) {
setInterval( () => {
this.ping();
}, this.opts.pingInterval ).unref();
}
}

connect( node: string ) {
Expand Down
2 changes: 1 addition & 1 deletion test-hashpool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ async function sleep( ms ) {
}

async function main() {
const memcached = new HashPool( [ 'localhost:11211', 'localhost:11311' ] );
const memcached = new HashPool( [ 'localhost:11211', 'localhost:11311' ], { pingInterval: 1000 } );
while ( true ) {
try {
const key = 'test';
Expand Down

0 comments on commit 93741ab

Please sign in to comment.