3939class TCP extends Adapter
4040{
4141 /** @var array<int, Client> */
42- protected array $ backendConnections = [];
42+ protected array $ connections = [];
4343
4444 /** @var float Backend connection timeout in seconds */
45- protected float $ connectTimeout = 5.0 ;
45+ protected float $ timeout = 5.0 ;
4646
4747 /** @var bool Whether read/write split routing is enabled */
4848 protected bool $ readWriteSplit = false ;
4949
5050 /** @var Parser|null Lazy-initialized query parser */
51- protected ?Parser $ queryParser = null ;
51+ protected ?Parser $ parser = null ;
5252
5353 /**
5454 * Per-connection transaction pinning state.
5555 * When a connection is in a transaction, all queries are routed to primary.
5656 *
5757 * @var array<int, bool>
5858 */
59- protected array $ pinnedConnections = [];
59+ protected array $ pinned = [];
6060
6161 public function __construct (
6262 ?Resolver $ resolver = null ,
@@ -72,9 +72,9 @@ public function __construct(
7272 /**
7373 * Set backend connection timeout
7474 */
75- public function setConnectTimeout (float $ timeout ): static
75+ public function setTimeout (float $ timeout ): static
7676 {
77- $ this ->connectTimeout = $ timeout ;
77+ $ this ->timeout = $ timeout ;
7878
7979 return $ this ;
8080 }
@@ -105,9 +105,9 @@ public function isReadWriteSplit(): bool
105105 /**
106106 * Check if a connection is pinned to primary (in a transaction)
107107 */
108- public function isConnectionPinned (int $ clientFd ): bool
108+ public function isPinned (int $ clientFd ): bool
109109 {
110- return $ this ->pinnedConnections [$ clientFd ] ?? false ;
110+ return $ this ->pinned [$ clientFd ] ?? false ;
111111 }
112112
113113 /**
@@ -149,29 +149,29 @@ public function getDescription(): string
149149 * @param int $clientFd Client file descriptor for transaction tracking
150150 * @return QueryType QueryType::Read or QueryType::Write
151151 */
152- public function classifyQuery (string $ data , int $ clientFd ): QueryType
152+ public function classify (string $ data , int $ clientFd ): QueryType
153153 {
154154 if (!$ this ->readWriteSplit ) {
155155 return QueryType::Write;
156156 }
157157
158158 // If connection is pinned to primary (in transaction), everything goes to primary
159- if ($ this ->isConnectionPinned ($ clientFd )) {
160- $ classification = $ this ->getQueryParser ()->parse ($ data );
159+ if ($ this ->isPinned ($ clientFd )) {
160+ $ classification = $ this ->getParser ()->parse ($ data );
161161
162162 // Transaction end unpins
163163 if ($ classification === QueryType::TransactionEnd) {
164- unset($ this ->pinnedConnections [$ clientFd ]);
164+ unset($ this ->pinned [$ clientFd ]);
165165 }
166166
167167 return QueryType::Write;
168168 }
169169
170- $ classification = $ this ->getQueryParser ()->parse ($ data );
170+ $ classification = $ this ->getParser ()->parse ($ data );
171171
172172 // Transaction begin pins to primary
173173 if ($ classification === QueryType::TransactionBegin) {
174- $ this ->pinnedConnections [$ clientFd ] = true ;
174+ $ this ->pinned [$ clientFd ] = true ;
175175
176176 return QueryType::Write;
177177 }
@@ -215,9 +215,9 @@ public function routeQuery(string $resourceId, QueryType $queryType): Connection
215215 *
216216 * Should be called when a client disconnects to clean up state.
217217 */
218- public function clearConnectionState (int $ clientFd ): void
218+ public function clearState (int $ clientFd ): void
219219 {
220- unset($ this ->pinnedConnections [$ clientFd ]);
220+ unset($ this ->pinned [$ clientFd ]);
221221 }
222222
223223 /**
@@ -231,10 +231,10 @@ public function clearConnectionState(int $clientFd): void
231231 *
232232 * @throws \Exception
233233 */
234- public function getBackendConnection (string $ initialData , int $ clientFd ): Client
234+ public function getConnection (string $ initialData , int $ clientFd ): Client
235235 {
236- if (isset ($ this ->backendConnections [$ clientFd ])) {
237- return $ this ->backendConnections [$ clientFd ];
236+ if (isset ($ this ->connections [$ clientFd ])) {
237+ return $ this ->connections [$ clientFd ];
238238 }
239239
240240 $ result = $ this ->route ($ initialData );
@@ -245,47 +245,47 @@ public function getBackendConnection(string $initialData, int $clientFd): Client
245245 $ client = new Client (SWOOLE_SOCK_TCP );
246246
247247 $ client ->set ([
248- 'timeout ' => $ this ->connectTimeout ,
249- 'connect_timeout ' => $ this ->connectTimeout ,
248+ 'timeout ' => $ this ->timeout ,
249+ 'connect_timeout ' => $ this ->timeout ,
250250 'open_tcp_nodelay ' => true ,
251251 'socket_buffer_size ' => 2 * 1024 * 1024 ,
252252 ]);
253253
254- if (!$ client ->connect ($ host , $ port , $ this ->connectTimeout )) {
254+ if (!$ client ->connect ($ host , $ port , $ this ->timeout )) {
255255 throw new \Exception ("Failed to connect to backend: {$ host }: {$ port }" );
256256 }
257257
258- $ this ->backendConnections [$ clientFd ] = $ client ;
258+ $ this ->connections [$ clientFd ] = $ client ;
259259
260260 return $ client ;
261261 }
262262
263263 /**
264264 * Close backend connection for a client
265265 */
266- public function closeBackendConnection (int $ clientFd ): void
266+ public function closeConnection (int $ clientFd ): void
267267 {
268- if (isset ($ this ->backendConnections [$ clientFd ])) {
269- $ this ->backendConnections [$ clientFd ]->close ();
270- unset($ this ->backendConnections [$ clientFd ]);
268+ if (isset ($ this ->connections [$ clientFd ])) {
269+ $ this ->connections [$ clientFd ]->close ();
270+ unset($ this ->connections [$ clientFd ]);
271271 }
272272 }
273273
274274 /**
275275 * Get or create the query parser instance (lazy initialization)
276276 */
277- protected function getQueryParser (): Parser
277+ protected function getParser (): Parser
278278 {
279- if ($ this ->queryParser === null ) {
280- $ this ->queryParser = match ($ this ->getProtocol ()) {
279+ if ($ this ->parser === null ) {
280+ $ this ->parser = match ($ this ->getProtocol ()) {
281281 Protocol::PostgreSQL => new PostgreSQLParser (),
282282 Protocol::MySQL => new MySQLParser (),
283283 Protocol::MongoDB => new MongoDBParser (),
284284 default => throw new \Exception ('No query parser for protocol: ' . $ this ->getProtocol ()->value ),
285285 };
286286 }
287287
288- return $ this ->queryParser ;
288+ return $ this ->parser ;
289289 }
290290
291291 /**
@@ -310,7 +310,7 @@ protected function routeRead(string $resourceId): ConnectionResult
310310 }
311311
312312 if (!$ this ->skipValidation ) {
313- $ this ->validateEndpoint ($ endpoint );
313+ $ this ->validate ($ endpoint );
314314 }
315315
316316 return new ConnectionResult (
@@ -346,7 +346,7 @@ protected function routeWrite(string $resourceId): ConnectionResult
346346 }
347347
348348 if (!$ this ->skipValidation ) {
349- $ this ->validateEndpoint ($ endpoint );
349+ $ this ->validate ($ endpoint );
350350 }
351351
352352 return new ConnectionResult (
0 commit comments