@@ -405,18 +405,19 @@ protocol ClusterSystemInstrumentationProvider {
405405/// all the nodes of an existing cluster.
406406public struct ServiceDiscoverySettings {
407407 let implementation : ServiceDiscoveryImplementation
408- private let _subscribe : ( @escaping ( Result < [ Cluster . Endpoint ] , Error > ) -> Void , @escaping ( CompletionReason ) -> Void ) -> CancellationToken ?
409408
410409 public init < Discovery, S> ( _ implementation: Discovery , service: S )
411410 where
412411 Discovery: ServiceDiscovery ,
413412 Discovery. Instance == Cluster . Endpoint ,
414413 S == Discovery . Service
415414 {
416- self . implementation = . dynamic( AnyServiceDiscovery ( implementation) )
417- self . _subscribe = { onNext, onComplete in
418- implementation. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
419- }
415+ self . implementation = . dynamic(
416+ serviceDiscovery: AnyServiceDiscovery ( implementation) ,
417+ subscribe: { onNext, onComplete in
418+ implementation. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
419+ }
420+ )
420421 }
421422
422423 public init < Discovery, S> ( _ implementation: Discovery , service: S , mapInstanceToNode transformer: @escaping ( Discovery . Instance ) throws -> Cluster . Endpoint )
@@ -425,33 +426,78 @@ public struct ServiceDiscoverySettings {
425426 S == Discovery . Service
426427 {
427428 let mappedDiscovery : MapInstanceServiceDiscovery < Discovery , Cluster . Endpoint > = implementation. mapInstance ( transformer)
428- self . implementation = . dynamic( AnyServiceDiscovery ( mappedDiscovery) )
429- self . _subscribe = { onNext, onComplete in
430- mappedDiscovery. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
429+ self . implementation = . dynamic(
430+ serviceDiscovery: AnyServiceDiscovery ( mappedDiscovery) ,
431+ subscribe: { onNext, onComplete in
432+ mappedDiscovery. subscribe ( to: service, onNext: onNext, onComplete: onComplete)
433+ }
434+ )
435+ }
436+
437+ init ( clusterdEndpoint: Cluster . Endpoint ) {
438+ self . implementation = . clusterDaemon(
439+ endpoint: clusterdEndpoint,
440+ initialize: { system in
441+ system. log. info ( " Joining [clusterd] at \( clusterdEndpoint) " )
442+ system. cluster. join ( endpoint: clusterdEndpoint)
443+ }
444+ )
445+ }
446+
447+ /// Locate the default `ClusterD` process and use it for discovering cluster nodes.
448+ public static var clusterd : Self {
449+ get {
450+ Self . clusterd ( endpoint: nil )
431451 }
432452 }
433453
454+ public static func clusterd( endpoint: Cluster . Endpoint ? ) -> Self {
455+ ServiceDiscoverySettings ( clusterdEndpoint: endpoint ?? ClusterDaemon . defaultEndpoint)
456+ }
457+
458+ public static func seed( nodes: Set < Cluster . Endpoint > ) -> Self {
459+ . init( static: nodes)
460+ }
461+
434462 public init ( static nodes: Set < Cluster . Endpoint > ) {
435- self . implementation = . static( nodes)
436- self . _subscribe = { onNext, _ in
437- // Call onNext once and never again since the list of nodes doesn't change
438- onNext ( . success( Array ( nodes) ) )
439- // Ignore onComplete because static service discovery never terminates
440-
441- // No cancellation token
442- return nil
443- }
463+ self . implementation = . static(
464+ endpoints: nodes,
465+ subscribe: { onNext, _ in
466+ // Call onNext once and never again since the list of nodes doesn't change
467+ onNext ( . success( Array ( nodes) ) )
468+ // Ignore onComplete because static service discovery never terminates
469+
470+ // No cancellation token
471+ return nil
472+ }
473+ )
444474 }
445475
446476 /// Similar to `ServiceDiscovery.subscribe` however it allows the handling of the listings to be generic and handled by the cluster system.
447477 /// This function is only intended for internal use by the `DiscoveryShell`.
448478 func subscribe( onNext nextResultHandler: @escaping ( Result < [ Cluster . Endpoint ] , Error > ) -> Void , onComplete completionHandler: @escaping ( CompletionReason ) -> Void ) -> CancellationToken ? {
449- self . _subscribe ( nextResultHandler, completionHandler)
479+ switch self . implementation {
480+ case . static( _, let subscribe) ,
481+ . dynamic( _, let subscribe) :
482+ subscribe ( nextResultHandler, completionHandler)
483+ case . clusterDaemon:
484+ . none
485+ }
486+ }
487+
488+ func initializeClusterd( _ system: ClusterSystem ) {
489+ switch self . implementation {
490+ case . clusterDaemon( _, let initialize) :
491+ initialize ( system)
492+ default :
493+ break
494+ }
450495 }
451496
452497 enum ServiceDiscoveryImplementation {
453- case `static`( Set < Cluster . Endpoint > )
454- case dynamic( AnyServiceDiscovery )
498+ case `static`( endpoints: Set < Cluster . Endpoint > , subscribe: ( @escaping ( Result < [ Cluster . Endpoint ] , Error > ) -> Void , @escaping ( CompletionReason ) -> Void ) -> CancellationToken ? )
499+ case dynamic( serviceDiscovery: AnyServiceDiscovery , subscribe: ( @escaping ( Result < [ Cluster . Endpoint ] , Error > ) -> Void , @escaping ( CompletionReason ) -> Void ) -> CancellationToken ? )
500+ case clusterDaemon( endpoint: Cluster . Endpoint , initialize: ( ClusterSystem ) -> Void )
455501 }
456502}
457503
0 commit comments