Skip to content

Commit 42b67f4

Browse files
committed
Merge pull request #63 from pierrre/master
use passed options
2 parents d995f8a + 8cc54b4 commit 42b67f4

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

http.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ type HTTPPool struct {
4646
// If nil, the client uses http.DefaultTransport.
4747
Transport func(Context) http.RoundTripper
4848

49-
// base path including leading and trailing slash, e.g. "/_groupcache/"
50-
basePath string
51-
5249
// this peer's base URL, e.g. "https://example.net:8000"
5350
self string
5451

52+
// opts specifies the options.
53+
opts HTTPPoolOptions
54+
5555
mu sync.Mutex // guards peers and httpGetters
5656
peers *consistenthash.Map
5757
httpGetters map[string]*httpGetter // keyed by e.g. "http://10.0.0.2:8008"
@@ -78,7 +78,7 @@ type HTTPPoolOptions struct {
7878
// for example "http://example.net:8000".
7979
func NewHTTPPool(self string) *HTTPPool {
8080
p := NewHTTPPoolOpts(self, nil)
81-
http.Handle(p.basePath, p)
81+
http.Handle(p.opts.BasePath, p)
8282
return p
8383
}
8484

@@ -93,23 +93,21 @@ func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool {
9393
}
9494
httpPoolMade = true
9595

96-
opts := HTTPPoolOptions{}
96+
p := &HTTPPool{
97+
self: self,
98+
httpGetters: make(map[string]*httpGetter),
99+
}
97100
if o != nil {
98-
opts = *o
101+
p.opts = *o
99102
}
100-
if opts.BasePath == "" {
101-
opts.BasePath = defaultBasePath
103+
if p.opts.BasePath == "" {
104+
p.opts.BasePath = defaultBasePath
102105
}
103-
if opts.Replicas == 0 {
104-
opts.Replicas = defaultReplicas
106+
if p.opts.Replicas == 0 {
107+
p.opts.Replicas = defaultReplicas
105108
}
109+
p.peers = consistenthash.New(p.opts.Replicas, p.opts.HashFn)
106110

107-
p := &HTTPPool{
108-
basePath: opts.BasePath,
109-
self: self,
110-
peers: consistenthash.New(opts.Replicas, opts.HashFn),
111-
httpGetters: make(map[string]*httpGetter),
112-
}
113111
RegisterPeerPicker(func() PeerPicker { return p })
114112
return p
115113
}
@@ -120,11 +118,11 @@ func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool {
120118
func (p *HTTPPool) Set(peers ...string) {
121119
p.mu.Lock()
122120
defer p.mu.Unlock()
123-
p.peers = consistenthash.New(defaultReplicas, nil)
121+
p.peers = consistenthash.New(p.opts.Replicas, p.opts.HashFn)
124122
p.peers.Add(peers...)
125123
p.httpGetters = make(map[string]*httpGetter, len(peers))
126124
for _, peer := range peers {
127-
p.httpGetters[peer] = &httpGetter{transport: p.Transport, baseURL: peer + p.basePath}
125+
p.httpGetters[peer] = &httpGetter{transport: p.Transport, baseURL: peer + p.opts.BasePath}
128126
}
129127
}
130128

@@ -142,10 +140,10 @@ func (p *HTTPPool) PickPeer(key string) (ProtoGetter, bool) {
142140

143141
func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
144142
// Parse request.
145-
if !strings.HasPrefix(r.URL.Path, p.basePath) {
143+
if !strings.HasPrefix(r.URL.Path, p.opts.BasePath) {
146144
panic("HTTPPool serving unexpected path: " + r.URL.Path)
147145
}
148-
parts := strings.SplitN(r.URL.Path[len(p.basePath):], "/", 2)
146+
parts := strings.SplitN(r.URL.Path[len(p.opts.BasePath):], "/", 2)
149147
if len(parts) != 2 {
150148
http.Error(w, "bad request", http.StatusBadRequest)
151149
return

0 commit comments

Comments
 (0)