Skip to content

Commit

Permalink
Add support for SpawnDaemon().
Browse files Browse the repository at this point in the history
  • Loading branch information
Baliedge committed Oct 17, 2024
1 parent 5ad2836 commit 5c17be5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
27 changes: 24 additions & 3 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ func Restart(ctx context.Context) error {
}

// StartWith a local cluster with specific addresses
func StartWith(localPeers []gubernator.PeerInfo) error {
func StartWith(localPeers []gubernator.PeerInfo, opts ...option) error {
for _, peer := range localPeers {
ctx, cancel := context.WithTimeout(context.Background(), clock.Second*10)
d, err := gubernator.SpawnDaemon(ctx, gubernator.DaemonConfig{
cfg := gubernator.DaemonConfig{
Logger: logrus.WithField("instance", peer.GRPCAddress),
InstanceID: peer.GRPCAddress,
GRPCListenAddress: peer.GRPCAddress,
Expand All @@ -163,7 +163,11 @@ func StartWith(localPeers []gubernator.PeerInfo) error {
GlobalTimeout: clock.Second * 5,
BatchTimeout: clock.Second * 5,
},
})
}
for _, opt := range opts {
opt.Apply(&cfg)
}
d, err := gubernator.SpawnDaemon(ctx, cfg)
cancel()
if err != nil {
return errors.Wrapf(err, "while starting server for addr '%s'", peer.GRPCAddress)
Expand Down Expand Up @@ -196,3 +200,20 @@ func Stop() {
peers = nil
daemons = nil
}

type option interface {
Apply(cfg *gubernator.DaemonConfig)
}

type eventChannelOption struct {
eventChannel chan<- gubernator.HitEvent
}

func (o *eventChannelOption) Apply(cfg *gubernator.DaemonConfig) {
cfg.EventChannel = o.eventChannel
}

// WithEventChannel sets EventChannel to Gubernator config.
func WithEventChannel(eventChannel chan<- gubernator.HitEvent) option {
return &eventChannelOption{eventChannel: eventChannel}
}
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ type DaemonConfig struct {
// (Optional) TraceLevel sets the tracing level, this controls the number of spans included in a single trace.
// Valid options are (tracing.InfoLevel, tracing.DebugLevel) Defaults to tracing.InfoLevel
TraceLevel tracing.Level

// (Optional) EventChannel receives hit events
EventChannel chan<- HitEvent
}

func (d *DaemonConfig) ClientTLS() *tls.Config {
Expand Down
1 change: 1 addition & 0 deletions daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (s *Daemon) Start(ctx context.Context) error {
CacheSize: s.conf.CacheSize,
Workers: s.conf.Workers,
InstanceID: s.conf.InstanceID,
EventChannel: s.conf.EventChannel,
}

s.V1Server, err = NewV1Instance(s.instanceConf)
Expand Down

0 comments on commit 5c17be5

Please sign in to comment.