-
Notifications
You must be signed in to change notification settings - Fork 100
/
list.go
119 lines (101 loc) · 3.32 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tworandomchoices
import (
"math/rand"
"time"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/peer/peerlist/v2"
)
type listOptions struct {
capacity int
source rand.Source
failFast bool
}
var defaultListOptions = listOptions{
capacity: 10,
}
// ListOption customizes the behavior of a fewest pending of two random peers
// list.
type ListOption interface {
apply(*listOptions)
}
type listOptionFunc func(*listOptions)
func (f listOptionFunc) apply(options *listOptions) { f(options) }
// Capacity specifies the default capacity of the underlying
// data structures for this list.
//
// Defaults to 10.
func Capacity(capacity int) ListOption {
return listOptionFunc(func(options *listOptions) {
options.capacity = capacity
})
}
// Seed specifies the seed for generating random choices.
func Seed(seed int64) ListOption {
return listOptionFunc(func(options *listOptions) {
options.source = rand.NewSource(seed)
})
}
// Source is a source of randomness for the peer list.
func Source(source rand.Source) ListOption {
return listOptionFunc(func(options *listOptions) {
options.source = source
})
}
// FailFast indicates that the peer list should not wait for a peer to become
// available when choosing a peer.
//
// This option is preferrable when the better failure mode is to retry from the
// origin, since another proxy instance might already have a connection.
func FailFast() ListOption {
return listOptionFunc(func(options *listOptions) {
options.failFast = true
})
}
// New creates a new fewest pending requests of two random peers peer list.
func New(transport peer.Transport, opts ...ListOption) *List {
options := defaultListOptions
for _, opt := range opts {
opt.apply(&options)
}
if options.source == nil {
options.source = rand.NewSource(time.Now().UnixNano())
}
plOpts := []peerlist.ListOption{
peerlist.Capacity(options.capacity),
peerlist.NoShuffle(),
}
if options.failFast {
plOpts = append(plOpts, peerlist.FailFast())
}
return &List{
List: peerlist.New(
"two-random-choices",
transport,
newTwoRandomChoicesList(options.capacity, options.source),
plOpts...,
),
}
}
// List is a PeerList that rotates which peers are to be selected randomly
type List struct {
*peerlist.List
}