Skip to content

Commit

Permalink
Reworked unlimited_reconnects into max_reconnects
Browse files Browse the repository at this point in the history
Changed unlimited_reconnects to max_reconnects. If not set is MAX_FAILED_RECONNECTS used. If set as -1 are unlimited reconnects used.
  • Loading branch information
Jocke4f committed Dec 27, 2021
1 parent 1c246a8 commit be30fa4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/_helpers/configSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ export default {
description: "Configure reconnect intervall",
type: "number"
},
unlimited_reconnects: {
description: "Enables unlimited reconnect attemps",
type: "boolean"
max_reconnects: {
description: "Configured number of reconnects",
type: "number"
}
},
required: ["deviceId", "address", "id", "sourceId", "bus", "rename", "reconnect_intervall", "unlimted_reconnects"]
required: ["deviceId", "address", "id", "sourceId", "bus", "rename", "reconnect_intervall", "max_reconnects"]
},
uniqueItems: true
},
Expand Down
2 changes: 1 addition & 1 deletion src/_models/DeviceSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface DeviceSource {
bus: string;
rename: boolean;
reconnect_intervall: number;
unlimited_reconnects: boolean;
max_reconnects: number;

// Volatile
cloudConnection?: any;
Expand Down
2 changes: 1 addition & 1 deletion src/_models/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Source {
sourceTypeId: string;
data: Record<string, any>;
reconnect_intervall: number;
unlimited_reconnects: boolean;
max_reconnects: number;

// Volatile
cloudClientId?: any;
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@ function ToggleTestMode(enabled: boolean) {
data: {
addressesNumber: devices.length,
},
reconnect_intervall: 5000, // NEW
unlimited_reconnects: true, // NEW
reconnect_intervall: 5000,
max_reconnects: 5,
};
//turn on test mode
sources.push(testModeSource);
Expand All @@ -988,8 +988,8 @@ function ToggleTestMode(enabled: boolean) {
sourceId: testModeSource.id,
bus: "",
rename: false,
reconnect_intervall: 5000, // NEW
unlimited_reconnects: false, // NEW
reconnect_intervall: 5000,
max_reconnects: 5,
});
}

Expand Down Expand Up @@ -1565,8 +1565,8 @@ function TallyArbiter_Edit_Source(obj: Manage): ManageResponse {
sources[i].data = sourceObj.data;
sourceTypeId = sources[i].sourceTypeId;
connected = sources[i].connected;
sources[i].reconnect_intervall = sourceObj.reconnect_intervall; // NEW
sources[i].unlimited_reconnects = sourceObj.unlimited_reconnects; // NEW
sources[i].reconnect_intervall = sourceObj.reconnect_intervall;
sources[i].max_reconnects = sourceObj.max_reconnects;
}
}

Expand Down Expand Up @@ -1728,8 +1728,8 @@ function TallyArbiter_Edit_Device_Source(obj: Manage): ManageResponse {
device_sources[i].bus = deviceSourceObj.bus;
}
device_sources[i].rename = deviceSourceObj.rename;
device_sources[i].reconnect_intervall = deviceSourceObj.reconnect_intervall; // NEW
device_sources[i].unlimited_reconnects = deviceSourceObj.unlimited_reconnects; // NEW
device_sources[i].reconnect_intervall = deviceSourceObj.reconnect_intervall;
device_sources[i].max_reconnects = deviceSourceObj.max_reconnects;
}

let deviceName = GetDeviceByDeviceId(deviceId).name;
Expand Down
40 changes: 26 additions & 14 deletions src/sources/_Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ export class TallyInput extends EventEmitter {
super();
this.source = source;
logger(`Source: ${this.source.name} Creating connection.`, 'info-quiet');

// Set max_reconnect to MAX_FAILED_RECONNECTS if not included in config.
if (this.source.max_reconnects == undefined) {
this.source.max_reconnects = MAX_FAILED_RECONNECTS;
logger(`Source: ${this.source.name} Set max reconnect.`, 'info-quiet');
}

// Log number of reconnect attempts
if (this.source.max_reconnects == -1) {
logger(`Source: ${this.source.name} Inifinite reconnect attempts.`, 'info-quiet');
} else {
logger(`Source: ${this.source.name} Reconnect attempts ${this.source.max_reconnects}.`, 'info-quiet');
}

// Log reconnect timeout
// Configured timeout only used if larger then RECONNECT_INTERVAL
if (this.source.reconnect_intervall > RECONNECT_INTERVAL) {
logger(`Source: ${this.source.name} Configured reconnect timeout: ${this.source.reconnect_intervall}.`, 'info-quiet');
} else {
logger(`Source: ${this.source.name} Default reconnect timeout: ${RECONNECT_INTERVAL}.`, 'info-quiet');
}

this.connected.subscribe((connected) => {
if (connected) {
// Connected, no more reconnects for now
Expand All @@ -31,21 +53,14 @@ export class TallyInput extends EventEmitter {
} else {
if (!this.tryReconnecting) {
// Connection attempt at startup
logger(`Source: ${this.source.name} Connect triggered at startup.`, 'info-quiet');

if (this.source.unlimited_reconnects) {
logger(`Source: ${this.source.name} Inifinite reconnect attempts.`, 'info-quiet');
} else {
logger(`Source: ${this.source.name} Max default reconnect attempts ${MAX_FAILED_RECONNECTS}.`, 'info-quiet');
}
logger(`Source: ${this.source.name} Connect triggered at startup.`, 'info-quiet');
this.tryReconnecting = true;
return;
}

// Reconnect if number of reconnects less than max number of reconnects or
// if infinite reconnects are configured
if ((this.tryReconnecting && this.reconnectFailureCounter < MAX_FAILED_RECONNECTS) ||
(this.tryReconnecting && this.source.unlimited_reconnects)) {
// if infinite reconnects are configured (-1)
if (this.tryReconnecting && (this.source.max_reconnects == -1) || (this.reconnectFailureCounter < this.source.max_reconnects)) {
if (this.reconnectTimeout) {
logger(`Source: ${this.source.name} Reconnect timeout not set.`, 'info-quiet');
return;
Expand All @@ -54,21 +69,18 @@ export class TallyInput extends EventEmitter {
this.reconnectFailureCounter++;
logger(`Source: ${this.source.name} Reconnect attempt: ${this.reconnectFailureCounter}.`, 'info-quiet');

// Use configured timeout only if larger then tally arbiter default
// Use configured timeout only if larger then RECONNECT_INTERVAL
if (this.source.reconnect_intervall > RECONNECT_INTERVAL) {
logger(`Source: ${this.source.name} Specific reconnect timeout: ${this.source.reconnect_intervall}.`, 'info-quiet');
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = undefined;
this.reconnect();
}, this.source.reconnect_intervall);
} else {
logger(`Source: ${this.source.name} Default reconnect timeout ${RECONNECT_INTERVAL}.`, 'info-quiet');
this.reconnectTimeout = setTimeout(() => {
logger(`Source: ${this.source.name} Default timeout.`, 'info-quiet');
this.reconnectTimeout = undefined;
this.reconnect();
}, RECONNECT_INTERVAL);

}
} else {
logger(`Source: ${this.source.name} No more reconnects.`, 'info-quiet');
Expand Down

0 comments on commit be30fa4

Please sign in to comment.