@@ -46,12 +46,12 @@ type HTTPPool struct {
46
46
// If nil, the client uses http.DefaultTransport.
47
47
Transport func (Context ) http.RoundTripper
48
48
49
- // base path including leading and trailing slash, e.g. "/_groupcache/"
50
- basePath string
51
-
52
49
// this peer's base URL, e.g. "https://example.net:8000"
53
50
self string
54
51
52
+ // opts specifies the options.
53
+ opts HTTPPoolOptions
54
+
55
55
mu sync.Mutex // guards peers and httpGetters
56
56
peers * consistenthash.Map
57
57
httpGetters map [string ]* httpGetter // keyed by e.g. "http://10.0.0.2:8008"
@@ -78,7 +78,7 @@ type HTTPPoolOptions struct {
78
78
// for example "http://example.net:8000".
79
79
func NewHTTPPool (self string ) * HTTPPool {
80
80
p := NewHTTPPoolOpts (self , nil )
81
- http .Handle (p .basePath , p )
81
+ http .Handle (p .opts . BasePath , p )
82
82
return p
83
83
}
84
84
@@ -93,23 +93,21 @@ func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool {
93
93
}
94
94
httpPoolMade = true
95
95
96
- opts := HTTPPoolOptions {}
96
+ p := & HTTPPool {
97
+ self : self ,
98
+ httpGetters : make (map [string ]* httpGetter ),
99
+ }
97
100
if o != nil {
98
- opts = * o
101
+ p . opts = * o
99
102
}
100
- if opts .BasePath == "" {
101
- opts .BasePath = defaultBasePath
103
+ if p . opts .BasePath == "" {
104
+ p . opts .BasePath = defaultBasePath
102
105
}
103
- if opts .Replicas == 0 {
104
- opts .Replicas = defaultReplicas
106
+ if p . opts .Replicas == 0 {
107
+ p . opts .Replicas = defaultReplicas
105
108
}
109
+ p .peers = consistenthash .New (p .opts .Replicas , p .opts .HashFn )
106
110
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
- }
113
111
RegisterPeerPicker (func () PeerPicker { return p })
114
112
return p
115
113
}
@@ -120,11 +118,11 @@ func NewHTTPPoolOpts(self string, o *HTTPPoolOptions) *HTTPPool {
120
118
func (p * HTTPPool ) Set (peers ... string ) {
121
119
p .mu .Lock ()
122
120
defer p .mu .Unlock ()
123
- p .peers = consistenthash .New (defaultReplicas , nil )
121
+ p .peers = consistenthash .New (p . opts . Replicas , p . opts . HashFn )
124
122
p .peers .Add (peers ... )
125
123
p .httpGetters = make (map [string ]* httpGetter , len (peers ))
126
124
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 }
128
126
}
129
127
}
130
128
@@ -142,10 +140,10 @@ func (p *HTTPPool) PickPeer(key string) (ProtoGetter, bool) {
142
140
143
141
func (p * HTTPPool ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
144
142
// Parse request.
145
- if ! strings .HasPrefix (r .URL .Path , p .basePath ) {
143
+ if ! strings .HasPrefix (r .URL .Path , p .opts . BasePath ) {
146
144
panic ("HTTPPool serving unexpected path: " + r .URL .Path )
147
145
}
148
- parts := strings .SplitN (r .URL .Path [len (p .basePath ):], "/" , 2 )
146
+ parts := strings .SplitN (r .URL .Path [len (p .opts . BasePath ):], "/" , 2 )
149
147
if len (parts ) != 2 {
150
148
http .Error (w , "bad request" , http .StatusBadRequest )
151
149
return
0 commit comments