@@ -400,14 +400,14 @@ protocol ClusterSystemInstrumentationProvider {
400400/// in an attempt to form (or join) a cluster with those nodes. These typically should include a few initial contact points, but can also include
401401/// all the nodes of an existing cluster.
402402public struct ServiceDiscoverySettings {
403- internal let implementation : AnyServiceDiscovery
404- private let _subscribe : ( @escaping ( Result < [ Node ] , Error > ) -> Void , @escaping ( CompletionReason ) -> Void ) -> CancellationToken
403+ let implementation : ServiceDiscoveryImplementation
404+ private let _subscribe : ( @escaping ( Result < [ Node ] , Error > ) -> Void , @escaping ( CompletionReason ) -> Void ) -> CancellationToken ?
405405
406406 public init < Discovery, S> ( _ implementation: Discovery , service: S )
407407 where Discovery: ServiceDiscovery , Discovery. Instance == Node ,
408408 S == Discovery . Service
409409 {
410- self . implementation = AnyServiceDiscovery ( implementation)
410+ self . implementation = . dynamic ( AnyServiceDiscovery ( implementation) )
411411 self . _subscribe = { onNext, onComplete in
412412 implementation. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
413413 }
@@ -418,15 +418,32 @@ public struct ServiceDiscoverySettings {
418418 S == Discovery . Service
419419 {
420420 let mappedDiscovery : MapInstanceServiceDiscovery < Discovery , Node > = implementation. mapInstance ( transformer)
421- self . implementation = AnyServiceDiscovery ( mappedDiscovery)
421+ self . implementation = . dynamic ( AnyServiceDiscovery ( mappedDiscovery) )
422422 self . _subscribe = { onNext, onComplete in
423423 mappedDiscovery. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
424424 }
425425 }
426426
427+ public init ( static nodes: Set < Node > ) {
428+ self . implementation = . static( nodes)
429+ self . _subscribe = { onNext, _ in
430+ // Call onNext once and never again since the list of nodes doesn't change
431+ onNext ( . success( Array ( nodes) ) )
432+ // Ignore onComplete because static service discovery never terminates
433+
434+ // No cancellation token
435+ return nil
436+ }
437+ }
438+
427439 /// Similar to `ServiceDiscovery.subscribe` however it allows the handling of the listings to be generic and handled by the cluster system.
428440 /// This function is only intended for internal use by the `DiscoveryShell`.
429- func subscribe( onNext nextResultHandler: @escaping ( Result < [ Node ] , Error > ) -> Void , onComplete completionHandler: @escaping ( CompletionReason ) -> Void ) -> CancellationToken {
441+ func subscribe( onNext nextResultHandler: @escaping ( Result < [ Node ] , Error > ) -> Void , onComplete completionHandler: @escaping ( CompletionReason ) -> Void ) -> CancellationToken ? {
430442 self . _subscribe ( nextResultHandler, completionHandler)
431443 }
444+
445+ enum ServiceDiscoveryImplementation {
446+ case `static`( Set < Node > )
447+ case dynamic( AnyServiceDiscovery )
448+ }
432449}
0 commit comments