Skip to content

Commit

Permalink
node: Fix defer (wormhole-foundation#2334)
Browse files Browse the repository at this point in the history
* node: Fix defer

* node/pkg/wathcers: fix error
  • Loading branch information
panoel authored Feb 6, 2023
1 parent 3b8de17 commit d8f4740
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions node/pkg/watchers/solana/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,32 @@ func (s *SolanaWatcher) SetupWebSocket(ctx context.Context) error {
case <-ctx.Done():
return nil
default:
rCtx, cancel := context.WithTimeout(ctx, time.Second*300) // 5 minute
defer cancel()

if _, msg, err := ws.Read(rCtx); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
// When a websocket context times out, it closes the websocket... This means we have to re-subscribe
ws.Close(websocket.StatusNormalClosure, "")
err, ws = s.SetupSubscription(ctx)
if err != nil {
return err
if err := func() error {

rCtx, cancel := context.WithTimeout(ctx, time.Second*300) // 5 minute
defer cancel()

if _, msg, err := ws.Read(rCtx); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
// When a websocket context times out, it closes the websocket... This means we have to re-subscribe
ws.Close(websocket.StatusNormalClosure, "")
err, ws = s.SetupSubscription(ctx)
if err != nil {
return err
}
return nil
}
continue
}

logger.Error(fmt.Sprintf("ReadMessage: '%s'", err.Error()))
if errors.Is(err, io.EOF) {
return err
logger.Error(fmt.Sprintf("ReadMessage: '%s'", err.Error()))
if errors.Is(err, io.EOF) {
return err
}
} else {
s.pumpData <- msg
}
} else {
s.pumpData <- msg
return nil
}(); err != nil {
return err
}
}
}
Expand All @@ -288,7 +294,9 @@ func (s *SolanaWatcher) Run(ctx context.Context) error {
s.errC = make(chan error)
s.pumpData = make(chan []byte)

if s.wsUrl != nil {
useWs := false
if s.wsUrl != nil && *s.wsUrl != "" {
useWs = true
err := s.SetupWebSocket(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -347,7 +355,7 @@ func (s *SolanaWatcher) Run(ctx context.Context) error {
ContractAddress: contractAddr,
})

if s.wsUrl == nil {
if !useWs {
rangeStart := lastSlot + 1
rangeEnd := slot

Expand Down

0 comments on commit d8f4740

Please sign in to comment.