@@ -85,9 +85,37 @@ 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 :
91119 cdef:
92120 bint load_balance = description.load_balance
93121 bint raise_exc = False
@@ -126,25 +154,18 @@ cdef class ThinConnImpl(BaseConnImpl):
126154 raise_exc = i == num_attempts - 1 \
127155 and j == num_lists - 1 \
128156 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
157+ self ._connect_with_address(address, description, params,
158+ raise_exc)
135159 if self ._protocol._in_connect:
136160 continue
137161 address_list.lru_index = (idx1 + 1 ) % num_addresses
138162 description.lru_index = (idx2 + 1 ) % num_lists
139- return
163+ return 0
140164 time.sleep(description.retry_delay)
141165
142- cdef ConnectParamsImpl _connect_with_params(self ,
143- ConnectParamsImpl params):
166+ cdef int _connect_with_params(self , ConnectParamsImpl params) except - 1 :
144167 """
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.
168+ Internal method used for connecting with the given parameters.
148169 """
149170 cdef:
150171 DescriptionList description_list = params.description_list
@@ -160,14 +181,10 @@ cdef class ThinConnImpl(BaseConnImpl):
160181 else :
161182 idx = i
162183 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:
184+ self ._connect_with_description(description, params, final_desc)
185+ if not self ._protocol._in_connect:
168186 description_list.lru_index = (idx + 1 ) % num_descriptions
169187 break
170- return redirect_params
171188
172189 cdef Message _create_message(self , type typ):
173190 """
@@ -183,74 +200,6 @@ cdef class ThinConnImpl(BaseConnImpl):
183200 self ._pool = None
184201 self ._protocol._force_close()
185202
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.settimeout(None )
228- if address.protocol == " tcps" :
229- sock = get_ssl_socket(sock, params, description, address)
230- self ._drcp_enabled = description.server_type == " pooled"
231- if self ._cclass is None :
232- self ._cclass = description.cclass
233- self ._protocol._set_socket(sock)
234- redirect_params = self ._protocol._connect_phase_one(self , params,
235- description,
236- address)
237- if redirect_params is not None :
238- return redirect_params
239- except exceptions.DatabaseError:
240- if raise_exception:
241- raise
242- return
243- except (socket.gaierror, ConnectionRefusedError) as e:
244- if raise_exception:
245- errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
246- exception = str (e))
247- return
248- except Exception as e:
249- errors._raise_err(errors.ERR_CONNECTION_FAILED, cause = e,
250- exception = str (e))
251- return
252- self ._protocol._connect_phase_two(self , description, params)
253-
254203 cdef Statement _get_statement(self , str sql, bint cache_statement):
255204 """
256205 Get a statement from the statement cache, or prepare a new statement
@@ -340,14 +289,9 @@ cdef class ThinConnImpl(BaseConnImpl):
340289 self ._protocol._process_single_message(message)
341290
342291 def connect (self , ConnectParamsImpl params ):
343- cdef ConnectParamsImpl redirect_params
344292 if params._password is None :
345293 errors._raise_err(errors.ERR_NO_PASSWORD)
346- while True :
347- redirect_params = self ._connect_with_params(params)
348- if redirect_params is None :
349- break
350- params = redirect_params
294+ self ._connect_with_params(params)
351295 self ._statement_cache = collections.OrderedDict()
352296 self ._statement_cache_size = params.stmtcachesize
353297 self ._statement_cache_lock = threading.Lock()
0 commit comments