-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch_transport.cpp
More file actions
382 lines (302 loc) · 10.1 KB
/
Copy pathch_transport.cpp
File metadata and controls
382 lines (302 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
+----------------------------------------------------------------------+
| php-clickhouse — native async ClickHouse client for PHP TrueAsync |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"). |
+----------------------------------------------------------------------+
Transport bridge between clickhouse-cpp and the TrueAsync reactor.
clickhouse-cpp owns the protocol and buffers/compresses on top of the
transport; the InputStream::DoRead / OutputStream::DoWrite we provide are the
bottom layer and move raw bytes. We reuse clickhouse-cpp's own
connect/DNS/socket-option logic (NonSecureSocketFactory + the protected
Socket::handle_) and replace only the byte movement with reactor-driven,
non-blocking recv/send.
The readiness wait follows the canonical TrueAsync DB-driver pattern (see
ext/pdo_pgsql/pgsql_driver.c pdo_pgsql_await_socket): a single reactor poll
event + waker C callback + one suspend. No per-read request allocation and no
re-arming suspend loop.
NOTE: the initial TCP connect is still blocking (clickhouse-cpp's Socket ctor);
async connect is a follow-up. The read/write path is reactor-driven.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
extern "C" {
#include "php.h"
#include "Zend/zend_async_API.h"
#include "main/php_streams.h"
}
#include <clickhouse/base/socket.h>
#include <clickhouse/client.h>
#include <errno.h>
#ifdef PHP_WIN32
/* winsock2.h / ws2tcpip.h are pulled in by <clickhouse/base/socket.h> above. */
#else
#include <fcntl.h>
#include <sys/socket.h>
#endif
#include <memory>
#include "ch_exceptions.h"
#include "ch_transport.h"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
/* The byte-movement paths below are identical across platforms except for how
* a socket reports "would block": POSIX sets errno (EAGAIN/EWOULDBLOCK), Winsock
* keeps its own WSAE* codes behind WSAGetLastError(), and recv/send take an int
* length on Windows. These shims absorb that difference. */
#ifdef PHP_WIN32
#define CH_SOCK_LAST_ERROR WSAGetLastError()
#define CH_SOCK_EINTR WSAEINTR
#define CH_SOCK_EAGAIN WSAEWOULDBLOCK
#define CH_SOCK_EWOULDBLOCK WSAEWOULDBLOCK
#define CH_SOCK_IOLEN(len) (static_cast<int>(len))
#else
#define CH_SOCK_LAST_ERROR errno
#define CH_SOCK_EINTR EINTR
#define CH_SOCK_EAGAIN EAGAIN
#define CH_SOCK_EWOULDBLOCK EWOULDBLOCK
#define CH_SOCK_IOLEN(len) (len)
#endif
namespace {
/*
* Suspend the current coroutine until `fd` is ready for `events`. Returns true
* on readiness; false on cancellation / timeout / error (a PHP exception may be
* pending). Mirrors pdo_pgsql_await_socket.
*/
bool ch_await_socket(zend_socket_t fd, async_poll_event events, zend_ulong timeout_ms)
{
zend_coroutine_t *coroutine = ZEND_ASYNC_CURRENT_COROUTINE;
if (coroutine == nullptr) {
return false;
}
zend_async_poll_event_t *poll_event = ZEND_ASYNC_NEW_SOCKET_EVENT(fd, events);
if (poll_event == nullptr || EG(exception) != nullptr) {
return false;
}
zend_async_waker_new_with_timeout(coroutine, timeout_ms, NULL);
if (EG(exception) != nullptr) {
ZEND_ASYNC_EVENT_RELEASE(&poll_event->base);
return false;
}
zend_async_resume_when(coroutine, &poll_event->base, true,
zend_async_waker_callback_resolve, NULL);
if (EG(exception) != nullptr) {
zend_async_waker_clean(coroutine);
return false;
}
bool suspended = ZEND_ASYNC_SUSPEND();
zend_async_waker_clean(coroutine);
return suspended && EG(exception) == nullptr;
}
/* InputStream: non-blocking recv, awaiting readability through the reactor. */
class AsyncInput final : public clickhouse::InputStream {
public:
explicit AsyncInput(zend_socket_t fd) : fd_(fd) {}
protected:
bool Skip(size_t /*bytes*/) override { return false; }
size_t DoRead(void *buf, size_t len) override
{
for (;;) {
ssize_t n = ::recv(fd_, static_cast<char *>(buf), CH_SOCK_IOLEN(len), 0);
if (n > 0) {
return static_cast<size_t>(n);
}
if (n == 0) {
throw chasync::ConnectionError("clickhouse: connection closed by peer");
}
if (CH_SOCK_LAST_ERROR == CH_SOCK_EINTR) {
continue;
}
if (CH_SOCK_LAST_ERROR == CH_SOCK_EAGAIN || CH_SOCK_LAST_ERROR == CH_SOCK_EWOULDBLOCK) {
if (!ch_await_socket(fd_, ASYNC_READABLE, 0)) {
throw chasync::ConnectionError("clickhouse: read interrupted");
}
continue;
}
throw chasync::ConnectionError("clickhouse: recv failed");
}
}
private:
zend_socket_t fd_; /* owned by the clickhouse-cpp Socket */
};
/* OutputStream: non-blocking send, awaiting writability through the reactor. */
class AsyncOutput final : public clickhouse::OutputStream {
public:
explicit AsyncOutput(zend_socket_t fd) : fd_(fd) {}
protected:
size_t DoWrite(const void *data, size_t len) override
{
const char *p = static_cast<const char *>(data);
size_t sent = 0;
while (sent < len) {
ssize_t n = ::send(fd_, p + sent, CH_SOCK_IOLEN(len - sent), MSG_NOSIGNAL);
if (n > 0) {
sent += static_cast<size_t>(n);
continue;
}
if (CH_SOCK_LAST_ERROR == CH_SOCK_EINTR) {
continue;
}
if (CH_SOCK_LAST_ERROR == CH_SOCK_EAGAIN || CH_SOCK_LAST_ERROR == CH_SOCK_EWOULDBLOCK) {
if (!ch_await_socket(fd_, ASYNC_WRITABLE, 0)) {
throw chasync::ConnectionError("clickhouse: write interrupted");
}
continue;
}
throw chasync::ConnectionError("clickhouse: send failed");
}
return len;
}
private:
zend_socket_t fd_; /* owned by the clickhouse-cpp Socket */
};
/*
* clickhouse-cpp Socket whose byte movement goes through the reactor. The base
* ctor connects (blocking for now) and sets the protected handle_; we only
* switch it to non-blocking. The fd stays owned by the base Socket (closed in
* ~Socket), so there is no fd-ownership handoff to coordinate.
*/
class AsyncSocket final : public clickhouse::Socket {
public:
explicit AsyncSocket(const clickhouse::NetworkAddress &addr)
: clickhouse::Socket(addr)
{
set_nonblocking();
}
AsyncSocket(const clickhouse::NetworkAddress &addr,
const clickhouse::SocketTimeoutParams &timeout_params)
: clickhouse::Socket(addr, timeout_params)
{
set_nonblocking();
}
std::unique_ptr<clickhouse::InputStream> makeInputStream() const override
{
return std::make_unique<AsyncInput>(handle_);
}
std::unique_ptr<clickhouse::OutputStream> makeOutputStream() const override
{
return std::make_unique<AsyncOutput>(handle_);
}
private:
void set_nonblocking()
{
#ifdef PHP_WIN32
u_long nonblocking = 1;
ioctlsocket(handle_, FIONBIO, &nonblocking);
#else
int flags = fcntl(handle_, F_GETFL, 0);
if (flags >= 0) {
fcntl(handle_, F_SETFL, flags | O_NONBLOCK);
}
#endif
}
};
/* TLS transport: a php_stream ssl:// socket. php_stream socket IO is async
* under TrueAsync, so the handshake and reads/writes yield the coroutine. */
class TlsInput final : public clickhouse::InputStream {
public:
explicit TlsInput(php_stream *stream) : stream_(stream) {}
protected:
bool Skip(size_t /*bytes*/) override { return false; }
size_t DoRead(void *buf, size_t len) override
{
ssize_t n = php_stream_read(stream_, static_cast<char *>(buf), len);
if (n <= 0) {
throw chasync::ConnectionError("clickhouse: tls connection closed");
}
return static_cast<size_t>(n);
}
private:
php_stream *stream_; /* owned by TlsSocket */
};
class TlsOutput final : public clickhouse::OutputStream {
public:
explicit TlsOutput(php_stream *stream) : stream_(stream) {}
protected:
size_t DoWrite(const void *data, size_t len) override
{
const char *p = static_cast<const char *>(data);
size_t sent = 0;
while (sent < len) {
ssize_t n = php_stream_write(stream_, p + sent, len - sent);
if (n <= 0) {
throw chasync::ConnectionError("clickhouse: tls write failed");
}
sent += static_cast<size_t>(n);
}
return len;
}
private:
php_stream *stream_; /* owned by TlsSocket */
};
class TlsSocket final : public clickhouse::SocketBase {
public:
TlsSocket(const std::string &host, uint16_t port, bool verify)
{
php_stream_context *ctx = php_stream_context_alloc();
zval flag;
ZVAL_BOOL(&flag, verify);
php_stream_context_set_option(ctx, "ssl", "verify_peer", &flag);
php_stream_context_set_option(ctx, "ssl", "verify_peer_name", &flag);
std::string url = "ssl://" + host + ":" + std::to_string(port);
zend_string *errstr = nullptr;
int errcode = 0;
stream_ = php_stream_xport_create(url.c_str(), url.size(), 0,
STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT, nullptr, nullptr, ctx,
&errstr, &errcode);
std::string error = errstr != nullptr
? std::string(ZSTR_VAL(errstr), ZSTR_LEN(errstr))
: "tls connect failed";
if (errstr != nullptr) {
zend_string_release(errstr);
}
if (stream_ == nullptr) {
throw chasync::ConnectionError("clickhouse: " + error);
}
}
~TlsSocket() override
{
if (stream_ != nullptr) {
php_stream_close(stream_);
}
}
std::unique_ptr<clickhouse::InputStream> makeInputStream() const override
{
return std::make_unique<TlsInput>(stream_);
}
std::unique_ptr<clickhouse::OutputStream> makeOutputStream() const override
{
return std::make_unique<TlsOutput>(stream_);
}
private:
php_stream *stream_;
};
/* Factory: reuse NonSecureSocketFactory connect()/DNS/socket-options for the
* plaintext path; for TLS, connect a php_stream ssl:// socket instead. */
class AsyncSocketFactory final : public clickhouse::NonSecureSocketFactory {
public:
AsyncSocketFactory(bool tls, bool verify) : tls_(tls), verify_(verify) {}
std::unique_ptr<clickhouse::SocketBase> connect(const clickhouse::ClientOptions &opts,
const clickhouse::Endpoint &endpoint) override
{
if (tls_) {
return std::make_unique<TlsSocket>(endpoint.host, endpoint.port, verify_);
}
return clickhouse::NonSecureSocketFactory::connect(opts, endpoint);
}
protected:
std::unique_ptr<clickhouse::Socket> doConnect(const clickhouse::NetworkAddress &address,
const clickhouse::ClientOptions & /*opts*/) override
{
return std::make_unique<AsyncSocket>(address);
}
private:
bool tls_;
bool verify_;
};
} // namespace
std::unique_ptr<clickhouse::SocketFactory> ch_make_async_socket_factory(bool tls, bool verify)
{
return std::make_unique<AsyncSocketFactory>(tls, verify);
}