Skip to content

Commit

Permalink
Merge pull request #850 from ExchangeUnion/feat/maximum-capacity-chec…
Browse files Browse the repository at this point in the history
…ks-client

feat(client): add maximum capacity checks to client
  • Loading branch information
sangaman authored Mar 27, 2019
2 parents ce25345 + 0c698a1 commit f6279cd
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ type ChannelBalance = {
*/
abstract class BaseClient extends EventEmitter {
public abstract readonly cltvDelta: number;
public maximumOutboundCapacity = 0;
protected status: ClientStatus = ClientStatus.NotInitialized;
protected reconnectionTimer?: NodeJS.Timer;

/** Time in milliseconds between attempts to recheck connectivity to the client. */
protected static readonly RECONNECT_TIMER = 5000;
private updateCapacityTimer?: NodeJS.Timer;
/** Time in milliseconds between updating the maximum outbound capacity */
private CAPACITY_REFRESH_INTERVAL = 60000;

constructor(protected logger: Logger) {
super();
Expand All @@ -38,6 +42,23 @@ abstract class BaseClient extends EventEmitter {
protected setStatus(status: ClientStatus): void {
this.logger.info(`${this.constructor.name} status: ${ClientStatus[status]}`);
this.status = status;
this.checkTimers();
}
private checkTimers() {
if (this.status === ClientStatus.ConnectionVerified) {
this.updateCapacityTimer = setInterval(async () => {
try {
this.maximumOutboundCapacity = (await this.channelBalance()).balance;
} catch (e) {
// TODO: Mark client as disconnected
this.logger.error(`failed to fetch channelbalance from client: ${e}`);
}
}, this.CAPACITY_REFRESH_INTERVAL);
} else {
if (this.updateCapacityTimer) {
clearInterval(this.updateCapacityTimer);
}
}
}

/**
Expand Down Expand Up @@ -73,6 +94,9 @@ abstract class BaseClient extends EventEmitter {
if (this.reconnectionTimer) {
clearTimeout(this.reconnectionTimer);
}
if (this.updateCapacityTimer) {
clearInterval(this.updateCapacityTimer);
}
this.closeSpecific();
}
protected abstract closeSpecific(): void;
Expand Down

0 comments on commit f6279cd

Please sign in to comment.