package.dist.src.internal.grpc.idle-grpc-client-wrapper.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk Show documentation
Show all versions of sdk Show documentation
Client SDK for Momento services
The newest version!
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdleGrpcClientWrapper = void 0;
/**
* This wrapper allows us to ensure that a grpc client is not re-used if it has been idle
* for longer than a specified period of time. This is important in some environments,
* such as AWS Lambda, where the runtime may be paused indefinitely between invocations.
* In such cases we have observed that while the runtime is suspended, the connection
* may have been closed by the server. (e.g., AWS NLB has an idle timeout of 350s:
* https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancers.html#connection-idle-timeout )
* When the runtime resumes, it does not recognize that the connection has been closed,
* and it may continue to attempt to send bytes to it, resulting in client-side timeouts
* (DEADLINE_EXCEEDED). Forcefully refreshing the client if it has been idle for too
* long will prevent this.
*
* NOTE: We can't rely on keepalive pings in this scenario, because the lambda runtime
* may be suspended in such a way that background tasks such as the keepalive pings
* will not be able to execute.
*/
class IdleGrpcClientWrapper {
constructor(props) {
this.logger = props.loggerFactory.getLogger(this);
this.clientFactoryFn = props.clientFactoryFn;
this.client = this.clientFactoryFn();
this.maxIdleMillis = props.maxIdleMillis;
this.lastAccessTime = Date.now();
this.maxClientAgeMillis = props.maxClientAgeMillis;
this.clientCreatedTime = Date.now();
}
getClient() {
this.logger.trace(`Checking to see if client has been idle for more than ${this.maxIdleMillis} ms`);
if (Date.now() - this.lastAccessTime > this.maxIdleMillis) {
this.logger.info(`Client has been idle for more than ${this.maxIdleMillis} ms; reconnecting.`);
this.client.close();
this.client = this.clientFactoryFn();
}
if (this.maxClientAgeMillis !== undefined) {
this.logger.trace(`Checking to see if client was created more than ${this.maxClientAgeMillis} ms`);
if (Date.now() - this.clientCreatedTime > this.maxClientAgeMillis) {
this.logger.info(`Client was created more than ${this.maxClientAgeMillis} millis ago; recreating as asked.`);
this.client.close();
this.client = this.clientFactoryFn();
this.clientCreatedTime = Date.now();
}
}
this.lastAccessTime = Date.now();
return this.client;
}
}
exports.IdleGrpcClientWrapper = IdleGrpcClientWrapper;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaWRsZS1ncnBjLWNsaWVudC13cmFwcGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL2ludGVybmFsL2dycGMvaWRsZS1ncnBjLWNsaWVudC13cmFwcGVyLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQVVBOzs7Ozs7Ozs7Ozs7Ozs7R0FlRztBQUNILE1BQWEscUJBQXFCO0lBYWhDLFlBQVksS0FBb0M7UUFDOUMsSUFBSSxDQUFDLE1BQU0sR0FBRyxLQUFLLENBQUMsYUFBYSxDQUFDLFNBQVMsQ0FBQyxJQUFJLENBQUMsQ0FBQztRQUNsRCxJQUFJLENBQUMsZUFBZSxHQUFHLEtBQUssQ0FBQyxlQUFlLENBQUM7UUFDN0MsSUFBSSxDQUFDLE1BQU0sR0FBRyxJQUFJLENBQUMsZUFBZSxFQUFFLENBQUM7UUFDckMsSUFBSSxDQUFDLGFBQWEsR0FBRyxLQUFLLENBQUMsYUFBYSxDQUFDO1FBQ3pDLElBQUksQ0FBQyxjQUFjLEdBQUcsSUFBSSxDQUFDLEdBQUcsRUFBRSxDQUFDO1FBQ2pDLElBQUksQ0FBQyxrQkFBa0IsR0FBRyxLQUFLLENBQUMsa0JBQWtCLENBQUM7UUFDbkQsSUFBSSxDQUFDLGlCQUFpQixHQUFHLElBQUksQ0FBQyxHQUFHLEVBQUUsQ0FBQztJQUN0QyxDQUFDO0lBRUQsU0FBUztRQUNQLElBQUksQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUNmLHlEQUF5RCxJQUFJLENBQUMsYUFBYSxLQUFLLENBQ2pGLENBQUM7UUFDRixJQUFJLElBQUksQ0FBQyxHQUFHLEVBQUUsR0FBRyxJQUFJLENB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luZm8oXG4gICAgICAgIGBDbGllbnQgaGFzIGJlZW4gaWRsZSBmb3IgbW9yZSB0aGFuICR7dGhpcy5tYXhJZGxlTWlsbGlzfSBtczsgcmVjb25uZWN0aW5nLmBcbiAgICAgICk7XG4gICAgICB0aGlzLmNsaWVudC5jbG9zZSgpO1xuICAgICAgdGhpcy5jbGllbnQgPSB0aGlzLmNsaWVudEZhY3RvcnlGbigpO1xuICAgIH1cblxuICAgIGlmICh0aGlzLm1heENsaWVudEFnZU1pbGxpcyAhPT0gdW5kZWZpbmVkKSB7XG4gICAgICB0aGlzLmxvZ2dlci50cmFjZShcbiAgICAgICAgYENoZWNraW5nIHRvIHNlZSBpZiBjbGllbnQgd2FzIGNyZWF0ZWQgbW9yZSB0aGFuICR7dGhpcy5tYXhDbGllbnRBZ2VNaWxsaXN9IG1zYFxuICAgICAgKTtcbiAgICAgIGlmIChEYXRlLm5vdygpIC0gdGhpcy5jbGllbnRDcmVhdGVkVGltZSA+IHRoaXMubWF4Q2xpZW50QWdlTWlsbGlzKSB7XG4gICAgICAgIHRoaXMubG9nZ2VyLmluZm8oXG4gICAgICAgICAgYENsaWVudCB3YXMgY3JlYXRlZCBtb3JlIHRoYW4gJHt0aGlzLm1heENsaWVudEFnZU1pbGxpc30gbWlsbGlzIGFnbzsgcmVjcmVhdGluZyBhcyBhc2tlZC5gXG4gICAgICAgICk7XG4gICAgICAgIHRoaXMuY2xpZW50LmNsb3NlKCk7XG4gICAgICAgIHRoaXMuY2xpZW50ID0gdGhpcy5jbGllbnRGYWN0b3J5Rm4oKTtcbiAgICAgICAgdGhpcy5jbGllbnRDcmVhdGVkVGltZSA9IERhdGUubm93KCk7XG4gICAgICB9XG4gICAgfVxuXG4gICAgdGhpcy5sYXN0QWNjZXNzVGltZSA9IERhdGUubm93KCk7XG4gICAgcmV0dXJuIHRoaXMuY2xpZW50O1xuICB9XG59XG4iXX0=
© 2015 - 2025 Weber Informatics LLC | Privacy Policy