blob: 61ef468649a5595f329e8fa6aa05ef80dd188ba4 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// Copyright 2015 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rafthttp
16
17import (
18 "net/url"
19 "sync"
20
21 "go.etcd.io/etcd/pkg/types"
22)
23
24type urlPicker struct {
25 mu sync.Mutex // guards urls and picked
26 urls types.URLs
27 picked int
28}
29
30func newURLPicker(urls types.URLs) *urlPicker {
31 return &urlPicker{
32 urls: urls,
33 }
34}
35
36func (p *urlPicker) update(urls types.URLs) {
37 p.mu.Lock()
38 defer p.mu.Unlock()
39 p.urls = urls
40 p.picked = 0
41}
42
43func (p *urlPicker) pick() url.URL {
44 p.mu.Lock()
45 defer p.mu.Unlock()
46 return p.urls[p.picked]
47}
48
49// unreachable notices the picker that the given url is unreachable,
50// and it should use other possible urls.
51func (p *urlPicker) unreachable(u url.URL) {
52 p.mu.Lock()
53 defer p.mu.Unlock()
54 if u == p.urls[p.picked] {
55 p.picked = (p.picked + 1) % len(p.urls)
56 }
57}