IlyaSkriblovsky / txredisapi

non-blocking redis client for python twisted
Apache License 2.0
235 stars 91 forks source link

关于redis Sentinel的问题 #121

Closed github-yxb closed 4 years ago

github-yxb commented 6 years ago

我测试了txredisapi的问sentinel功能, 发现sentinel并不能正常工作,当我使用 txredisapi.Sentinel([("xxxxxxxx", 7379)], password = "1234") txredisapi会向sentinel发送AUTH指令,但其实sentinel并不支持AUTH指令, 所以sentinel返回错误,导到不能获取到master。 这个password选项其实应该是给master或slave使用的。 还有代码上的疑惑 self.sentinels = [ lazyConnection(host, port, **connection_kwargs) for host, port in sentinel_addresses ]

在sentinel初始化时创建了连向sentinel的factory,但应该是使用SentinelConnectionFactory吧。

def master_for(self, service_name, factory_class=SentinelConnectionFactory, dbid=None, poolsize=1, **connection_kwargs): factory = factory_class(sentinel_manager=self, service_name=service_name, is_master=True, uuid=None, dbid=dbid, poolsize=poolsize, **connection_kwargs) return self._connect_factory_and_return_handler(factory, poolsize) 而获取master时,应该当返回RedisFactory而不是SentinelConnectionFactory。

IlyaSkriblovsky commented 6 years ago

Thanks for your report!

  1. You are right, using authentication with Sentinel seems to be broken :( Not because it is sending AUTH to Sentinel, but because it sends ROLE to Redis before AUTH. I've fixed it in #122, could you please check that it works now? I've tested it with following snippet:
from twisted.internet import defer
from twisted.internet.task import react
from txredisapi import Sentinel

@defer.inlineCallbacks
def main(reactor):
    redis = Sentinel([('localhost', 26379)]).master_for('test', password='qweasdzxc')
    response = yield redis.get('hello')
    print(response)
    yield redis.disconnect()

react(main)
  1. I think factory names are ok, but there is some unobvious naming :) master_for returns SentinelConnectionFactory because it means "Redis connection that asks Sentinel for the master/slave addr and then connects to it". self.sentinels uses regular RedisFactory because connections to Sentinel is just regular connections without "ask sentinel for the real address" magic.