go.uber.org/yarpc@v1.72.1/peer/roundrobin/config.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 roundrobin
    22  
    23  import (
    24  	"fmt"
    25  	"time"
    26  
    27  	"go.uber.org/yarpc/api/peer"
    28  	"go.uber.org/yarpc/yarpcconfig"
    29  	"go.uber.org/yarpc/yarpcerrors"
    30  )
    31  
    32  // Configuration descripes how to build a round-robin peer list.
    33  type Configuration struct {
    34  	Capacity *int `config:"capacity"`
    35  	FailFast bool `config:"failFast"`
    36  	// DefaultChooseTimeout specifies the deadline to add to Choose calls if not
    37  	// present. This enables calls without deadlines, ie streaming, to choose
    38  	// peers without waiting indefinitely.
    39  	DefaultChooseTimeout *time.Duration `config:"defaultChooseTimeout"`
    40  }
    41  
    42  // Spec returns a configuration specification for the round-robin peer list
    43  // implementation, making it possible to select the least recently chosen peer
    44  // with transports that use outbound peer list configuration (like HTTP).
    45  //
    46  //  cfg := yarpcconfig.New()
    47  //  cfg.MustRegisterPeerList(roundrobin.Spec())
    48  //
    49  // This enables the round-robin peer list:
    50  //
    51  //  outbounds:
    52  //    otherservice:
    53  //      unary:
    54  //        http:
    55  //          url: https://host:port/rpc
    56  //          round-robin:
    57  //            peers:
    58  //              - 127.0.0.1:8080
    59  //              - 127.0.0.1:8081
    60  //
    61  // Other than a specific peer or peers list, use any peer list updater
    62  // registered with a yarpc Configurator.
    63  // The configuration allows for alternative initial allocation capacity and a
    64  // fail-fast option.
    65  // With fail-fast enabled, the peer list will return an error immediately if no
    66  // peers are available (connected) at the time the request is sent.
    67  // The default choose timeout enables calls without deadlines, ie streaming, to
    68  // choose peers without waiting indefinitely.
    69  //
    70  //  round-robin:
    71  //    peers:
    72  //      - 127.0.0.1:8080
    73  //    capacity: 1
    74  //    failFast: true
    75  //    defaultChooseTimeout: 1s
    76  func Spec() yarpcconfig.PeerListSpec {
    77  	return SpecWithOptions()
    78  }
    79  
    80  // SpecWithOptions accepts additional list constructor options.
    81  func SpecWithOptions(options ...ListOption) yarpcconfig.PeerListSpec {
    82  	return yarpcconfig.PeerListSpec{
    83  		Name: "round-robin",
    84  		BuildPeerList: func(cfg Configuration, t peer.Transport, k *yarpcconfig.Kit) (peer.ChooserList, error) {
    85  			opts := make([]ListOption, 0, len(options)+3)
    86  
    87  			opts = append(opts, options...)
    88  
    89  			if cfg.Capacity != nil {
    90  				if *cfg.Capacity <= 0 {
    91  					return nil, yarpcerrors.Newf(yarpcerrors.CodeInvalidArgument,
    92  						fmt.Sprintf("Capacity must be greater than 0. Got: %d.", *cfg.Capacity))
    93  				}
    94  				opts = append(opts, Capacity(*cfg.Capacity))
    95  			}
    96  			if cfg.FailFast {
    97  				opts = append(opts, FailFast())
    98  			}
    99  			if cfg.DefaultChooseTimeout != nil {
   100  				opts = append(opts, DefaultChooseTimeout(*cfg.DefaultChooseTimeout))
   101  			}
   102  			return New(t, opts...), nil
   103  		},
   104  	}
   105  }