Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add maximum capacity checks to client #850

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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