go.uber.org/yarpc@v1.72.1/peer/hashring32/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 hashring32 22 23 import ( 24 "time" 25 26 "go.uber.org/net/metrics" 27 "go.uber.org/yarpc/api/peer" 28 "go.uber.org/yarpc/peer/hashring32/internal/farmhashring" 29 "go.uber.org/yarpc/yarpcconfig" 30 "go.uber.org/zap" 31 ) 32 33 // Config is the configuration object for hashring32yarpc 34 type Config struct { 35 // OffsetHeader allows clients to pass in a header to adjust to offset value 36 // in the Choose function. 37 OffsetHeader string `config:"offsetHeader"` 38 39 // OffsetGeneratorValue allows clients to generate an offset automatically when using hashring32 40 // 41 // For example, if this value is set to 4, the offset used by hashring32 42 // will be between [0-4]. 43 // 44 // It should be noted that this option will not be used if the option 45 // OffsetHeader is being used. 46 OffsetGeneratorValue int `config:"offsetGeneratorValue"` 47 48 // PeerOverrideHeader allows clients to pass a header containing the shard 49 // identifier for a specific peer to override the destination address for 50 // the outgoing request. 51 // 52 // For example, if the peer list uses addresses to identify peers, 53 // the hash ring will have retained a peer for every known address. 54 // Specifying an address like "127.0.0.1" in the route override header will 55 // deflect the request to that exact peer. 56 // If that peer is not available, the request will continue on to the peer 57 // implied by the shard key. 58 PeerOverrideHeader string `config:"peerOverrideHeader"` 59 60 // AlternateShardKeyHeader allows clients to pass a header containing the shard 61 // identifier for a specific peer to override the destination address for the 62 // outgoing request. 63 AlternateShardKeyHeader string `config:"alternateShardKeyHeader"` 64 65 ReplicaDelimiter string `config:"replicaDelimiter"` 66 67 // NumReplicas specifies the number of replicas to use for each peer in the ring. 68 // Default is 100 69 NumReplicas int `config:"numReplicas"` 70 71 // NumPeersEstimate specifies an estimate for the number of identified peers 72 // the hashring will contain. 73 // 74 // This figure and the number of replicas determines the initial capacity of the ring slice. 75 // Default is 1500 76 NumPeersEstimate int `config:"numPeersEstimate"` 77 78 // DefaultChooseTimeout specifies the deadline to add to Choose calls if not 79 // present. This enables calls without deadlines, ie streaming, to choose 80 // peers without waiting indefinitely. 81 DefaultChooseTimeout *time.Duration `config:"defaultChooseTimeout"` 82 } 83 84 // Spec returns a configuration specification for the hashed peer list 85 // implementation, making it possible to select peer based on a specified hashing 86 // function. 87 func Spec(logger *zap.Logger, meter *metrics.Scope) yarpcconfig.PeerListSpec { 88 // TODO thread meter through list options to abstract list metrics. 89 90 return yarpcconfig.PeerListSpec{ 91 Name: "hashring32", 92 BuildPeerList: func(c Config, t peer.Transport, k *yarpcconfig.Kit) (peer.ChooserList, error) { 93 opts := []Option{ 94 OffsetHeader(c.OffsetHeader), 95 ReplicaDelimiter(c.ReplicaDelimiter), 96 PeerOverrideHeader(c.PeerOverrideHeader), 97 AlternateShardKeyHeader(c.AlternateShardKeyHeader), 98 Logger(logger), 99 } 100 101 if c.DefaultChooseTimeout != nil { 102 opts = append(opts, DefaultChooseTimeout(*c.DefaultChooseTimeout)) 103 } 104 105 if c.NumReplicas != 0 { 106 opts = append(opts, NumReplicas(c.NumReplicas)) 107 } 108 109 if c.NumPeersEstimate != 0 { 110 opts = append(opts, NumPeersEstimate(c.NumPeersEstimate)) 111 } 112 113 if c.OffsetGeneratorValue != 0 && c.OffsetHeader == "" { 114 opts = append(opts, OffsetGeneratorValue(c.OffsetGeneratorValue)) 115 } 116 117 return New( 118 t, 119 farmhashring.Fingerprint32, 120 opts..., 121 ), nil 122 }, 123 } 124 }