还有几台活着
按模型,[(a,活), (b,挂), (c,活)],available 交回几台?
贯穿本节的健康检查模型: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(available([("a", True), ("b", False), ("c", True)]))
全部评论