go.uber.org/yarpc@v1.72.1/peer/peerlist/v2/peer.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package peerlist 22 23 import ( 24 "sync" 25 26 "go.uber.org/yarpc/api/peer" 27 ) 28 29 // peerThunk captures a peer and its corresponding subscriber, 30 // and serves as a subscriber by proxy. 31 type peerThunk struct { 32 lock sync.RWMutex 33 list *List 34 id peer.Identifier 35 peer peer.Peer 36 subscriber peer.Subscriber 37 boundOnFinish func(error) 38 } 39 40 func (t *peerThunk) onFinish(error) { 41 t.peer.EndRequest() 42 } 43 44 func (t *peerThunk) Identifier() string { 45 return t.peer.Identifier() 46 } 47 48 func (t *peerThunk) Status() peer.Status { 49 return t.peer.Status() 50 } 51 52 func (t *peerThunk) StartRequest() { 53 t.peer.StartRequest() 54 } 55 56 func (t *peerThunk) EndRequest() { 57 t.peer.EndRequest() 58 } 59 60 // NotifyStatusChanged forwards a status notification to the peer list and to 61 // the underlying identifier chooser list. 62 func (t *peerThunk) NotifyStatusChanged(pid peer.Identifier) { 63 t.list.notifyStatusChanged(pid) 64 65 if s := t.Subscriber(); s != nil { 66 s.NotifyStatusChanged(pid) 67 } 68 } 69 70 // SetSubscriber assigns a subscriber to the subscriber thunk. 71 func (t *peerThunk) SetSubscriber(s peer.Subscriber) { 72 t.lock.Lock() 73 t.subscriber = s 74 t.lock.Unlock() 75 } 76 77 // Subscriber returns the subscriber. 78 func (t *peerThunk) Subscriber() peer.Subscriber { 79 t.lock.RLock() 80 s := t.subscriber 81 t.lock.RUnlock() 82 return s 83 }