Selaa lähdekoodia

return nil if there are no tokens (i.e. no hosts available)

Wyndham Blanton 9 vuotta sitten
vanhempi
commit
bc813c4384
1 muutettua tiedostoa jossa 10 lisäystä ja 3 poistoa
  1. 10 3
      token.go

+ 10 - 3
token.go

@@ -203,15 +203,22 @@ func (t *tokenRing) GetHostForToken(token token) *HostInfo {
 	if t == nil {
 		return nil
 	}
-
+	
+	l := len(t.tokens)
+	// no host tokens, no available hosts
+	if l == 0{
+		return nil
+	}
+	
 	// find the primary replica
 	ringIndex := sort.Search(
-		len(t.tokens),
+		l,
 		func(i int) bool {
 			return !t.tokens[i].Less(token)
 		},
 	)
-	if ringIndex == len(t.tokens) {
+	
+	if ringIndex == l {
 		// wrap around to the first in the ring
 		ringIndex = 0
 	}