Skip to content

Commit

Permalink
added timeout to conn factory
Browse files Browse the repository at this point in the history
  • Loading branch information
0xluk committed Mar 1, 2024
1 parent 01e2965 commit 1bd2b7d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/pkg/errors v0.9.1
github.com/qubic/go-node-connector v0.3.0
github.com/qubic/go-node-connector v0.3.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.61.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/qubic/go-node-connector v0.3.0 h1:jmdge7CK5CGoz7iC7SzKQhN23oLKzo8oLlPzqCMYy9o=
github.com/qubic/go-node-connector v0.3.0/go.mod h1:y0eMsGPY1DFEzz2JovUgEIwBZ/27zoJ8G81nX9yt8xI=
github.com/qubic/go-node-connector v0.3.1 h1:Bbz7ptqY6hd6xl+FCcvTw2KrMCf6048OQK60sikClkQ=
github.com/qubic/go-node-connector v0.3.1/go.mod h1:y0eMsGPY1DFEzz2JovUgEIwBZ/27zoJ8G81nX9yt8xI=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ func do(pool connpool.Pool, fallbackTick, batchSize uint64, ps *store.PebbleStor

err = validateMultiple(conn, fallbackTick, batchSize, ps)
if err != nil {
conn.Close()
if pc, ok := conn.(*connpool.PoolConn); ok {
fmt.Printf("Marking conn: %s unusable\n", pc.Conn.RemoteAddr().String())
pc.MarkUnusable()
pc.Close()
}
return errors.Wrap(err, "validating multiple")
}
log.Printf("Batch completed, continuing to next one")
Expand All @@ -128,16 +132,13 @@ func do(pool connpool.Pool, fallbackTick, batchSize uint64, ps *store.PebbleStor
}

func validateMultiple(conn net.Conn, fallbackStartTick uint64, batchSize uint64, ps *store.PebbleStore) error {
defer conn.Close()

ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()

client, err := qubic.NewClientWithConn(ctx, conn)
if err != nil {
return errors.Wrap(err, "creating qubic client")
}
defer client.Close()

val := validator.NewValidator(client, ps)
tickInfo, err := client.GetTickInfo(ctx)
Expand Down Expand Up @@ -200,7 +201,7 @@ func (cf *connectionFactory) connect() (net.Conn, error) {
return nil, errors.Wrap(err, "getting new random peer")
}
fmt.Printf("connecting to: %s\n", peer)
return net.Dial("tcp", net.JoinHostPort(peer, "21841"))
return net.DialTimeout("tcp", net.JoinHostPort(peer, "21841"), 5*time.Second)
}

type response struct {
Expand Down

0 comments on commit 1bd2b7d

Please sign in to comment.