Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
fix: better error handling when closing relay connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jun 19, 2024
1 parent 93d70f6 commit 9528d80
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions service/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ func (svc *service) StartNostr(ctx context.Context, encryptionKey string) error
break
}
}
if relay != nil && relay.IsConnected() {
err := relay.Close()
if err != nil {
logger.Logger.WithError(err).Error("Could not close relay connection")
}
}
closeRelay(relay)

//connect to the relay
logger.Logger.Infof("Connecting to the relay: %s", relayUrl)
Expand Down Expand Up @@ -99,13 +94,7 @@ func (svc *service) StartNostr(ctx context.Context, encryptionKey string) error
//err being nil means that the context was canceled and we should exit the program.
break
}
logger.Logger.Info("Disconnecting from relay...")
if relay != nil && relay.IsConnected() {
err := relay.Close()
if err != nil {
logger.Logger.WithError(err).Error("Could not close relay connection")
}
}
closeRelay(relay)
svc.Shutdown()
logger.Logger.Info("Relay subroutine ended")
svc.wg.Done()
Expand Down Expand Up @@ -208,3 +197,20 @@ func (svc *service) launchLNBackend(ctx context.Context, encryptionKey string) e
svc.lnClient = lnClient
return nil
}

func closeRelay(relay *nostr.Relay) {
if relay != nil && relay.IsConnected() {
logger.Logger.Info("Closing relay connection...")
func() {
defer func() {
if r := recover(); r != nil {
logger.Logger.WithField("r", r).Error("Recovered from panic when closing relay")
}
}()
err := relay.Close()
if err != nil {
logger.Logger.WithError(err).Error("Could not close relay connection")
}
}()
}
}

0 comments on commit 9528d80

Please sign in to comment.