Skip to content

Commit dc3b94a

Browse files
committed
Improve the re-announce timings
1 parent 3b5421e commit dc3b94a

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

lib/registry.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Service from './service.js';
33

44
const REANNOUNCE_MAX_MS = 60 * 60 * 1000;
55
const REANNOUNCE_FACTOR = 3;
6+
const REANNOUNCE_TIMES = 3;
67

78
export default class Registry {
89
_server;
@@ -135,38 +136,31 @@ export default class Registry {
135136
* Broadcasts right away, then after 3 seconds, 9 seconds, 27 seconds,
136137
* and so on, up to a maximum interval of one hour.
137138
*/
138-
#announce(service) {
139-
let delay = 1000;
139+
#announce(service, count = 1) {
140140
let packet = service._records();
141141

142142
this._server.register(packet);
143143

144-
let broadcast = () => {
145-
// abort if the service have or is being stopped in the meantime
146-
if ( ! service._activated || service._destroyed) {
147-
return;
148-
}
149-
150-
service.emit('anouncing', JSON.parse(JSON.stringify(packet)));
151-
152-
this._server.mdns.respond(packet, () => {
153-
// This function will optionally be called with an error object. We'll
154-
// just silently ignore it and retry as we normally would
155-
if ( ! service.published) {
156-
service._activated = true;
157-
service.published = true;
158-
service.emit('up');
159-
}
144+
// abort if the service have or is being stopped in the meantime
145+
if ( ! service._activated || service._destroyed || count > REANNOUNCE_TIMES) {
146+
return;
147+
}
160148

161-
delay = Math.min(delay * REANNOUNCE_FACTOR, REANNOUNCE_MAX_MS);
149+
service.emit('anouncing', JSON.parse(JSON.stringify(packet)));
162150

163-
if ( ! service._destroyed) {
164-
// setTimeout(broadcast, delay).unref();
165-
}
166-
});
167-
};
151+
this._server.mdns.respond(packet, () => {
152+
// This function will optionally be called with an error object. We'll
153+
// just silently ignore it and retry as we normally would
154+
if ( ! service.published) {
155+
service._activated = true;
156+
service.published = true;
157+
service.emit('up');
158+
}
168159

169-
broadcast();
160+
if ( ! service._destroyed) {
161+
setTimeout(this.#announce.bind(this, service, count + 1), Math.min(count * REANNOUNCE_FACTOR * 1000, REANNOUNCE_MAX_MS));
162+
}
163+
});
170164
}
171165

172166
/**

0 commit comments

Comments
 (0)