65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"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),
|
|
}
|
|
|
|
if cfg.Debug {
|
|
discoveryOpts = append(discoveryOpts, discovery.WithDebug())
|
|
}
|
|
|
|
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: discoverySet,
|
|
messenger: messengerSrv,
|
|
log: log,
|
|
}, nil
|
|
}
|
|
|
|
func (a *Application) Start(ctx context.Context, wg *sync.WaitGroup) {
|
|
wg.Add(len(a.discoverySet))
|
|
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)
|
|
}
|