go.uber.org/yarpc@v1.72.1/api/peer/list.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 peer
    22  
    23  import (
    24  	"context"
    25  
    26  	"go.uber.org/yarpc/api/transport"
    27  )
    28  
    29  // Chooser is a collection of Peers. Outbounds request peers from the
    30  // peer.Chooser to determine where to send requests.
    31  // The chooser is responsible for managing the lifecycle of any retained peers.
    32  type Chooser interface {
    33  	transport.Lifecycle
    34  
    35  	// Choose a Peer for the next call, block until a peer is available (or timeout)
    36  	Choose(context.Context, *transport.Request) (peer Peer, onFinish func(error), err error)
    37  }
    38  
    39  // List listens to adds and removes of Peers from a peer list updater.
    40  // A Chooser will implement the List interface in order to receive
    41  // updates to the list of Peers it is keeping track of.
    42  type List interface {
    43  	// Update performs the additions and removals to the Peer List
    44  	Update(updates ListUpdates) error
    45  }
    46  
    47  // ListUpdates specifies the updates to be made to a List
    48  type ListUpdates struct {
    49  	// Additions are the identifiers that should be added to the list
    50  	Additions []Identifier
    51  
    52  	// Removals are the identifiers that should be removed to the list
    53  	Removals []Identifier
    54  }
    55  
    56  // ChooserList is both a Chooser and a List, useful for expressing both
    57  // capabilities of a single instance.
    58  type ChooserList interface {
    59  	Chooser
    60  	List
    61  }
    62  
    63  // ListImplementation is a collection of available peers, with its own
    64  // subscribers for peer status change notifications.
    65  // The available peer list encapsulates the logic for selecting from among
    66  // available peers, whereas a ChooserList is responsible for retaining,
    67  // releasing, and monitoring peer availability.
    68  // Use "go.uber.org/yarpc/peer/peerlist".List in conjunction with a
    69  // ListImplementation to produce a "go.uber.org/yarpc/api/peer".List.
    70  //
    71  // peerlist.List and ListImplementation compose well with sharding schemes the
    72  // degenerate to returning the only available peer.
    73  //
    74  // The peerlist.List calls Add, Remove, and Choose under a write lock so the
    75  // implementation is free to perform mutations on its own data without locks.
    76  //
    77  // Deprecated in favor of "go.uber.org/yarpc/peer/peerlist/v2".Implementation.
    78  type ListImplementation interface {
    79  	transport.Lifecycle
    80  
    81  	Add(StatusPeer) Subscriber
    82  	Remove(StatusPeer, Subscriber)
    83  	// Choose must return an available peer under a list read lock, so must
    84  	// not block.
    85  	Choose(context.Context, *transport.Request) StatusPeer
    86  }
    87  
    88  // Binder is a callback for peer.Bind that accepts a peer list and binds it to
    89  // a peer list updater for the duration of the returned peer list updater.
    90  // The peer list updater must implement the lifecycle interface, and start and
    91  // stop updates over that lifecycle.
    92  // The binder must not block on updating the list, because update may block
    93  // until the peer list has started.
    94  // The binder must return a peer list updater that will begin updating when it
    95  // starts, and stop updating when it stops.
    96  type Binder func(List) transport.Lifecycle