Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wallet): add timeout client connection #1396

Merged
merged 9 commits into from
Jul 8, 2024
10 changes: 10 additions & 0 deletions cmd/wallet/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"time"

"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/wallet"
"github.com/spf13/cobra"
Expand All @@ -10,6 +12,7 @@ var (
pathOpt *string
offlineOpt *bool
serverAddrOpt *string
timeoutOpt *int
)

func addPasswordOption(c *cobra.Command) *string {
Expand All @@ -27,6 +30,8 @@ func openWallet() (*wallet.Wallet, error) {
wlt.SetServerAddr(*serverAddrOpt)
}

wlt.SetClientTimeout(time.Duration(*timeoutOpt) * time.Second)

return wlt, err
}

Expand All @@ -44,6 +49,11 @@ func main() {
cmd.PactusDefaultWalletPath(cmd.PactusDefaultHomeDir()), "the path to the wallet file")
offlineOpt = rootCmd.PersistentFlags().Bool("offline", false, "offline mode")
serverAddrOpt = rootCmd.PersistentFlags().String("server", "", "server gRPC address")
timeoutOpt = rootCmd.PersistentFlags().Int("timeout", 1,
"specifies the timeout duration for the client connection in seconds. "+
"this timeout determines how long the client will attempt to establish a connection with the server "+
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
"before failing with an error. Adjust this value based on network conditions and server response times "+
"to ensure a balance between responsiveness and reliability.")

buildCreateCmd(rootCmd)
buildRecoverCmd(rootCmd)
Expand Down
30 changes: 26 additions & 4 deletions wallet/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/hex"
"errors"
"net"
"time"

"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/types/amount"
Expand All @@ -20,15 +22,17 @@ type grpcClient struct {
ctx context.Context
servers []string
conn *grpc.ClientConn
timeout time.Duration
blockchainClient pactus.BlockchainClient
transactionClient pactus.TransactionClient
}

func newGrpcClient() *grpcClient {
ctx := context.WithoutCancel(context.Background())
ctx := context.Background()

return &grpcClient{
ctx: ctx,
timeout: time.Second * 1,
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
conn: nil,
blockchainClient: nil,
transactionClient: nil,
Expand All @@ -39,16 +43,30 @@ func (c *grpcClient) SetServerAddrs(servers []string) {
c.servers = servers
}

func (c *grpcClient) SetTimeoutConnection(timeout time.Duration) {
if timeout > 0 {
c.timeout = timeout
}
}

func (c *grpcClient) connect() error {
if c.conn != nil {
return nil
}

for _, server := range c.servers {
conn, err := grpc.NewClient(server,
grpc.WithTransportCredentials(insecure.NewCredentials()))
opts := make([]grpc.DialOption, 0)
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))

if len(c.servers) > 1 {
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
opts = append(opts, grpc.WithContextDialer(func(_ context.Context, s string) (net.Conn, error) {
return net.DialTimeout("tcp", s, c.timeout)
}))
}

conn, err := grpc.NewClient(server, opts...)
if err != nil {
continue
return err
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
}

blockchainClient := pactus.NewBlockchainClient(conn)
Expand All @@ -58,6 +76,10 @@ func (c *grpcClient) connect() error {
_, err = blockchainClient.GetBlockchainInfo(c.ctx,
&pactus.GetBlockchainInfoRequest{})
if err != nil {
if err := conn.Close(); err != nil {
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
return err
}

continue
}

Expand Down
4 changes: 4 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (w *Wallet) SetServerAddr(addr string) {
w.grpcClient.SetServerAddrs([]string{addr})
}

func (w *Wallet) SetClientTimeout(timeout time.Duration) {
Ja7ad marked this conversation as resolved.
Show resolved Hide resolved
w.grpcClient.SetTimeoutConnection(timeout)
}

func (w *Wallet) Name() string {
return path.Base(w.path)
}
Expand Down
Loading