Skip to content

Commit

Permalink
Make services use a generated ID rather than PID to avoid dupes, incr…
Browse files Browse the repository at this point in the history
…ease rate of TTL renews for Consul sessions
  • Loading branch information
Brandon Fulljames committed May 17, 2019
1 parent 617968b commit 0aad617
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/serviceDiscovery/engines/consul/index.js
Expand Up @@ -89,20 +89,19 @@ class ConsulService extends Service {
announce(port, metadata, cb) {
logger.debug(`Announcing service ${this.name} on port ${port}`);
this._announces.push({port, metadata});
this.session((err, id) => {
this.session((err) => {
if (err) {
return cb(err);
}

const key = [this.path, os.hostname(), process.pid].join('/');
const key = [this.path, os.hostname(), helpers.generateId(port)].join('/');
this._consul.kv.set({
key,
value: JSON.stringify({
port,
ip: this._ip,
data: metadata
}),
acquire: id
})
}, cb);
});
}
Expand Down Expand Up @@ -242,7 +241,7 @@ class ConsulService extends Service {
logger.error.data(err).log(`Failed to renew session ${this._sessionId}`);
}

this._keepaliveTimer = setTimeout(this._keepalive.bind(this, sessionId), this.ttl);
this._keepaliveTimer = setTimeout(this._keepalive.bind(this, sessionId), this.ttl / 2);
});
}

Expand Down

0 comments on commit 0aad617

Please sign in to comment.