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