Skip to content

Commit

Permalink
fix websocket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Borja Aranda committed Sep 13, 2023
1 parent b163a1d commit 08577f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/ethereum/go-ethereum v1.12.2
github.com/gorilla/websocket v1.5.0
github.com/rs/zerolog v1.30.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.16.0
Expand All @@ -21,7 +22,6 @@ require (
github.com/go-stack/stack v1.8.1 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
18 changes: 14 additions & 4 deletions pkg/timelock/timelock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"net"
"net/url"
"os"
"os/signal"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gorilla/websocket"
"github.com/rs/zerolog"
"github.com/smartcontractkit/timelock-worker/pkg/timelock/contract"
)
Expand Down Expand Up @@ -77,8 +79,17 @@ func NewTimelockWorker(nodeURL, timelockAddress, callProxyAddress, privateKey st
return nil, fmt.Errorf("the provided private key is not valid: got %s", privateKey)
}

// All variables provided are correct, start allocating new structures.
client, err := rpc.Dial(nodeURL)
// Provide a ws dialer customized to maintain the connection alive.
dialer := rpc.WithWebsocketDialer(websocket.Dialer{
HandshakeTimeout: 45 * time.Second,
NetDial: (&net.Dialer{
Timeout: 45 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
})

// Dial the RPC with the custom dialer.
client, err := rpc.DialOptions(context.Background(), nodeURL, dialer)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -235,8 +246,7 @@ func (tw *Worker) Listen(ctx context.Context) error {
// Check if the error is not nil, because sub.Unsubscribe will
// signal the channel sub.Err() to close it, leading to false nil errors.
if err != nil {
tw.logger.Info().Msgf("subscription: %s", err.Error())
loop = false
tw.logger.Error().Msgf("subscription: %s", err.Error())
}

case signal := <-stopCh:
Expand Down

0 comments on commit 08577f7

Please sign in to comment.