第 2 个请求发给谁
按模型,只在活的里轮询,pick_healthy([(a,活),(b,挂),(c,活)], 1) 交回谁?
贯穿本节的健康检查模型:status 是 [(后端名, 是否活着)]。healthy 交回还活着的后端名(按原序),available 数活着几个,pick_healthy(status, n) 只在活着的后端里轮询(都挂了交回空串),all_down 是不是全挂了。
def healthy(status):
# status: [(后端名, 是否活着)];交回还活着的后端名(按原序)
return [name for name, up in status if up]
def available(status):
# 还有几个后端活着
return len(healthy(status))
def pick_healthy(status, n):
# 只在活着的后端里轮询;一个都不活交回 ""
ups = healthy(status)
return ups[n % len(ups)] if ups else ""
def all_down(status):
# 是不是全挂了
return available(status) == 0
print(pick_healthy([("a", True), ("b", False), ("c", True)], 1))
全部评论