advertise method
Advertises a service
Implementation
@override
Future<Duration> advertise(String ns, [List<DiscoveryOption> options = const []]) async {
final opts = DiscoveryOptions().apply(options);
var ttl = opts.ttl;
if (ttl == null || ttl > const Duration(hours: 3)) {
// the DHT provider record validity is 24hrs, but it is recommended to republish at least every 6hrs
// we go one step further and republish every 3hrs
ttl = const Duration(hours: 3);
}
final cid = await nsToCid(ns);
// this context requires a timeout; it determines how long the DHT looks for
// closest peers to the key/CID before it goes on to provide the record to them.
// Not setting a timeout here will make the DHT wander forever.
try {
await _router.provide(cid, true).timeout(const Duration(seconds: 60));
} catch (e) {
if (e is TimeoutException) {
// Log the timeout for monitoring and debugging purposes
developer.log(
'Timeout while providing CID in RoutingDiscovery.advertise',
name: 'dart_libp2p.discovery.routing',
error: e,
time: DateTime.now(),
);
// We continue execution despite the timeout since this is a non-critical error
// The DHT may have partially succeeded in finding peers before the timeout
} else {
// For other errors, we rethrow as they might indicate more serious issues
developer.log(
'Error in RoutingDiscovery.advertise',
name: 'dart_libp2p.discovery.routing',
error: e,
time: DateTime.now(),
);
rethrow;
}
}
return ttl;
}