Skip to content

Commit

Permalink
handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcb9ff9 committed Mar 17, 2023
1 parent 58420af commit e3abdee
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
9 changes: 4 additions & 5 deletions network/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func interceptor(
)
}

func (g *GrpcStream) Client(ctx context.Context, stream network.Stream) *grpc.ClientConn {
func (g *GrpcStream) Client(ctx context.Context, stream network.Stream) (*grpc.ClientConn, error) {
return WrapClient(ctx, stream)
}

Expand Down Expand Up @@ -128,7 +128,7 @@ func (g *GrpcStream) Close() error {

// --- conn ---

func WrapClient(ctx context.Context, s network.Stream) *grpc.ClientConn {
func WrapClient(ctx context.Context, s network.Stream) (*grpc.ClientConn, error) {
opts := grpc.WithContextDialer(func(ctx context.Context, peerIdStr string) (net.Conn, error) {
return &streamConn{s}, nil
})
Expand All @@ -145,11 +145,10 @@ func WrapClient(ctx context.Context, s network.Stream) *grpc.ClientConn {
opts)

if err != nil {
// TODO: this should not fail at all
panic(err)
return nil, err
}

return conn
return conn, nil
}

// streamConn represents a net.Conn wrapped to be compatible with net.conn
Expand Down
2 changes: 1 addition & 1 deletion network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type Network interface {
}

type Protocol interface {
Client(context.Context, network.Stream) *rawGrpc.ClientConn
Client(context.Context, network.Stream) (*rawGrpc.ClientConn, error)
Handler() func(network.Stream)
}

Expand Down
10 changes: 8 additions & 2 deletions network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,14 @@ func (s *DefaultServer) NewProtoConnection(

s.logger.Debug("create grpc client", "protocol", protocol, "peer", peerID)

// don't need context.WithTimeout, rpc client is fake
return p.Client(ctx, stream), nil
clt, err := p.Client(ctx, stream)
if err != nil {
s.logger.Error("create grpc client failed", "protocol", protocol, "peer", peerID, "err", err)

return nil, err
}

return clt, nil
}

func (s *DefaultServer) GetMetrics() *Metrics {
Expand Down

0 comments on commit e3abdee

Please sign in to comment.