| Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014 gRPC authors. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | // Package peer defines various peer information associated with RPCs and |
| 20 | // corresponding utils. |
| 21 | package peer |
| 22 | |
| 23 | import ( |
| 24 | "context" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 25 | "fmt" |
| Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 26 | "net" |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 27 | "strings" |
| Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 28 | |
| 29 | "google.golang.org/grpc/credentials" |
| 30 | ) |
| 31 | |
| 32 | // Peer contains the information of the peer for an RPC, such as the address |
| 33 | // and authentication information. |
| 34 | type Peer struct { |
| 35 | // Addr is the peer address. |
| 36 | Addr net.Addr |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 37 | // LocalAddr is the local address. |
| 38 | LocalAddr net.Addr |
| Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 39 | // AuthInfo is the authentication information of the transport. |
| 40 | // It is nil if there is no transport security being used. |
| 41 | AuthInfo credentials.AuthInfo |
| 42 | } |
| 43 | |
| Abhay Kumar | 40252eb | 2025-10-13 13:25:53 +0000 | [diff] [blame] | 44 | // String ensures the Peer types implements the Stringer interface in order to |
| 45 | // allow to print a context with a peerKey value effectively. |
| 46 | func (p *Peer) String() string { |
| 47 | if p == nil { |
| 48 | return "Peer<nil>" |
| 49 | } |
| 50 | sb := &strings.Builder{} |
| 51 | sb.WriteString("Peer{") |
| 52 | if p.Addr != nil { |
| 53 | fmt.Fprintf(sb, "Addr: '%s', ", p.Addr.String()) |
| 54 | } else { |
| 55 | fmt.Fprintf(sb, "Addr: <nil>, ") |
| 56 | } |
| 57 | if p.LocalAddr != nil { |
| 58 | fmt.Fprintf(sb, "LocalAddr: '%s', ", p.LocalAddr.String()) |
| 59 | } else { |
| 60 | fmt.Fprintf(sb, "LocalAddr: <nil>, ") |
| 61 | } |
| 62 | if p.AuthInfo != nil { |
| 63 | fmt.Fprintf(sb, "AuthInfo: '%s'", p.AuthInfo.AuthType()) |
| 64 | } else { |
| 65 | fmt.Fprintf(sb, "AuthInfo: <nil>") |
| 66 | } |
| 67 | sb.WriteString("}") |
| 68 | |
| 69 | return sb.String() |
| 70 | } |
| 71 | |
| Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 72 | type peerKey struct{} |
| 73 | |
| 74 | // NewContext creates a new context with peer information attached. |
| 75 | func NewContext(ctx context.Context, p *Peer) context.Context { |
| 76 | return context.WithValue(ctx, peerKey{}, p) |
| 77 | } |
| 78 | |
| 79 | // FromContext returns the peer information in ctx if it exists. |
| 80 | func FromContext(ctx context.Context) (p *Peer, ok bool) { |
| 81 | p, ok = ctx.Value(peerKey{}).(*Peer) |
| 82 | return |
| 83 | } |