[#1291] listener: Stop only once

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-04-05 13:20:23 +03:00 committed by Alex Vanin
parent 77f4a5844b
commit e62e02815b
2 changed files with 9 additions and 8 deletions

View file

@ -282,9 +282,7 @@ func (s *Server) Stop() {
s.setHealthStatus(control.HealthStatus_SHUTTING_DOWN)
go s.morphListener.Stop()
if !s.withoutMainNet {
go s.mainnetListener.Stop()
}
go s.mainnetListener.Stop()
for _, c := range s.closers {
if err := c(); err != nil {

View file

@ -90,7 +90,7 @@ type ListenerParams struct {
type listener struct {
mtx *sync.RWMutex
once *sync.Once
startOnce, stopOnce *sync.Once
started bool
@ -124,7 +124,7 @@ var (
//
// Returns an error if listener was already started.
func (l listener) Listen(ctx context.Context) {
l.once.Do(func() {
l.startOnce.Do(func() {
if err := l.listen(ctx, nil); err != nil {
l.log.Error("could not start listen to events",
zap.String("error", err.Error()),
@ -140,7 +140,7 @@ func (l listener) Listen(ctx context.Context) {
//
// Returns an error if listener was already started.
func (l listener) ListenWithError(ctx context.Context, intError chan<- error) {
l.once.Do(func() {
l.startOnce.Do(func() {
if err := l.listen(ctx, intError); err != nil {
l.log.Error("could not start listen to events",
zap.String("error", err.Error()),
@ -570,7 +570,9 @@ func (l listener) RegisterNotaryHandler(hi NotaryHandlerInfo) {
// Stop closes subscription channel with remote neo node.
func (l listener) Stop() {
l.subscriber.Close()
l.stopOnce.Do(func() {
l.subscriber.Close()
})
}
func (l *listener) RegisterBlockHandler(handler BlockHandler) {
@ -593,7 +595,8 @@ func NewListener(p ListenerParams) (Listener, error) {
return &listener{
mtx: new(sync.RWMutex),
once: new(sync.Once),
startOnce: new(sync.Once),
stopOnce: new(sync.Once),
notificationParsers: make(map[scriptHashWithType]NotificationParser),
notificationHandlers: make(map[scriptHashWithType][]Handler),
log: p.Logger,