go.uber.org/yarpc@v1.72.1/peer/tworandomchoices/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 tworandomchoices
    22  
    23  import (
    24  	"context"
    25  	"math/rand"
    26  	"time"
    27  
    28  	"go.uber.org/yarpc/api/peer"
    29  	"go.uber.org/yarpc/api/transport"
    30  	"go.uber.org/yarpc/api/x/introspection"
    31  	"go.uber.org/yarpc/peer/abstractlist"
    32  	"go.uber.org/zap"
    33  )
    34  
    35  type listOptions struct {
    36  	capacity int
    37  	source   rand.Source
    38  	failFast bool
    39  	logger   *zap.Logger
    40  }
    41  
    42  var defaultListOptions = listOptions{
    43  	capacity: 10,
    44  }
    45  
    46  // ListOption customizes the behavior of a fewest pending of two random peers
    47  // list.
    48  type ListOption interface {
    49  	apply(*listOptions)
    50  }
    51  
    52  type listOptionFunc func(*listOptions)
    53  
    54  func (f listOptionFunc) apply(options *listOptions) { f(options) }
    55  
    56  // Capacity specifies the default capacity of the underlying
    57  // data structures for this list.
    58  //
    59  // Defaults to 10.
    60  func Capacity(capacity int) ListOption {
    61  	return listOptionFunc(func(options *listOptions) {
    62  		options.capacity = capacity
    63  	})
    64  }
    65  
    66  // Seed specifies the seed for generating random choices.
    67  func Seed(seed int64) ListOption {
    68  	return listOptionFunc(func(options *listOptions) {
    69  		options.source = rand.NewSource(seed)
    70  	})
    71  }
    72  
    73  // Source is a source of randomness for the peer list.
    74  func Source(source rand.Source) ListOption {
    75  	return listOptionFunc(func(options *listOptions) {
    76  		options.source = source
    77  	})
    78  }
    79  
    80  // FailFast indicates that the peer list should not wait for a peer to become
    81  // available when choosing a peer.
    82  //
    83  // This option is preferrable when the better failure mode is to retry from the
    84  // origin, since another proxy instance might already have a connection.
    85  func FailFast() ListOption {
    86  	return listOptionFunc(func(options *listOptions) {
    87  		options.failFast = true
    88  	})
    89  }
    90  
    91  // Logger specifies a logger.
    92  func Logger(logger *zap.Logger) ListOption {
    93  	return listOptionFunc(func(options *listOptions) {
    94  		options.logger = logger
    95  	})
    96  }
    97  
    98  // New creates a new fewest pending requests of two random peers peer list.
    99  func New(transport peer.Transport, opts ...ListOption) *List {
   100  	options := defaultListOptions
   101  	for _, opt := range opts {
   102  		opt.apply(&options)
   103  	}
   104  
   105  	if options.source == nil {
   106  		options.source = rand.NewSource(time.Now().UnixNano())
   107  	}
   108  
   109  	plOpts := []abstractlist.Option{
   110  		abstractlist.Capacity(options.capacity),
   111  		abstractlist.NoShuffle(),
   112  	}
   113  
   114  	if options.logger != nil {
   115  		plOpts = append(plOpts, abstractlist.Logger(options.logger))
   116  	}
   117  	if options.failFast {
   118  		plOpts = append(plOpts, abstractlist.FailFast())
   119  	}
   120  
   121  	return &List{
   122  		list: abstractlist.New(
   123  			"two-random-choices",
   124  			transport,
   125  			newTwoRandomChoicesList(options.capacity, options.source),
   126  			plOpts...,
   127  		),
   128  	}
   129  }
   130  
   131  // List is a PeerList that rotates which peers are to be selected randomly
   132  type List struct {
   133  	list *abstractlist.List
   134  }
   135  
   136  // Start causes the peer list to start.
   137  //
   138  // Starting will retain all peers that have been added but not removed
   139  // the first time it is called.
   140  //
   141  // Start may be called any number of times and in any order in relation to Stop
   142  // but will only cause the list to start the first time, and only if it has not
   143  // already been stopped.
   144  func (l *List) Start() error {
   145  	return l.list.Start()
   146  }
   147  
   148  // Stop causes the peer list to stop.
   149  //
   150  // Stopping will release all retained peers to the underlying transport.
   151  //
   152  // Stop may be called any number of times and in order in relation to Start but
   153  // will only cause the list to stop the first time, and only if it has
   154  // previously been started.
   155  func (l *List) Stop() error {
   156  	return l.list.Stop()
   157  }
   158  
   159  // IsRunning returns whether the list has started and not yet stopped.
   160  func (l *List) IsRunning() bool {
   161  	return l.list.IsRunning()
   162  }
   163  
   164  // Choose returns a peer, suitable for sending a request.
   165  //
   166  // The peer is not guaranteed to be connected and available, but the peer list
   167  // makes every attempt to ensure this and minimize the probability that a
   168  // chosen peer will fail to carry a request.
   169  func (l *List) Choose(ctx context.Context, req *transport.Request) (peer peer.Peer, onFinish func(error), err error) {
   170  	return l.list.Choose(ctx, req)
   171  }
   172  
   173  // Update may add and remove logical peers in the list.
   174  //
   175  // The peer list uses a transport to obtain a physical peer for each logical
   176  // peer.
   177  // The transport is responsible for informing the peer list whether the peer is
   178  // available or unavailable, but cannot guarantee that the peer will still be
   179  // available after it is chosen.
   180  func (l *List) Update(updates peer.ListUpdates) error {
   181  	return l.list.Update(updates)
   182  }
   183  
   184  // NotifyStatusChanged forwards a status change notification to an individual
   185  // peer in the list.
   186  //
   187  // This satisfies the peer.Subscriber interface and should only be used to
   188  // send notifications in tests.
   189  // The list's RetainPeer and ReleasePeer methods deal with an individual
   190  // peer.Subscriber instance for each peer in the list, avoiding a map lookup.
   191  func (l *List) NotifyStatusChanged(pid peer.Identifier) {
   192  	l.list.NotifyStatusChanged(pid)
   193  }
   194  
   195  // Introspect reveals information about the list to the internal YARPC
   196  // introspection system.
   197  func (l *List) Introspect() introspection.ChooserStatus {
   198  	return l.list.Introspect()
   199  }
   200  
   201  // Peers produces a slice of all retained peers.
   202  func (l *List) Peers() []peer.StatusPeer {
   203  	return l.list.Peers()
   204  }