浏览代码

Merge pull request #826 from wyndhblb/master

return nil if there are no tokens (i.e. no hosts available)
Chris Bannister 9 年之前
父节点
当前提交
47f897f054
共有 2 个文件被更改,包括 10 次插入2 次删除
  1. 1 0
      AUTHORS
  2. 9 2
      token.go

+ 1 - 0
AUTHORS

@@ -82,3 +82,4 @@ Robert Nix <robert@nicerobot.org>
 Nathan Youngman <git@nathany.com>
 Charles Law <charles.law@gmail.com>; <claw@conduce.com>
 Nathan Davies <nathanjamesdavies@gmail.com>
+Bo Blanton <bo.blanton@gmail.com>

+ 9 - 2
token.go

@@ -204,14 +204,21 @@ func (t *tokenRing) GetHostForToken(token token) *HostInfo {
 		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
 	}