go.uber.org/yarpc@v1.72.1/peer/peerlist/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) onStart() {
    41  	t.peer.StartRequest()
    42  }
    43  
    44  func (t *peerThunk) onFinish(error) {
    45  	t.peer.EndRequest()
    46  }
    47  
    48  func (t *peerThunk) Identifier() string {
    49  	return t.peer.Identifier()
    50  }
    51  
    52  func (t *peerThunk) Status() peer.Status {
    53  	return t.peer.Status()
    54  }
    55  
    56  func (t *peerThunk) StartRequest() {
    57  	t.peer.StartRequest()
    58  }
    59  
    60  func (t *peerThunk) EndRequest() {
    61  	t.peer.EndRequest()
    62  }
    63  
    64  // NotifyStatusChanged forwards a status notification to the peer list and to
    65  // the underlying identifier chooser list.
    66  func (t *peerThunk) NotifyStatusChanged(pid peer.Identifier) {
    67  	t.list.notifyStatusChanged(pid)
    68  
    69  	s := t.Subscriber()
    70  	if s != nil {
    71  		s.NotifyStatusChanged(pid)
    72  	}
    73  }
    74  
    75  // SetSubscriber assigns a subscriber to the subscriber thunk.
    76  func (t *peerThunk) SetSubscriber(s peer.Subscriber) {
    77  	t.lock.Lock()
    78  	t.subscriber = s
    79  	t.lock.Unlock()
    80  }
    81  
    82  // Subscriber returns the subscriber.
    83  func (t *peerThunk) Subscriber() peer.Subscriber {
    84  	t.lock.RLock()
    85  	s := t.subscriber
    86  	t.lock.RUnlock()
    87  	return s
    88  }