Messaging service, adapters, refactoring
This commit is contained in:
@@ -7,29 +7,47 @@ import (
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"git.derfenix.pro/fenix/astontest/adapters/randomstring"
|
||||
"git.derfenix.pro/fenix/astontest/adapters/stdoutstore"
|
||||
"git.derfenix.pro/fenix/astontest/pkg/discovery"
|
||||
"git.derfenix.pro/fenix/astontest/pkg/messenger"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
discoverySet discovery.DiscoverySet
|
||||
messenger *messenger.Messenger
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewApplication(cfg *Config, log *zap.Logger) (*Application, error) {
|
||||
discoveryOpts := []discovery.Option{discovery.WithBroadcastInterval(cfg.BroadcastInterval)}
|
||||
discoveryOpts := []discovery.Option{
|
||||
discovery.WithBroadcastInterval(cfg.BroadcastInterval),
|
||||
}
|
||||
|
||||
if cfg.Debug {
|
||||
discoveryOpts = append(discoveryOpts, discovery.WithDebug())
|
||||
}
|
||||
|
||||
set, err := discovery.NewDiscoverySet(log.Named("discovery"), cfg.DiscoveryPort, discoveryOpts...)
|
||||
discoverySet, err := discovery.NewDiscoverySet(log.Named("discovery"), cfg.DiscoveryPort, discoveryOpts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new discovery set: %w", err)
|
||||
|
||||
}
|
||||
|
||||
messengerSrv := messenger.NewMessenger(
|
||||
discoverySet.NewNodes(),
|
||||
cfg.MessagingPort,
|
||||
stdoutstore.New(),
|
||||
randomstring.New(cfg.RandomMessageSize),
|
||||
cfg.MessagingInterval,
|
||||
log.Named("messenger"),
|
||||
)
|
||||
|
||||
go discoverySet.FailNodes(messengerSrv.FailNodesCh)
|
||||
|
||||
return &Application{
|
||||
discoverySet: set,
|
||||
discoverySet: discoverySet,
|
||||
messenger: messengerSrv,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
@@ -39,4 +57,35 @@ func (a *Application) Start(ctx context.Context, wg *sync.WaitGroup) {
|
||||
for _, discover := range a.discoverySet {
|
||||
go discover.Start(ctx, wg)
|
||||
}
|
||||
|
||||
wg.Add(2)
|
||||
go a.messenger.StartServer(ctx, wg)
|
||||
go a.messenger.Start(ctx, wg)
|
||||
}
|
||||
|
||||
var _ = `
|
||||
s1-1 | Received 'apq34yod73' from <172.19.0.5>
|
||||
s1-1 | Received 'cbtfl716li' from <172.19.0.3>
|
||||
s1-1 | Received 'n84fg3mx9o' from <172.19.0.2>
|
||||
s1-1 | Received 'zb98146fqz' from <172.19.0.4>
|
||||
|
||||
s2-1 | Received '7h1s9ruilr' from <172.19.0.2>
|
||||
s2-1 | Received 'aij179r0ck' from <172.19.0.3>
|
||||
s2-1 | Received 'm3k0snj7ma' from <172.19.0.6>
|
||||
s2-1 | Received 'tw5726fo7e' from <172.19.0.5>
|
||||
|
||||
s3-1 | Received '32d20marhg' from <172.19.0.6>
|
||||
s3-1 | Received 'ffs9pi6o9j' from <172.19.0.3>
|
||||
s3-1 | Received 'wcso6aashe' from <172.19.0.5>
|
||||
s3-1 | Received 'wfhcy9xbdj' from <172.19.0.4>
|
||||
|
||||
s4-1 | Received '2le3u1ikyg' from <172.19.0.2>
|
||||
s4-1 | Received '5locacst6w' from <172.19.0.4>
|
||||
s4-1 | Received 'cf47nyd2ca' from <172.19.0.6>
|
||||
s4-1 | Received 'guvfhb7wud' from <172.19.0.5>
|
||||
|
||||
s5-1 | Received 'dsr7qt2x5l' from <172.19.0.3>
|
||||
s5-1 | Received 'l67gc5xdt3' from <172.19.0.2>
|
||||
s5-1 | Received 'mof0yp4vxt' from <172.19.0.6>
|
||||
s5-1 | Received 'slc9tw30r4' from <172.19.0.4>
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user