Skip to content

Commit

Permalink
Merge pull request #6 from plexsysio/tracing
Browse files Browse the repository at this point in the history
chore: peerID from context
  • Loading branch information
aloknerurkar committed Sep 18, 2023
2 parents 9972c0c + a32a673 commit 64a1e9d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ type (
)

// GetPeerID returns the peer id of the remote peer from the context.
func GetPeerID(ctx context.Context) (string, error) {
func GetPeerID(ctx context.Context) (peer.ID, error) {
peer, ok := ctx.Value(peerKey{}).(peer.ID)
if !ok {
return "", errors.New("failed to get peer id from context")
}
return peer.String(), nil
return peer, nil
}

// SetPeerID sets the peer id of the remote peer in the context.
Expand Down Expand Up @@ -153,7 +153,7 @@ func Unary[Req any, Resp any](
return sWrap.WriteMsg(ctx, &pbv1.Message{Body: respBody})
}

ctx := context.WithValue(context.Background(), peerKey{}, stream.Conn().RemotePeer())
ctx := SetPeerID(context.Background(), stream.Conn().RemotePeer())
reqMsg, err := sWrap.ReadMsg(ctx)
if err != nil {
return
Expand Down Expand Up @@ -192,7 +192,7 @@ func UpStream[Req any, Resp any](
sWrap := wrapStream(stream)
defer stream.Close()

ctx := context.WithValue(context.Background(), peerKey{}, stream.Conn().RemotePeer())
ctx := SetPeerID(context.Background(), stream.Conn().RemotePeer())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down Expand Up @@ -328,7 +328,7 @@ func DownStream[Req any, Resp any](
return nil
}

ctx := context.WithValue(context.Background(), peerKey{}, stream.Conn().RemotePeer())
ctx := SetPeerID(context.Background(), stream.Conn().RemotePeer())
reqMsg, err := sWrap.ReadMsg(ctx)
if err != nil {
return
Expand Down Expand Up @@ -368,7 +368,7 @@ func BidirStream[Req any, Resp any](
sWrap := wrapStream(stream)
defer stream.Close()

ctx := context.WithValue(context.Background(), peerKey{}, stream.Conn().RemotePeer())
ctx := SetPeerID(context.Background(), stream.Conn().RemotePeer())
ctx, cancel := context.WithCancel(ctx)
defer cancel()

Expand Down

0 comments on commit 64a1e9d

Please sign in to comment.