UNPKG

@hiero-ledger/sdk

Version:
3 lines (2 loc) 2.46 kB
import e from"./ManagedNodeAddress.js";class t{constructor(t={}){if(null!=t.newNode)this._address="string"==typeof t.newNode.address?e.fromString(t.newNode.address):t.newNode.address,this._cert=void 0,this._channel=null,this._channelInitFunction=t.newNode.channelInitFunction,this._lastUsed=Date.now(),this._readmitTime=Date.now(),this._useCount=0,this._badGrpcStatusCount=0,this._minBackoff=8e3,this._maxBackoff=36e5,this._currentBackoff=this._minBackoff;else{if(null==t.cloneNode)throw new Error(`failed to create ManagedNode: ${JSON.stringify(t)}`);this._address=t.cloneNode.address,this._cert=t.cloneNode.node._cert,this._channel=t.cloneNode.node._channel,this._channelInitFunction=t.cloneNode.node._channelInitFunction,this._currentBackoff=t.cloneNode.node._currentBackoff,this._lastUsed=t.cloneNode.node._lastUsed,this._readmitTime=t.cloneNode.node._readmitTime,this._useCount=t.cloneNode.node._useCount,this._badGrpcStatusCount=t.cloneNode.node._badGrpcStatusCount,this._minBackoff=t.cloneNode.node._minBackoff,this._maxBackoff=t.cloneNode.node._minBackoff}}getKey(){throw new Error("not implemented")}setCert(e){return this}get address(){return this._address}get attempts(){return this._badGrpcStatusCount}get minBackoff(){return this._minBackoff}setMinBackoff(e){return this._currentBackoff<=e&&(this._currentBackoff=e),this._minBackoff=e,this}get maxBackoff(){return this._maxBackoff}setMaxBackoff(e){return this._currentBackoff<=e&&(this._currentBackoff=e),this._maxBackoff=e,this}getChannel(){return this._useCount++,this.__lastUsed=Date.now(),null!=this._channel||(this._channel=this._channelInitFunction(this.address.toString(),this._cert)),this._channel}isHealthy(){return this._readmitTime<=Date.now()}increaseBackoff(){this._currentBackoff=Math.min(2*this._currentBackoff,this._maxBackoff),this._readmitTime=Date.now()+this._currentBackoff}decreaseBackoff(){this._currentBackoff=Math.max(this._currentBackoff/2,this._minBackoff)}getRemainingTime(){return this._readmitTime-this._lastUsed}backoff(){return new Promise(e=>setTimeout(e,this.getRemainingTime()))}compare(e){let t=this.getRemainingTime()-e.getRemainingTime();return 0!=t?t:(t=this._currentBackoff-e._currentBackoff,0!=t?t:(t=this._badGrpcStatusCount-e._badGrpcStatusCount,0!=t?t:(t=this._useCount-e._useCount,0!=t?t:this._lastUsed-e._lastUsed)))}close(){null!=this._channel&&this._channel.close(),this._channel=null}}export{t as default}; //# sourceMappingURL=ManagedNode.js.map