mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-07 14:46:44 +00:00
kaspawallet: Add timeout to connect to daemon, and print friendly error (#1756)
message if it's not available
This commit is contained in:
parent
f317f51cdd
commit
9bedf84740
@ -1,14 +1,26 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Connect connects to the kaspawalletd server, and returns the client instance
|
// Connect connects to the kaspawalletd server, and returns the client instance
|
||||||
func Connect(address string) (pb.KaspawalletdClient, func(), error) {
|
func Connect(address string) (pb.KaspawalletdClient, func(), error) {
|
||||||
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
|
// Connection is local, so 1 second timeout is sufficient
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
return nil, nil, errors.New("kaspactl daemon is not running, start it with `kaspactl start-daemon`")
|
||||||
|
}
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user