@@ -85,9 +85,40 @@ cdef class ThinConnImpl(BaseConnImpl):
8585 elif stmt._cursor_id != 0 :
8686 self ._add_cursor_to_close(stmt)
8787
88- cdef object _connect_with_description(self , Description description,
89- ConnectParamsImpl params,
90- bint final_desc):
88+ cdef int _connect_with_address(self , Address address,
89+ Description description,
90+ ConnectParamsImpl params,
91+ bint raise_exception) except - 1 :
92+ """
93+ Internal method used for connecting with the given description and
94+ address.
95+ """
96+ try :
97+ self ._protocol._connect_phase_one(self , params, description,
98+ address)
99+ except exceptions.DatabaseError:
100+ if raise_exception:
101+ raise
102+ return 0
103+ except (socket.gaierror, ConnectionRefusedError) as e:
104+ if raise_exception:
105+ errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
106+ exception = str (e))
107+ return 0
108+ except Exception as e:
109+ errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
110+ exception = str (e))
111+ self ._drcp_enabled = description.server_type == " pooled"
112+ if self ._cclass is None :
113+ self ._cclass = description.cclass
114+ self ._protocol._connect_phase_two(self , description, params)
115+
116+ cdef int _connect_with_description(self , Description description,
117+ ConnectParamsImpl params,
118+ bint final_desc) except - 1 :
119+ """
120+ Internal method used for connecting with the given description.
121+ """
91122 cdef:
92123 bint load_balance = description.load_balance
93124 bint raise_exc = False
@@ -126,25 +157,18 @@ cdef class ThinConnImpl(BaseConnImpl):
126157 raise_exc = i == num_attempts - 1 \
127158 and j == num_lists - 1 \
128159 and k == num_addresses - 1
129- redirect_params = self ._connect_with_address(address,
130- description,
131- params,
132- raise_exc)
133- if redirect_params is not None :
134- return redirect_params
160+ self ._connect_with_address(address, description, params,
161+ raise_exc)
135162 if self ._protocol._in_connect:
136163 continue
137164 address_list.lru_index = (idx1 + 1 ) % num_addresses
138165 description.lru_index = (idx2 + 1 ) % num_lists
139- return
166+ return 0
140167 time.sleep(description.retry_delay)
141168
142- cdef ConnectParamsImpl _connect_with_params(self ,
143- ConnectParamsImpl params):
169+ cdef int _connect_with_params(self , ConnectParamsImpl params) except - 1 :
144170 """
145- Internal method used for connecting with the given parameters. If the
146- listener requests a redirect, the redirect data is returned so that
147- this process can be repeated as needed.
171+ Internal method used for connecting with the given parameters.
148172 """
149173 cdef:
150174 DescriptionList description_list = params.description_list
@@ -160,14 +184,10 @@ cdef class ThinConnImpl(BaseConnImpl):
160184 else :
161185 idx = i
162186 description = descriptions[idx]
163- redirect_params = self ._connect_with_description(description,
164- params,
165- final_desc)
166- if redirect_params is not None \
167- or not self ._protocol._in_connect:
187+ self ._connect_with_description(description, params, final_desc)
188+ if not self ._protocol._in_connect:
168189 description_list.lru_index = (idx + 1 ) % num_descriptions
169190 break
170- return redirect_params
171191
172192 cdef Message _create_message(self , type typ):
173193 """
@@ -183,75 +203,6 @@ cdef class ThinConnImpl(BaseConnImpl):
183203 self ._pool = None
184204 self ._protocol._force_close()
185205
186- cdef object _connect_with_address(self , Address address,
187- Description description,
188- ConnectParamsImpl params,
189- bint raise_exception):
190- """
191- Creates a socket on which to communicate using the provided parameters.
192- If a proxy is configured, a connection to the proxy is established and
193- the target host and port is forwarded to the proxy. The socket is used
194- to establish a connection with the database. If a redirect is
195- required, the redirect parameters are returned.
196- """
197- cdef:
198- bint use_proxy = (address.https_proxy is not None )
199- double timeout = description.tcp_connect_timeout
200- if use_proxy:
201- if address.protocol != " tcps" :
202- errors._raise_err(errors.ERR_HTTPS_PROXY_REQUIRES_TCPS)
203- connect_info = (address.https_proxy, address.https_proxy_port)
204- else :
205- connect_info = (address.host, address.port)
206- try :
207- sock = socket.create_connection(connect_info, timeout)
208- if use_proxy:
209- data = f" CONNECT {address.host}:{address.port} HTTP/1.0\r \n \r \n "
210- sock.send(data.encode())
211- reply = sock.recv(1024 )
212- match = re.search(' HTTP/1.[01]\\ s+(\\ d+)\\ s+' , reply.decode())
213- if match is None or match.groups()[0 ] != ' 200' :
214- errors._raise_err(errors.ERR_PROXY_FAILURE,
215- response = reply.decode())
216- if description.expire_time > 0 :
217- sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1 )
218- if hasattr (socket, " TCP_KEEPIDLE" ) \
219- and hasattr (socket, " TCP_KEEPINTVL" ) \
220- and hasattr (socket, " TCP_KEEPCNT" ):
221- sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE,
222- description.expire_time * 60 )
223- sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL,
224- 6 )
225- sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT,
226- 10 )
227- sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 )
228- sock.settimeout(None )
229- if address.protocol == " tcps" :
230- sock = get_ssl_socket(sock, params, description, address)
231- self ._drcp_enabled = description.server_type == " pooled"
232- if self ._cclass is None :
233- self ._cclass = description.cclass
234- self ._protocol._set_socket(sock)
235- redirect_params = self ._protocol._connect_phase_one(self , params,
236- description,
237- address)
238- if redirect_params is not None :
239- return redirect_params
240- except exceptions.DatabaseError:
241- if raise_exception:
242- raise
243- return
244- except (socket.gaierror, ConnectionRefusedError) as e:
245- if raise_exception:
246- errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
247- exception = str (e))
248- return
249- except Exception as e:
250- errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
251- exception = str (e))
252- return
253- self ._protocol._connect_phase_two(self , description, params)
254-
255206 cdef Statement _get_statement(self , str sql, bint cache_statement):
256207 """
257208 Get a statement from the statement cache, or prepare a new statement
@@ -341,14 +292,9 @@ cdef class ThinConnImpl(BaseConnImpl):
341292 self ._protocol._process_single_message(message)
342293
343294 def connect (self , ConnectParamsImpl params ):
344- cdef ConnectParamsImpl redirect_params
345295 if params._password is None :
346296 errors._raise_err(errors.ERR_NO_PASSWORD)
347- while True :
348- redirect_params = self ._connect_with_params(params)
349- if redirect_params is None :
350- break
351- params = redirect_params
297+ self ._connect_with_params(params)
352298 self ._statement_cache = collections.OrderedDict()
353299 self ._statement_cache_size = params.stmtcachesize
354300 self ._statement_cache_lock = threading.Lock()
0 commit comments