@@ -91,13 +91,13 @@ def return_code(self):
9191def _execute (command : str ):
9292 success = False
9393 try :
94- conn = get_conn_through_sentinel ()
94+ conn = get_conn ()
9595 response = conn .execute_command (* command .split ())
9696 success = True
9797 except (redis .exceptions .ConnectionError , redis .exceptions .ResponseError ):
9898 try :
9999 reload_username_password_from_file_system_if_needed (app )
100- conn = get_conn_through_sentinel ()
100+ conn = get_conn ()
101101 response = conn .execute_command (* command .split ())
102102 success = True
103103 except Exception as err :
@@ -151,21 +151,30 @@ def reload_username_password_from_file_system_if_needed(app):
151151 app .config ["REDIS_USERNAME" ] = redis_username
152152
153153
154- def get_conn_through_sentinel ():
155- # it would be nice to call sentinel.master_for
156- # redis-py API here. But this does not work
157- # when the bdb is configured with TLS
158- # creating the connection directly instead
154+ def get_conn ():
155+ if app . config [ 'USE_SENTINEL' ]:
156+ return _get_sentinel_conn ()
157+ else :
158+ return _get_service_conn ()
159159
160- master_info = get_master (app .config ['REDIS_URL' ])
161- master_ip = str (master_info [0 ])
162- master_port = str (master_info [1 ])
163160
161+ def _get_service_conn ():
162+ return redis .Redis (host = app .config ["DB_SERVICE_HOST" ],
163+ port = app .config ["DB_SERVICE_PORT" ],
164+ password = app .config ['REDIS_PASSWORD' ],
165+ decode_responses = True )
166+
167+
168+ def _get_sentinel_conn ():
169+ # it would be nice to call sentinel.master_for redis-py API here. But this does not work when the bdb is configured
170+ # with TLS creating the connection directly instead
171+
172+ master_info = get_master (app .config ['REDIS_URL' ])
164173 connection_args = {
165- "host" : master_ip ,
166- "port" : master_port ,
174+ "host" : str ( master_info [ 0 ]) ,
175+ "port" : str ( master_info [ 1 ]) ,
167176 "password" : app .config ['REDIS_PASSWORD' ],
168- "decode_responses" : True
177+ "decode_responses" : True
169178 }
170179 redis_username = app .config ['REDIS_USERNAME' ]
171180 if redis_username :
0 commit comments