Skip to content

Commit

Permalink
fix: remove breaks in relay loop that could cause permanent disconnec…
Browse files Browse the repository at this point in the history
…tion from relay (#869)

fix: breaks in relay loop that could cause permanent disconnection from relay
  • Loading branch information
rolznz authored Dec 16, 2024
1 parent d51db84 commit 2738166
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ func (svc *service) startNostr(ctx context.Context) error {
waitToReconnectSeconds := 0
var createAppEventListener events.EventSubscriber
for i := 0; ; i++ {

// wait for a delay if any before retrying
if waitToReconnectSeconds > 0 {
contextCancelled := false

select {
case <-ctx.Done(): //context cancelled
logger.Logger.Info("service context cancelled while waiting for retry")
contextCancelled = true
case <-time.After(time.Duration(waitToReconnectSeconds) * time.Second): //timeout
}
if contextCancelled {
break
}
contextCancelled := false

select {
case <-ctx.Done(): // application service context cancelled
logger.Logger.Info("service context cancelled")
contextCancelled = true
case <-time.After(time.Duration(waitToReconnectSeconds) * time.Second): //timeout
}
if contextCancelled {
break
}

closeRelay(relay)
Expand Down Expand Up @@ -123,7 +120,6 @@ func (svc *service) startNostr(ctx context.Context) error {
logger.Logger.WithError(err).Error("Got an error from the relay while listening to subscription.")
continue
}
break
}
select {
case <-ctx.Done():
Expand All @@ -135,10 +131,7 @@ func (svc *service) startNostr(ctx context.Context) error {
} else {
logger.Logger.Error("Relay context cancelled, but no connection error...trying to reconnect")
}
continue
}
//err being nil means that the context was canceled and we should exit the program.
break
}
closeRelay(relay)
logger.Logger.Info("Relay subroutine ended")
Expand Down

0 comments on commit 2738166

Please sign in to comment.