homebridge-navilink 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +25 -2
- package/dist/api/circuit-breaker.d.ts +111 -0
- package/dist/api/circuit-breaker.js +221 -0
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/api/rest.d.ts +22 -0
- package/dist/api/rest.js +132 -62
- package/dist/diagnostics/collector.d.ts +13 -4
- package/dist/diagnostics/collector.js +25 -4
- package/dist/diagnostics/format.d.ts +4 -2
- package/dist/diagnostics/format.js +12 -7
- package/dist/platform.js +8 -1
- package/dist/session.d.ts +6 -0
- package/dist/session.js +23 -6
- package/dist/types/index.d.ts +5 -0
- package/dist/utils/errors.d.ts +15 -0
- package/dist/utils/errors.js +22 -1
- package/docs/FEATURES.md +2 -1
- package/docs/PROTOCOL.md +2 -0
- package/docs/README-DETAILED.md +26 -4
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.4](https://github.com/tbaur/homebridge-navilink/compare/v1.0.3...v1.0.4) (2026-10-18)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* rename cached accessories through updateDisplayName ([#32](https://github.com/tbaur/homebridge-navilink/issues/32)) ([c03764b](https://github.com/tbaur/homebridge-navilink/commit/c03764ba55e2e0fa794ec0607a47d6355e719234))
|
|
9
|
+
|
|
10
|
+
## [1.0.3](https://github.com/tbaur/homebridge-navilink/compare/v1.0.2...v1.0.3) (2026-09-20)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* fail fast REST sign-in during a sustained cloud outage ([#20](https://github.com/tbaur/homebridge-navilink/issues/20)) ([41f0a39](https://github.com/tbaur/homebridge-navilink/commit/41f0a3963d9333850144d243945b83b6d6ac725c))
|
|
16
|
+
|
|
3
17
|
## [1.0.2](https://github.com/tbaur/homebridge-navilink/compare/v1.0.1...v1.0.2) (2026-09-20)
|
|
4
18
|
|
|
5
19
|
|
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/homebridge-navilink)
|
|
5
5
|
[](https://www.npmjs.com/package/homebridge-navilink)
|
|
6
6
|
[](https://nodejs.org)
|
|
7
|
+
[](https://github.com/homebridge/homebridge/wiki/Verified-Plugins)
|
|
7
8
|
[](https://homebridge.io)
|
|
8
9
|
[](LICENSE)
|
|
9
10
|
|
|
@@ -13,6 +14,8 @@ Scheduling, weekly programmes and commissioning stay in the NaviLink app, which
|
|
|
13
14
|
|
|
14
15
|
## Features
|
|
15
16
|
|
|
17
|
+
|
|
18
|
+
|
|
16
19
|
### Per appliance
|
|
17
20
|
|
|
18
21
|
- **Hot water thermostat:** the domestic hot water setpoint, reading the actual outlet temperature
|
|
@@ -22,6 +25,8 @@ Scheduling, weekly programmes and commissioning stay in the NaviLink app, which
|
|
|
22
25
|
- **Fault sensor:** a contact sensor that opens and logs on an error code, so HomeKit can notify you
|
|
23
26
|
- **Temperature sensors:** hot water in and out, heating flow and return, and the optional outdoor probe
|
|
24
27
|
|
|
28
|
+
|
|
29
|
+
|
|
25
30
|
### Reliability
|
|
26
31
|
|
|
27
32
|
- **Push, not polling.** The gateway sends changes as they happen, so a setpoint changed at the wall controller or in the NaviLink app reaches HomeKit in about a second. The poll is only a backstop
|
|
@@ -30,6 +35,9 @@ Scheduling, weekly programmes and commissioning stay in the NaviLink app, which
|
|
|
30
35
|
- **A connected socket is not a live appliance.** Readings carry the time they were taken, and a stale one becomes No Response instead
|
|
31
36
|
- **State:** No Response until the appliance has actually been read, and not a value it cannot confirm
|
|
32
37
|
- **Never loses your rooms:** a broken config disables the platform without unregistering anything
|
|
38
|
+
- **A REST circuit breaker** fails fast during a sustained cloud outage. The log is `Circuit breaker CLOSED -> OPEN`, then `HALF_OPEN` and `CLOSED` when it recovers. Firmware reads do not trip it
|
|
39
|
+
|
|
40
|
+
|
|
33
41
|
|
|
34
42
|
### Quality
|
|
35
43
|
|
|
@@ -42,6 +50,8 @@ Every accessory, field and log line is documented in [Detailed documentation](do
|
|
|
42
50
|
|
|
43
51
|
## Quick Start
|
|
44
52
|
|
|
53
|
+
|
|
54
|
+
|
|
45
55
|
### 1. Install
|
|
46
56
|
|
|
47
57
|
**Homebridge UI** (recommended): Plugins → Search `homebridge-navilink` → Install
|
|
@@ -50,6 +60,8 @@ Every accessory, field and log line is documented in [Detailed documentation](do
|
|
|
50
60
|
npm install -g homebridge-navilink
|
|
51
61
|
```
|
|
52
62
|
|
|
63
|
+
|
|
64
|
+
|
|
53
65
|
### 2. Prepare your appliance
|
|
54
66
|
|
|
55
67
|
Your NaviLink gateway must be connected and showing as online in the NaviLink app. Nothing needs enabling: the plugin uses the same service the app does. If the app cannot see the appliance, neither can this.
|
|
@@ -94,12 +106,15 @@ Accessories appear in the Home app after restart, showing No Response for the fe
|
|
|
94
106
|
|
|
95
107
|
Any Navien appliance that the NaviLink app controls. The cloud interface is the same for all of them; what differs is which capabilities an appliance reports.
|
|
96
108
|
|
|
109
|
+
|
|
97
110
|
| Verified against | Notes |
|
|
98
111
|
| ----------------- | ------------------------------------------------- |
|
|
99
112
|
| **NCB-240E (NG)** | Combi: hot water and space heating, firmware 4352 |
|
|
100
113
|
|
|
114
|
+
|
|
101
115
|
Other families are decoded from the same table the NaviLink app uses and are expected to work, but nobody has confirmed them here:
|
|
102
116
|
|
|
117
|
+
|
|
103
118
|
| Family | Expected |
|
|
104
119
|
| ------------------ | ------------------------------------------------------------------------------ |
|
|
105
120
|
| **NPE, NPE2, NPN** | Tankless water heaters. Hot water only; the heating thermostat disables itself |
|
|
@@ -108,12 +123,14 @@ Other families are decoded from the same table the NaviLink app uses and are exp
|
|
|
108
123
|
| **NFB, NFC** | Boilers with hot water and space heating |
|
|
109
124
|
| **NVW** | Water heater with a tank |
|
|
110
125
|
|
|
126
|
+
|
|
111
127
|
If yours is not listed, it will most likely work. Please open an issue with the output of `node scripts/capture-fixture.js --redact` either way, so the table can say so with confidence.
|
|
112
128
|
|
|
113
129
|
## Configuration Options
|
|
114
130
|
|
|
115
131
|
Only one `NaviLink` platform block is supported. It can hold as many appliances as the account owns.
|
|
116
132
|
|
|
133
|
+
|
|
117
134
|
| Option | Required to run | Description |
|
|
118
135
|
| ----------------------------- | --------------- | ------------------------------------------------------------------------------- |
|
|
119
136
|
| `name` | ✓ | Plugin instance name shown in Homebridge logs |
|
|
@@ -127,15 +144,17 @@ Only one `NaviLink` platform block is supported. It can hold as many appliances
|
|
|
127
144
|
| `options.structuredLogs` | | With diagnostics, also emit a JSON line (default false) |
|
|
128
145
|
| `options.accessoryPrefix` | | Prefix for every HomeKit name. Blank uses each appliance's name |
|
|
129
146
|
|
|
147
|
+
|
|
130
148
|
Each entry in `devices[]` takes `id` and `name`, plus an optional `channel` and one flag per accessory: `dhw`, `heating`, `power`, `recirculation`, `fault`, `temperatureSensors` and `outdoorSensor`. The [detailed documentation](docs/README-DETAILED.md#devices-entries) describes each one.
|
|
131
149
|
|
|
132
150
|
## Not Working?
|
|
133
151
|
|
|
134
152
|
1. **"NaviLink rejected the email address or password."** Sign in to the NaviLink app with the same credentials. The plugin has stopped trying on purpose, so fix the password and restart Homebridge
|
|
135
153
|
2. **Everything shows No Response.** Check the log for `platform disabled`, which means the configuration could not be read
|
|
136
|
-
3. **The heating thermostat says it has no loop.** Your appliance reports no space-heating circuit
|
|
137
|
-
4. **A recirculation switch that is not offered.** No pump is fitted, or it is not commissioned
|
|
154
|
+
3. **The heating thermostat says it has no loop.** Your appliance reports no space-heating circuit
|
|
155
|
+
4. **A recirculation switch that is not offered.** No pump is fitted, or it is not commissioned
|
|
138
156
|
5. **`rate limited`.** The cloud rate-limits control. The plugin pauses and retries on the next press
|
|
157
|
+
6. **`Circuit breaker CLOSED -> OPEN`.** Sign-in is paused; tiles stay No Response until the cloud recovers
|
|
139
158
|
|
|
140
159
|
The [full troubleshooting list](docs/README-DETAILED.md#troubleshooting) covers more, including what Off on the hot water tile does once you allow power-off.
|
|
141
160
|
|
|
@@ -151,6 +170,8 @@ This plugin needs your NaviLink password, stores it in the Homebridge configurat
|
|
|
151
170
|
- A NaviLink account with at least one gateway online
|
|
152
171
|
- A working internet connection
|
|
153
172
|
|
|
173
|
+
|
|
174
|
+
|
|
154
175
|
## More Info
|
|
155
176
|
|
|
156
177
|
- [Detailed documentation](docs/README-DETAILED.md)
|
|
@@ -160,6 +181,8 @@ This plugin needs your NaviLink password, stores it in the Homebridge configurat
|
|
|
160
181
|
- [Report Issues](https://github.com/tbaur/homebridge-navilink/issues)
|
|
161
182
|
- [Changelog](CHANGELOG.md)
|
|
162
183
|
|
|
184
|
+
|
|
185
|
+
|
|
163
186
|
## License
|
|
164
187
|
|
|
165
188
|
Copyright 2026 tbaur
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 tbaur
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* See LICENSE file for full license text
|
|
6
|
+
*
|
|
7
|
+
* @fileoverview Circuit breaker for REST resilience.
|
|
8
|
+
*
|
|
9
|
+
* Prevents hammering the NaviLink REST cloud when it is down: after a
|
|
10
|
+
* threshold of service-health failures the breaker opens, later calls fail
|
|
11
|
+
* fast until a cooldown elapses, then a single half-open probe decides
|
|
12
|
+
* whether to close again or stay open.
|
|
13
|
+
*
|
|
14
|
+
* MQTT is a separate transport and is not gated here. A dead broker is the
|
|
15
|
+
* session reconnect loop and the `mqttDown` diagnostic, not this breaker.
|
|
16
|
+
*/
|
|
17
|
+
/** Circuit breaker states. */
|
|
18
|
+
export declare enum CircuitState {
|
|
19
|
+
/** Normal operation — requests flow through. */
|
|
20
|
+
CLOSED = "CLOSED",
|
|
21
|
+
/** Circuit tripped — requests fail immediately. */
|
|
22
|
+
OPEN = "OPEN",
|
|
23
|
+
/** Testing whether the service recovered. */
|
|
24
|
+
HALF_OPEN = "HALF_OPEN"
|
|
25
|
+
}
|
|
26
|
+
/** Circuit breaker configuration. */
|
|
27
|
+
export interface CircuitBreakerConfig {
|
|
28
|
+
/** Number of failures within the window before opening the circuit. */
|
|
29
|
+
failureThreshold: number;
|
|
30
|
+
/** Time in ms before trying half-open after opening. */
|
|
31
|
+
resetTimeout: number;
|
|
32
|
+
/** Max concurrent probe requests allowed in half-open state. */
|
|
33
|
+
halfOpenMax: number;
|
|
34
|
+
/** Sliding window (ms) for counting failures. */
|
|
35
|
+
failureWindow?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Invoked whenever the circuit transitions between states. Used for
|
|
38
|
+
* observability so operators can see when the breaker opens or recovers.
|
|
39
|
+
*/
|
|
40
|
+
onStateChange?: (from: CircuitState, to: CircuitState) => void;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Default circuit breaker configuration.
|
|
44
|
+
*
|
|
45
|
+
* `halfOpenMax` is 1 on purpose: establish is sequential (sign-in, then
|
|
46
|
+
* list), and a single probe keeps recovery deterministic, matching the
|
|
47
|
+
* sibling plugins.
|
|
48
|
+
*/
|
|
49
|
+
export declare const DEFAULT_CIRCUIT_BREAKER_CONFIG: CircuitBreakerConfig;
|
|
50
|
+
/** Circuit breaker status snapshot. */
|
|
51
|
+
export interface CircuitBreakerStatus {
|
|
52
|
+
state: CircuitState;
|
|
53
|
+
failures: number;
|
|
54
|
+
successes: number;
|
|
55
|
+
lastFailureTime: number | null;
|
|
56
|
+
halfOpenRequests: number;
|
|
57
|
+
isOpen: boolean;
|
|
58
|
+
remainingResetTime: number | null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Circuit breaker for REST resilience.
|
|
62
|
+
* Prevents hammering NaviLink REST when it is returning sustained failures.
|
|
63
|
+
*/
|
|
64
|
+
export declare class CircuitBreaker {
|
|
65
|
+
private readonly failureThreshold;
|
|
66
|
+
private readonly resetTimeout;
|
|
67
|
+
private readonly halfOpenMax;
|
|
68
|
+
private readonly failureWindow;
|
|
69
|
+
private readonly onStateChange?;
|
|
70
|
+
private _state;
|
|
71
|
+
private failures;
|
|
72
|
+
private successes;
|
|
73
|
+
private lastFailureTime;
|
|
74
|
+
private halfOpenRequests;
|
|
75
|
+
private failureTimestamps;
|
|
76
|
+
constructor(config?: Partial<CircuitBreakerConfig>);
|
|
77
|
+
/** Current circuit state. */
|
|
78
|
+
get state(): CircuitState;
|
|
79
|
+
/** True when the circuit is open (requests should fail fast). */
|
|
80
|
+
get isOpen(): boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Transition to a new state, notifying observers only on an actual change.
|
|
83
|
+
*/
|
|
84
|
+
private transitionTo;
|
|
85
|
+
/** Drop failure timestamps that fall outside the sliding window. */
|
|
86
|
+
private cleanupFailures;
|
|
87
|
+
/** Whether the circuit currently allows a request through. */
|
|
88
|
+
canRequest(): boolean;
|
|
89
|
+
/** Record a successful request. */
|
|
90
|
+
recordSuccess(): void;
|
|
91
|
+
/** Record a failed request that reflects service health. */
|
|
92
|
+
recordFailure(): void;
|
|
93
|
+
/** Track a half-open probe request against the concurrency cap. */
|
|
94
|
+
trackHalfOpenRequest(): void;
|
|
95
|
+
/** Reset the circuit breaker to closed state. */
|
|
96
|
+
reset(): void;
|
|
97
|
+
/** Current circuit breaker status. */
|
|
98
|
+
getStatus(): CircuitBreakerStatus;
|
|
99
|
+
/**
|
|
100
|
+
* Execute a function with circuit breaker protection.
|
|
101
|
+
*
|
|
102
|
+
* @param isFailure Optional predicate controlling which thrown errors count
|
|
103
|
+
* as service-health failures while CLOSED. Defaults to treating every
|
|
104
|
+
* rejection as a failure. REST uses a narrower predicate (connection and
|
|
105
|
+
* protocol errors). While HALF_OPEN, every terminal rejection still
|
|
106
|
+
* re-opens the breaker so the probe slot cannot wedge.
|
|
107
|
+
*/
|
|
108
|
+
execute<T>(fn: () => Promise<T>, isFailure?: (error: unknown) => boolean): Promise<T>;
|
|
109
|
+
/** Create a wrapped version of a function with circuit breaker protection. */
|
|
110
|
+
wrap<T extends unknown[], R>(fn: (...args: T) => Promise<R>, isFailure?: (error: unknown) => boolean): (...args: T) => Promise<R>;
|
|
111
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) 2026 tbaur
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0
|
|
6
|
+
* See LICENSE file for full license text
|
|
7
|
+
*
|
|
8
|
+
* @fileoverview Circuit breaker for REST resilience.
|
|
9
|
+
*
|
|
10
|
+
* Prevents hammering the NaviLink REST cloud when it is down: after a
|
|
11
|
+
* threshold of service-health failures the breaker opens, later calls fail
|
|
12
|
+
* fast until a cooldown elapses, then a single half-open probe decides
|
|
13
|
+
* whether to close again or stay open.
|
|
14
|
+
*
|
|
15
|
+
* MQTT is a separate transport and is not gated here. A dead broker is the
|
|
16
|
+
* session reconnect loop and the `mqttDown` diagnostic, not this breaker.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.CircuitBreaker = exports.DEFAULT_CIRCUIT_BREAKER_CONFIG = exports.CircuitState = void 0;
|
|
20
|
+
const errors_1 = require("../utils/errors");
|
|
21
|
+
/** Circuit breaker states. */
|
|
22
|
+
var CircuitState;
|
|
23
|
+
(function (CircuitState) {
|
|
24
|
+
/** Normal operation — requests flow through. */
|
|
25
|
+
CircuitState["CLOSED"] = "CLOSED";
|
|
26
|
+
/** Circuit tripped — requests fail immediately. */
|
|
27
|
+
CircuitState["OPEN"] = "OPEN";
|
|
28
|
+
/** Testing whether the service recovered. */
|
|
29
|
+
CircuitState["HALF_OPEN"] = "HALF_OPEN";
|
|
30
|
+
})(CircuitState || (exports.CircuitState = CircuitState = {}));
|
|
31
|
+
/**
|
|
32
|
+
* Default circuit breaker configuration.
|
|
33
|
+
*
|
|
34
|
+
* `halfOpenMax` is 1 on purpose: establish is sequential (sign-in, then
|
|
35
|
+
* list), and a single probe keeps recovery deterministic, matching the
|
|
36
|
+
* sibling plugins.
|
|
37
|
+
*/
|
|
38
|
+
exports.DEFAULT_CIRCUIT_BREAKER_CONFIG = {
|
|
39
|
+
failureThreshold: 5,
|
|
40
|
+
resetTimeout: 30_000,
|
|
41
|
+
halfOpenMax: 1,
|
|
42
|
+
failureWindow: 60_000,
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Circuit breaker for REST resilience.
|
|
46
|
+
* Prevents hammering NaviLink REST when it is returning sustained failures.
|
|
47
|
+
*/
|
|
48
|
+
class CircuitBreaker {
|
|
49
|
+
failureThreshold;
|
|
50
|
+
resetTimeout;
|
|
51
|
+
halfOpenMax;
|
|
52
|
+
failureWindow;
|
|
53
|
+
onStateChange;
|
|
54
|
+
_state = CircuitState.CLOSED;
|
|
55
|
+
failures = 0;
|
|
56
|
+
successes = 0;
|
|
57
|
+
lastFailureTime = null;
|
|
58
|
+
halfOpenRequests = 0;
|
|
59
|
+
failureTimestamps = [];
|
|
60
|
+
constructor(config = {}) {
|
|
61
|
+
const merged = { ...exports.DEFAULT_CIRCUIT_BREAKER_CONFIG, ...config };
|
|
62
|
+
this.failureThreshold = merged.failureThreshold;
|
|
63
|
+
this.resetTimeout = merged.resetTimeout;
|
|
64
|
+
// At least one probe must be allowed. With a cap of 0, canRequest() would
|
|
65
|
+
// promote OPEN -> HALF_OPEN and then refuse every probe (0 < 0), wedging the
|
|
66
|
+
// breaker shut permanently with no path back to CLOSED.
|
|
67
|
+
this.halfOpenMax = Math.max(1, merged.halfOpenMax);
|
|
68
|
+
this.failureWindow = merged.failureWindow ?? 60_000;
|
|
69
|
+
this.onStateChange = merged.onStateChange;
|
|
70
|
+
}
|
|
71
|
+
/** Current circuit state. */
|
|
72
|
+
get state() {
|
|
73
|
+
return this._state;
|
|
74
|
+
}
|
|
75
|
+
/** True when the circuit is open (requests should fail fast). */
|
|
76
|
+
get isOpen() {
|
|
77
|
+
return this._state === CircuitState.OPEN;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Transition to a new state, notifying observers only on an actual change.
|
|
81
|
+
*/
|
|
82
|
+
transitionTo(next) {
|
|
83
|
+
if (this._state === next) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const previous = this._state;
|
|
87
|
+
this._state = next;
|
|
88
|
+
this.onStateChange?.(previous, next);
|
|
89
|
+
}
|
|
90
|
+
/** Drop failure timestamps that fall outside the sliding window. */
|
|
91
|
+
cleanupFailures() {
|
|
92
|
+
const cutoff = Date.now() - this.failureWindow;
|
|
93
|
+
this.failureTimestamps = this.failureTimestamps.filter((stamp) => stamp > cutoff);
|
|
94
|
+
this.failures = this.failureTimestamps.length;
|
|
95
|
+
}
|
|
96
|
+
/** Whether the circuit currently allows a request through. */
|
|
97
|
+
canRequest() {
|
|
98
|
+
if (this._state === CircuitState.CLOSED) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (this._state === CircuitState.OPEN) {
|
|
102
|
+
if (this.lastFailureTime && (Date.now() - this.lastFailureTime) >= this.resetTimeout) {
|
|
103
|
+
this.halfOpenRequests = 0;
|
|
104
|
+
this.successes = 0;
|
|
105
|
+
this.transitionTo(CircuitState.HALF_OPEN);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (this._state === CircuitState.HALF_OPEN) {
|
|
111
|
+
return this.halfOpenRequests < this.halfOpenMax;
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
/** Record a successful request. */
|
|
116
|
+
recordSuccess() {
|
|
117
|
+
if (this._state === CircuitState.HALF_OPEN) {
|
|
118
|
+
this.successes += 1;
|
|
119
|
+
if (this.successes >= this.halfOpenMax) {
|
|
120
|
+
this.reset();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (this._state === CircuitState.OPEN) {
|
|
124
|
+
// A request that was already in flight when the breaker re-opened still
|
|
125
|
+
// proves the service is reachable — close immediately rather than waiting
|
|
126
|
+
// out the full cooldown.
|
|
127
|
+
this.reset();
|
|
128
|
+
}
|
|
129
|
+
else if (this._state === CircuitState.CLOSED) {
|
|
130
|
+
this.cleanupFailures();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Record a failed request that reflects service health. */
|
|
134
|
+
recordFailure() {
|
|
135
|
+
// Already open: ignore late failures from overlapping half-open probes so
|
|
136
|
+
// they cannot keep pushing lastFailureTime forward and extend the cooldown.
|
|
137
|
+
if (this._state === CircuitState.OPEN) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const now = Date.now();
|
|
141
|
+
this.lastFailureTime = now;
|
|
142
|
+
this.failureTimestamps.push(now);
|
|
143
|
+
if (this._state === CircuitState.HALF_OPEN) {
|
|
144
|
+
this.halfOpenRequests = 0;
|
|
145
|
+
this.successes = 0;
|
|
146
|
+
this.transitionTo(CircuitState.OPEN);
|
|
147
|
+
}
|
|
148
|
+
else if (this._state === CircuitState.CLOSED) {
|
|
149
|
+
this.cleanupFailures();
|
|
150
|
+
if (this.failures >= this.failureThreshold) {
|
|
151
|
+
this.transitionTo(CircuitState.OPEN);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** Track a half-open probe request against the concurrency cap. */
|
|
156
|
+
trackHalfOpenRequest() {
|
|
157
|
+
if (this._state === CircuitState.HALF_OPEN) {
|
|
158
|
+
this.halfOpenRequests += 1;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/** Reset the circuit breaker to closed state. */
|
|
162
|
+
reset() {
|
|
163
|
+
this.failures = 0;
|
|
164
|
+
this.successes = 0;
|
|
165
|
+
this.lastFailureTime = null;
|
|
166
|
+
this.halfOpenRequests = 0;
|
|
167
|
+
this.failureTimestamps = [];
|
|
168
|
+
this.transitionTo(CircuitState.CLOSED);
|
|
169
|
+
}
|
|
170
|
+
/** Current circuit breaker status. */
|
|
171
|
+
getStatus() {
|
|
172
|
+
const now = Date.now();
|
|
173
|
+
let remainingResetTime = null;
|
|
174
|
+
if (this._state === CircuitState.OPEN && this.lastFailureTime) {
|
|
175
|
+
remainingResetTime = Math.max(0, this.resetTimeout - (now - this.lastFailureTime));
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
state: this._state,
|
|
179
|
+
failures: this.failures,
|
|
180
|
+
successes: this.successes,
|
|
181
|
+
lastFailureTime: this.lastFailureTime,
|
|
182
|
+
halfOpenRequests: this.halfOpenRequests,
|
|
183
|
+
isOpen: this.isOpen,
|
|
184
|
+
remainingResetTime,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Execute a function with circuit breaker protection.
|
|
189
|
+
*
|
|
190
|
+
* @param isFailure Optional predicate controlling which thrown errors count
|
|
191
|
+
* as service-health failures while CLOSED. Defaults to treating every
|
|
192
|
+
* rejection as a failure. REST uses a narrower predicate (connection and
|
|
193
|
+
* protocol errors). While HALF_OPEN, every terminal rejection still
|
|
194
|
+
* re-opens the breaker so the probe slot cannot wedge.
|
|
195
|
+
*/
|
|
196
|
+
async execute(fn, isFailure = () => true) {
|
|
197
|
+
if (!this.canRequest()) {
|
|
198
|
+
const status = this.getStatus();
|
|
199
|
+
throw new errors_1.CircuitBreakerError(status.remainingResetTime ?? this.resetTimeout);
|
|
200
|
+
}
|
|
201
|
+
if (this._state === CircuitState.HALF_OPEN) {
|
|
202
|
+
this.trackHalfOpenRequest();
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const result = await fn();
|
|
206
|
+
this.recordSuccess();
|
|
207
|
+
return result;
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
if (this._state === CircuitState.HALF_OPEN || isFailure(error)) {
|
|
211
|
+
this.recordFailure();
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/** Create a wrapped version of a function with circuit breaker protection. */
|
|
217
|
+
wrap(fn, isFailure) {
|
|
218
|
+
return (...args) => this.execute(() => fn(...args), isFailure);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.CircuitBreaker = CircuitBreaker;
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED
|
@@ -23,6 +23,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
__exportStar(require("./channel"), exports);
|
|
26
|
+
__exportStar(require("./circuit-breaker"), exports);
|
|
26
27
|
__exportStar(require("./http"), exports);
|
|
27
28
|
__exportStar(require("./identity"), exports);
|
|
28
29
|
__exportStar(require("./mqtt"), exports);
|
package/dist/api/rest.d.ts
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
* is logged, and the fields the plugin does not need are never read out of it.
|
|
26
26
|
*/
|
|
27
27
|
import type { PluginLogger } from '../types';
|
|
28
|
+
import { type CircuitBreakerStatus } from './circuit-breaker';
|
|
28
29
|
import { type JsonPost } from './http';
|
|
29
30
|
import type { IotCredentials } from './sigv4';
|
|
30
31
|
/** A signed-in session. */
|
|
@@ -67,6 +68,8 @@ export interface NaviLinkRestOptions {
|
|
|
67
68
|
durationMs: number;
|
|
68
69
|
ok: boolean;
|
|
69
70
|
}) => void;
|
|
71
|
+
/** Fired when the breaker transitions into OPEN, so diagnostics can count a trip. */
|
|
72
|
+
onCircuitOpen?: () => void;
|
|
70
73
|
}
|
|
71
74
|
/** Talks to the NaviLink REST service. */
|
|
72
75
|
export declare class NaviLinkRest {
|
|
@@ -75,7 +78,11 @@ export declare class NaviLinkRest {
|
|
|
75
78
|
private readonly now;
|
|
76
79
|
private readonly signal;
|
|
77
80
|
private readonly metrics;
|
|
81
|
+
private readonly onCircuitOpen;
|
|
82
|
+
private readonly circuitBreaker;
|
|
78
83
|
constructor(options: NaviLinkRestOptions);
|
|
84
|
+
/** Live breaker status for diagnostics. Never reads the network. */
|
|
85
|
+
getCircuitBreakerStatus(): CircuitBreakerStatus;
|
|
79
86
|
/**
|
|
80
87
|
* Exchange an email and password for a session.
|
|
81
88
|
*
|
|
@@ -112,6 +119,21 @@ export declare class NaviLinkRest {
|
|
|
112
119
|
additionalValue: string;
|
|
113
120
|
}): Promise<string | undefined>;
|
|
114
121
|
private call;
|
|
122
|
+
/**
|
|
123
|
+
* Run one REST logical attempt behind the circuit breaker.
|
|
124
|
+
*
|
|
125
|
+
* Pre-flight OPEN rejections do not count as API samples: nothing was sent.
|
|
126
|
+
* Connection and protocol errors trip the breaker; a rejected password does
|
|
127
|
+
* not. HALF_OPEN treats every terminal rejection as a failed probe so the
|
|
128
|
+
* slot cannot wedge.
|
|
129
|
+
*/
|
|
130
|
+
private guarded;
|
|
131
|
+
/**
|
|
132
|
+
* Surface circuit-breaker transitions so operators can see when NaviLink
|
|
133
|
+
* REST is being treated as unavailable and when it recovers. OPEN is warn;
|
|
134
|
+
* HALF_OPEN (probe) and CLOSED (recovery) are info.
|
|
135
|
+
*/
|
|
136
|
+
private logCircuitTransition;
|
|
115
137
|
private readBody;
|
|
116
138
|
/**
|
|
117
139
|
* Turn a failed sign-in into the right kind of error.
|