Skip to content

Commit bc68c69

Browse files
committed
Limit the number of open connections
Refuse a connection past 151 open ones with MySQL's 1040 error, so the proxy keeps serving instead of running out of file descriptors. 151 is MySQL's own max_connections default.
1 parent 9ee22d0 commit bc68c69

3 files changed

Lines changed: 161 additions & 72 deletions

File tree

‎packages/mysql-proxy/src/class-mysql-proxy.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class MySQL_Proxy {
2626
*/
2727
const LOGIN_TIMEOUT = 10;
2828

29+
/**
30+
* Number of client connections served at once.
31+
*
32+
* This matches the default MySQL "max_connections".
33+
*/
34+
const MAX_CONNECTIONS = 151;
35+
2936
/**
3037
* The socket to listen on.
3138
*
@@ -223,6 +230,20 @@ private function handle_new_client(): void {
223230
throw $this->new_socket_error();
224231
}
225232

233+
// Refuse the connection while the proxy is full, as MySQL does.
234+
if ( count( $this->clients ) >= self::MAX_CONNECTIONS ) {
235+
$this->logger->error(
236+
'Refusing a new client: {count} connections are already open',
237+
array( 'count' => count( $this->clients ) )
238+
);
239+
try {
240+
$this->write_response( $socket, MySQL_Protocol::build_err_packet( 0, 1040, '08004', 'Too many connections' ) );
241+
} finally {
242+
@socket_close( $socket );
243+
}
244+
return;
245+
}
246+
226247
// Create a new session for the client.
227248
$client_id = $this->get_client_id( $socket );
228249
$session = new MySQL_Session( $this->adapter, $client_id, $this->authentication, $this->logger );
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use WP_MySQL_Proxy\Adapter\Adapter;
5+
use WP_MySQL_Proxy\MySQL_Authentication;
6+
use WP_MySQL_Proxy\MySQL_Proxy;
7+
use WP_MySQL_Proxy\MySQL_Session;
8+
9+
class WP_MySQL_Proxy_Limits_Test extends TestCase {
10+
/** @dataProvider pending_logins */
11+
public function test_login_deadline( int $deadline_offset, bool $authenticated, bool $expected_open, ?int $expected_timeout ): void {
12+
$proxy = new MySQL_Proxy(
13+
$this->createMock( Adapter::class ),
14+
array( 'authentication' => new MySQL_Authentication( 'user', 'password' ) )
15+
);
16+
$clients = $this->property( 'clients' );
17+
$clients->setValue(
18+
$proxy,
19+
array(
20+
1 => array(
21+
'socket' => $this->socket(),
22+
'session' => $this->session( $authenticated ),
23+
'login_deadline' => time() + $deadline_offset,
24+
),
25+
)
26+
);
27+
28+
$this->assertSame( $expected_timeout, $this->call( $proxy, 'get_select_timeout' ) );
29+
$this->call( $proxy, 'close_timed_out_logins' );
30+
$this->assertCount( $expected_open ? 1 : 0, $clients->getValue( $proxy ) );
31+
}
32+
33+
public function pending_logins(): array {
34+
return array(
35+
'pending login within the deadline' => array( 5, false, true, 5 ),
36+
'pending login past the deadline' => array( -1, false, false, 0 ),
37+
'authenticated past the deadline' => array( -1, true, true, null ),
38+
);
39+
}
40+
41+
/** @dataProvider connection_counts */
42+
public function test_connection_limit( int $open, bool $expected_accepted ): void {
43+
$port = $this->free_port();
44+
$proxy = new MySQL_Proxy(
45+
$this->createMock( Adapter::class ),
46+
array(
47+
'authentication' => new MySQL_Authentication( 'user', 'password' ),
48+
'port' => $port,
49+
)
50+
);
51+
52+
// Stand in for the listener the proxy creates in start().
53+
$listener = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
54+
socket_set_option( $listener, SOL_SOCKET, SO_REUSEADDR, 1 );
55+
socket_bind( $listener, '127.0.0.1', $port );
56+
socket_listen( $listener );
57+
$this->property( 'socket' )->setValue( $proxy, $listener );
58+
59+
$clients = $this->property( 'clients' );
60+
$occupant = array(
61+
'socket' => $this->socket(),
62+
'session' => $this->session( true ),
63+
'login_deadline' => time() + 60,
64+
);
65+
66+
// Key the occupants away from any object ID the accepted socket can get.
67+
$occupants = array();
68+
for ( $i = 1; $i <= $open; $i++ ) {
69+
$occupants[ PHP_INT_MAX - $i ] = $occupant;
70+
}
71+
$clients->setValue( $proxy, $occupants );
72+
73+
$client = stream_socket_client( "tcp://127.0.0.1:$port", $errno, $error, 2 );
74+
stream_set_timeout( $client, 2 );
75+
$this->call( $proxy, 'handle_new_client' );
76+
$response = fread( $client, 256 );
77+
78+
if ( $expected_accepted ) {
79+
$this->assertSame( 10, ord( $response[4] ), 'The client gets the handshake packet.' );
80+
$this->assertCount( $open + 1, $clients->getValue( $proxy ) );
81+
$accepted = $clients->getValue( $proxy );
82+
$this->assertGreaterThan( time(), end( $accepted )['login_deadline'] );
83+
} else {
84+
$this->assertSame( 0xff, ord( $response[4] ) );
85+
$this->assertSame( 1040, unpack( 'v', substr( $response, 5, 2 ) )[1] );
86+
$this->assertSame( 'Too many connections', substr( $response, 13 ) );
87+
$this->assertCount( $open, $clients->getValue( $proxy ), 'A refused client is not registered.' );
88+
$this->assertSame( '', fread( $client, 1 ), 'A refused connection is closed.' );
89+
}
90+
91+
fclose( $client );
92+
socket_close( $listener );
93+
}
94+
95+
public function connection_counts(): array {
96+
return array(
97+
'below the limit' => array( MySQL_Proxy::MAX_CONNECTIONS - 1, true ),
98+
'at the limit' => array( MySQL_Proxy::MAX_CONNECTIONS, false ),
99+
);
100+
}
101+
102+
private function free_port(): int {
103+
$listener = stream_socket_server( 'tcp://127.0.0.1:0', $errno, $error );
104+
$port = (int) substr( strrchr( stream_socket_get_name( $listener, false ), ':' ), 1 );
105+
fclose( $listener );
106+
return $port;
107+
}
108+
109+
private function session( bool $authenticated ): MySQL_Session {
110+
$session = new MySQL_Session( $this->createMock( Adapter::class ), 1, new MySQL_Authentication( 'user', 'password' ) );
111+
if ( $authenticated ) {
112+
$challenge = MySQL_Test_Client::challenge( $session->get_initial_handshake() );
113+
$session->receive_bytes( MySQL_Test_Client::handshake_response( $challenge ) );
114+
$this->assertTrue( $session->is_authenticated() );
115+
}
116+
return $session;
117+
}
118+
119+
private function socket() {
120+
$pair = array();
121+
socket_create_pair( AF_UNIX, SOCK_STREAM, 0, $pair );
122+
return $pair[0];
123+
}
124+
125+
private function property( string $name ): ReflectionProperty {
126+
$property = new ReflectionProperty( MySQL_Proxy::class, $name );
127+
if ( PHP_VERSION_ID < 80100 ) {
128+
$property->setAccessible( true );
129+
}
130+
return $property;
131+
}
132+
133+
private function call( MySQL_Proxy $proxy, string $name ) {
134+
$method = new ReflectionMethod( MySQL_Proxy::class, $name );
135+
if ( PHP_VERSION_ID < 80100 ) {
136+
$method->setAccessible( true );
137+
}
138+
return $method->invoke( $proxy );
139+
}
140+
}

‎packages/mysql-proxy/tests/WP_MySQL_Proxy_Login_Timeout_Test.php‎

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)