Skip to content

Commit a2a47f2

Browse files
authored
robustsession: move server to front of list in prefer() instead of duplicating
In the prefer() function, move the specified preferred server to the top of the server list instead of prepending another copy of it. This keeps the list from ending up with duplicate entries, which can throw off the selection frequency.
1 parent e8cdb65 commit a2a47f2

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

robustsession/robustsession.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,21 @@ func (n *Network) setServers(servers []string) {
214214
n.servers = servers
215215
}
216216

217-
// prefer adds the specified server to the front of the servers list, thereby
218-
// trying to prefer it over other servers for the next request. Note that
219-
// exponential backoff overrides this, so this is only a hint, not a guarantee.
217+
// prefer moves (or adds, if it doesn't already exist) the specified server to
218+
// the top of the servers list, thereby trying to prefer it over other servers
219+
// for the next request. Note that exponential backoff overrides this, so this
220+
// is only a hint, not a guarantee.
220221
func (n *Network) prefer(server string) {
221222
n.mu.Lock()
222223
defer n.mu.Unlock()
223224

224-
n.servers = append([]string{server}, n.servers...)
225+
servers := []string{server}
226+
for i := 0; i < len(n.servers); i++ {
227+
if n.servers[i] != server {
228+
servers = append(servers, n.servers[i])
229+
}
230+
}
231+
n.servers = servers
225232
}
226233

227234
func (n *Network) failed(server string) {

0 commit comments

Comments
 (0)