github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/nphttp2/peer/peer.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package peer
    18  
    19  import (
    20  	"context"
    21  	"net"
    22  )
    23  
    24  type peerKey struct{}
    25  
    26  // Peer contains the information of the peer for an RPC, such as the address
    27  // and authentication information.
    28  type Peer struct {
    29  	// Addr is the peer address.
    30  	Addr net.Addr
    31  }
    32  
    33  // GRPCPeer is used for client to get remote service address
    34  func GRPCPeer(ctx context.Context, p *Peer) context.Context {
    35  	return context.WithValue(ctx, peerKey{}, p)
    36  }
    37  
    38  func GetPeerFromContext(ctx context.Context) (peer *Peer, ok bool) {
    39  	p := ctx.Value(peerKey{})
    40  	if p != nil {
    41  		return p.(*Peer), true
    42  	}
    43  	return nil, false
    44  }