go.uber.org/yarpc@v1.72.1/peer/randpeer/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 randpeer
    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  	defaultChooseTimeout *time.Duration
    40  	logger               *zap.Logger
    41  }
    42  
    43  var defaultListOptions = listOptions{
    44  	capacity: 10,
    45  }
    46  
    47  // ListOption customizes the behavior of a random 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  // DefaultChooseTimeout specifies the default timeout to add to 'Choose' calls
    99  // without context deadlines. This prevents long-lived streams from setting
   100  // calling deadlines.
   101  //
   102  // Defaults to 500ms.
   103  func DefaultChooseTimeout(timeout time.Duration) ListOption {
   104  	return listOptionFunc(func(c *listOptions) {
   105  		c.defaultChooseTimeout = &timeout
   106  	})
   107  }
   108  
   109  // New creates a new random peer list.
   110  func New(transport peer.Transport, opts ...ListOption) *List {
   111  	options := defaultListOptions
   112  	for _, opt := range opts {
   113  		opt.apply(&options)
   114  	}
   115  
   116  	if options.source == nil {
   117  		options.source = rand.NewSource(time.Now().UnixNano())
   118  	}
   119  
   120  	plOpts := []abstractlist.Option{
   121  		abstractlist.Capacity(options.capacity),
   122  		abstractlist.NoShuffle(),
   123  	}
   124  
   125  	if options.logger != nil {
   126  		plOpts = append(plOpts, abstractlist.Logger(options.logger))
   127  	}
   128  	if options.failFast {
   129  		plOpts = append(plOpts, abstractlist.FailFast())
   130  	}
   131  	if options.defaultChooseTimeout != nil {
   132  		plOpts = append(plOpts, abstractlist.DefaultChooseTimeout(*options.defaultChooseTimeout))
   133  	}
   134  
   135  	return &List{
   136  		list: abstractlist.New(
   137  			"random",
   138  			transport,
   139  			newRandomList(options.capacity, options.source),
   140  			plOpts...,
   141  		),
   142  	}
   143  }
   144  
   145  // List is a PeerList that rotates which peers are to be selected randomly
   146  type List struct {
   147  	list *abstractlist.List
   148  }
   149  
   150  // Start causes the peer list to start.
   151  //
   152  // Starting will retain all peers that have been added but not removed
   153  // the first time it is called.
   154  //
   155  // Start may be called any number of times and in any order in relation to Stop
   156  // but will only cause the list to start the first time, and only if it has not
   157  // already been stopped.
   158  func (l *List) Start() error {
   159  	return l.list.Start()
   160  }
   161  
   162  // Stop causes the peer list to stop.
   163  //
   164  // Stopping will release all retained peers to the underlying transport.
   165  //
   166  // Stop may be called any number of times and in order in relation to Start but
   167  // will only cause the list to stop the first time, and only if it has
   168  // previously been started.
   169  func (l *List) Stop() error {
   170  	return l.list.Stop()
   171  }
   172  
   173  // IsRunning returns whether the list has started and not yet stopped.
   174  func (l *List) IsRunning() bool {
   175  	return l.list.IsRunning()
   176  }
   177  
   178  // Choose returns a peer, suitable for sending a request.
   179  //
   180  // The peer is not guaranteed to be connected and available, but the peer list
   181  // makes every attempt to ensure this and minimize the probability that a
   182  // chosen peer will fail to carry a request.
   183  func (l *List) Choose(ctx context.Context, req *transport.Request) (peer peer.Peer, onFinish func(error), err error) {
   184  	return l.list.Choose(ctx, req)
   185  }
   186  
   187  // Update may add and remove logical peers in the list.
   188  //
   189  // The peer list uses a transport to obtain a physical peer for each logical
   190  // peer.
   191  // The transport is responsible for informing the peer list whether the peer is
   192  // available or unavailable, but cannot guarantee that the peer will still be
   193  // available after it is chosen.
   194  func (l *List) Update(updates peer.ListUpdates) error {
   195  	return l.list.Update(updates)
   196  }
   197  
   198  // NotifyStatusChanged forwards a status change notification to an individual
   199  // peer in the list.
   200  //
   201  // This satisfies the peer.Subscriber interface and should only be used to
   202  // send notifications in tests.
   203  // The list's RetainPeer and ReleasePeer methods deal with an individual
   204  // peer.Subscriber instance for each peer in the list, avoiding a map lookup.
   205  func (l *List) NotifyStatusChanged(pid peer.Identifier) {
   206  	l.list.NotifyStatusChanged(pid)
   207  }
   208  
   209  // Introspect reveals information about the list to the internal YARPC
   210  // introspection system.
   211  func (l *List) Introspect() introspection.ChooserStatus {
   212  	return l.list.Introspect()
   213  }
   214  
   215  // Peers produces a slice of all retained peers.
   216  func (l *List) Peers() []peer.StatusPeer {
   217  	return l.list.Peers()
   218  }