| 1 |
|
| /* --- Async TCP connect, I/O dispatch, timers, keepalive, reconnect, |
| 2 |
|
| * pipeline advance, and OpenSSL one-time init. --- |
| 3 |
|
| * |
| 4 |
|
| * Forward decls for symbols defined later in this file but called from |
| 5 |
|
| * earlier in the same file (cross-file callers use the forwards in |
| 6 |
|
| * ClickHouse.xs). |
| 7 |
|
| */ |
| 8 |
|
| static void io_cb(EV_P_ ev_io *w, int revents); |
| 9 |
|
| static void on_connect_done(ev_clickhouse_t *self); |
| 10 |
|
| static void start_keepalive(ev_clickhouse_t *self); |
| 11 |
|
| |
| 12 |
|
| /* Stash connection parameters without connecting (async-DNS pre-store). */ |
| 13 |
48 |
| static void store_conn_params(ev_clickhouse_t *self, const char *host, |
| 14 |
|
| unsigned int port, const char *user, |
| 15 |
|
| const char *password, const char *database) |
| 16 |
|
| { |
| 17 |
48 |
▶ | CLEAR_STR(self->host); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->host); |
0 |
48 |
48 |
|
| 18 |
48 |
▶ | CLEAR_STR(self->user); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->user); |
0 |
48 |
48 |
|
| 19 |
48 |
▶ | CLEAR_STR(self->password); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->password); |
0 |
48 |
48 |
|
| 20 |
48 |
▶ | CLEAR_STR(self->database); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->database); |
0 |
48 |
48 |
|
| 21 |
|
| |
| 22 |
48 |
| self->host = safe_strdup(host); |
| 23 |
48 |
| self->port = port; |
| 24 |
48 |
| self->user = safe_strdup(user); |
| 25 |
48 |
| self->password = safe_strdup(password); |
| 26 |
48 |
| self->database = safe_strdup(database); |
| 27 |
48 |
| } |
| 28 |
|
| |
| 29 |
|
| #ifdef HAVE_OPENSSL |
| 30 |
|
| /* Drain OpenSSL's per-thread error queue into self->last_tls_error |
| 31 |
|
| * (most recent error wins). Safe to call when the queue is empty — |
| 32 |
|
| * we leave the previous value untouched. */ |
| 33 |
0 |
| static void capture_tls_error(ev_clickhouse_t *self) { |
| 34 |
0 |
| unsigned long e = 0, last = 0; |
| 35 |
0 |
▶ | while ((e = ERR_get_error()) != 0) last = e; |
Branch: 1 branch
| Branch | True | False | Total |
| while ((e = ERR_get_error()) != 0) last = e; |
0 |
0 |
0 |
|
| 36 |
0 |
▶ | if (!last) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!last) return; |
0 |
0 |
0 |
|
| 37 |
|
| char buf[256]; |
| 38 |
0 |
| ERR_error_string_n(last, buf, sizeof(buf)); |
| 39 |
0 |
▶ | CLEAR_STR(self->last_tls_error); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->last_tls_error); |
0 |
0 |
0 |
|
| 40 |
0 |
| self->last_tls_error = savepv(buf); |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| /* SSL_ERROR_SYSCALL leaves OpenSSL's error queue empty — the detail |
| 44 |
|
| * lives in errno (ret < 0) or is a bare EOF (ret == 0). Record it so |
| 45 |
|
| * last_tls_error says something useful instead of going stale. */ |
| 46 |
1 |
| static void capture_tls_syscall_error(ev_clickhouse_t *self, int ret) { |
| 47 |
|
| char ebuf[160]; |
| 48 |
1 |
▶ | if (ret == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) |
0 |
1 |
1 |
|
| 49 |
0 |
| snprintf(ebuf, sizeof(ebuf), "TLS handshake EOF (SSL_ERROR_SYSCALL)"); |
| 50 |
|
| else |
| 51 |
1 |
| snprintf(ebuf, sizeof(ebuf), "TLS handshake I/O error: %s (SSL_ERROR_SYSCALL)", |
| 52 |
1 |
| strerror(errno)); |
| 53 |
1 |
▶ | CLEAR_STR(self->last_tls_error); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->last_tls_error); |
0 |
1 |
1 |
|
| 54 |
1 |
| self->last_tls_error = savepv(ebuf); |
| 55 |
1 |
| } |
| 56 |
|
| #endif |
| 57 |
|
| |
| 58 |
80 |
| static void start_connect(ev_clickhouse_t *self) { |
| 59 |
80 |
| struct addrinfo hints, *res = NULL, *ai; |
| 60 |
80 |
| int fd = -1, ret; |
| 61 |
|
| char port_str[16]; |
| 62 |
|
| |
| 63 |
80 |
| self->connect_gen++; |
| 64 |
|
| |
| 65 |
|
| { |
| 66 |
80 |
| int gen = self->connect_gen; |
| 67 |
80 |
| emit_trace(self, "connect %s:%u (%s)", |
| 68 |
|
| self->host, self->port, |
| 69 |
80 |
▶ | self->protocol == PROTO_NATIVE ? "native" : "http"); |
Branch: 1 branch
| Branch | True | False | Total |
| self->protocol == PROTO_NATIVE ? "native" : "http"); |
30 |
50 |
80 |
|
| 70 |
|
| /* A finish()/reset() from the handler wins: the outer frame must |
| 71 |
|
| * not connect over it (a finish would be silently undone). All |
| 72 |
|
| * callers end right after start_connect, so freeing here is safe. */ |
| 73 |
80 |
▶ | if (check_destroyed(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) return; |
0 |
80 |
80 |
|
| 74 |
80 |
▶ | if (self->magic != EV_CH_MAGIC) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC) return; |
0 |
80 |
80 |
|
| 75 |
80 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
0 |
80 |
80 |
|
| 76 |
|
| } |
| 77 |
80 |
| snprintf(port_str, sizeof(port_str), "%u", self->port); |
| 78 |
|
| |
| 79 |
80 |
| Zero(&hints, 1, struct addrinfo); |
| 80 |
80 |
| hints.ai_family = AF_UNSPEC; |
| 81 |
80 |
| hints.ai_socktype = SOCK_STREAM; |
| 82 |
|
| |
| 83 |
80 |
| ret = getaddrinfo(self->host, port_str, &hints, &res); |
| 84 |
80 |
▶ | if (ret != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret != 0) { |
0 |
80 |
80 |
|
| 85 |
|
| char errbuf[256]; |
| 86 |
0 |
| snprintf(errbuf, sizeof(errbuf), "getaddrinfo: %s", gai_strerror(ret)); |
| 87 |
0 |
| fail_connection(self, errbuf); |
| 88 |
0 |
| return; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* Try each resolved address in turn: dual-stack names routinely |
| 92 |
|
| * list an unreachable family first, and DNS round-robin sets may |
| 93 |
|
| * hold a dead entry. The first connect() that starts (immediate or |
| 94 |
|
| * EINPROGRESS) wins; only total failure reports an error. */ |
| 95 |
|
| { |
| 96 |
80 |
| int saved_errno = 0; |
| 97 |
80 |
▶ | for (ai = res; ai != NULL; ai = ai->ai_next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ai = res; ai != NULL; ai = ai->ai_next) { |
80 |
0 |
80 |
|
| 98 |
80 |
| int one = 1, fl; |
| 99 |
80 |
| fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 100 |
80 |
▶ | if (fd < 0) { saved_errno = errno; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { saved_errno = errno; continue; } |
0 |
80 |
80 |
|
| 101 |
80 |
| fl = fcntl(fd, F_GETFL); |
| 102 |
80 |
▶ | if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { |
80 |
0 |
80 |
| if (fl < 0 || fcntl(fd, F_SETFL, fl | O_NONBLOCK) < 0) { |
0 |
80 |
80 |
|
| 103 |
0 |
| saved_errno = errno; |
| 104 |
0 |
| close(fd); |
| 105 |
0 |
| fd = -1; |
| 106 |
0 |
| continue; |
| 107 |
|
| } |
| 108 |
80 |
| setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); |
| 109 |
80 |
| ret = connect(fd, ai->ai_addr, ai->ai_addrlen); |
| 110 |
80 |
▶ | if (ret == 0 || errno == EINPROGRESS) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (ret == 0 || errno == EINPROGRESS) break; |
80 |
0 |
80 |
| if (ret == 0 || errno == EINPROGRESS) break; |
0 |
80 |
80 |
|
| 111 |
0 |
| saved_errno = errno; |
| 112 |
0 |
| close(fd); |
| 113 |
0 |
| fd = -1; |
| 114 |
|
| } |
| 115 |
80 |
| freeaddrinfo(res); |
| 116 |
80 |
▶ | if (fd < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd < 0) { |
0 |
80 |
80 |
|
| 117 |
|
| char errbuf[256]; |
| 118 |
0 |
▶ | snprintf(errbuf, sizeof(errbuf), "connect: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| snprintf(errbuf, sizeof(errbuf), "connect: %s", |
0 |
0 |
0 |
|
| 119 |
|
| strerror(saved_errno ? saved_errno : ECONNREFUSED)); |
| 120 |
0 |
| fail_connection(self, errbuf); |
| 121 |
0 |
| return; |
| 122 |
|
| } |
| 123 |
|
| } |
| 124 |
|
| |
| 125 |
80 |
| self->fd = fd; |
| 126 |
80 |
| self->connecting = 1; |
| 127 |
|
| |
| 128 |
80 |
| ev_io_init(&self->rio, io_cb, self->fd, EV_READ); |
| 129 |
80 |
| self->rio.data = (void *)self; |
| 130 |
80 |
| ev_io_init(&self->wio, io_cb, self->fd, EV_WRITE); |
| 131 |
80 |
| self->wio.data = (void *)self; |
| 132 |
|
| |
| 133 |
|
| /* Arm connect_timeout on both paths: an immediately-connected |
| 134 |
|
| * socket still stalls in the TLS handshake / ServerHello phases. */ |
| 135 |
80 |
▶ | if (self->connect_timeout > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_timeout > 0) { |
6 |
74 |
80 |
|
| 136 |
|
| /* ev_timer_set on an active watcher corrupts libev's heap. */ |
| 137 |
6 |
▶ | if (self->timing) ev_timer_stop(self->loop, &self->timer); |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->timing) ev_timer_stop(self->loop, &self->timer); |
0 |
6 |
6 |
|
| 138 |
6 |
| ev_now_update(self->loop); |
| 139 |
6 |
| ev_timer_set(&self->timer, (ev_tstamp)self->connect_timeout, 0.0); |
| 140 |
6 |
| ev_timer_start(self->loop, &self->timer); |
| 141 |
6 |
| self->timing = 1; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
80 |
▶ | if (ret == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 0) { |
0 |
80 |
80 |
|
| 145 |
|
| /* connected immediately — connected=1 is deferred for native |
| 146 |
|
| * (until ServerHello) and TLS (until handshake completes) */ |
| 147 |
0 |
| self->connecting = 0; |
| 148 |
0 |
▶ | if (self->protocol != PROTO_NATIVE && !self->tls_enabled) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->protocol != PROTO_NATIVE && !self->tls_enabled) |
0 |
0 |
0 |
| if (self->protocol != PROTO_NATIVE && !self->tls_enabled) |
0 |
0 |
0 |
|
| 149 |
0 |
| self->connected = 1; |
| 150 |
0 |
| on_connect_done(self); |
| 151 |
0 |
| return; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
|
| /* in progress — wait for writability */ |
| 155 |
80 |
| start_writing(self); |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| /* Mark the connection as ready, fire on_connect, dispatch queued queries. |
| 159 |
|
| * Returns 1 if self was freed. */ |
| 160 |
62 |
| static int finish_connect(ev_clickhouse_t *self) { |
| 161 |
62 |
| self->handshaking = 0; |
| 162 |
62 |
| stop_timing(self); |
| 163 |
|
| /* Reset only here — at TCP accept time a peer that stalls the TLS |
| 164 |
|
| * handshake or ServerHello would otherwise spin reconnects forever |
| 165 |
|
| * without ever tripping reconnect_max_attempts. */ |
| 166 |
62 |
| self->reconnect_attempts = 0; |
| 167 |
62 |
| self->connected = 1; |
| 168 |
62 |
▶ | CLEAR_STR(self->last_tls_error); /* successful connect supersedes stale TLS error */ |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->last_tls_error); /* successful connect supersedes stale TLS error */ |
0 |
62 |
62 |
|
| 169 |
62 |
| start_keepalive(self); /* idle pings start at connect, not first query */ |
| 170 |
124 |
| if (self->on_connect && |
| 171 |
62 |
| fire_zero_arg_cb(self, self->on_connect, "connect")) return 1; |
| 172 |
62 |
▶ | if (!ngx_queue_empty(&self->send_queue)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ngx_queue_empty(&self->send_queue)) |
40 |
22 |
62 |
|
| 173 |
40 |
| return pipeline_advance(self); |
| 174 |
22 |
| return 0; |
| 175 |
|
| } |
| 176 |
|
| |
| 177 |
70 |
| static void on_connect_done(ev_clickhouse_t *self) { |
| 178 |
70 |
| self->connecting = 0; |
| 179 |
|
| /* TLS / native ServerHello may still be pending; queries issued in |
| 180 |
|
| * this window must queue, not croak "not connected". finish_connect |
| 181 |
|
| * clears it once the connection is fully ready. */ |
| 182 |
70 |
| self->handshaking = 1; |
| 183 |
|
| |
| 184 |
70 |
| stop_writing(self); |
| 185 |
|
| /* Keep the connect_timeout timer armed across the TLS handshake and |
| 186 |
|
| * native ServerHello phases — finish_connect() stops it once the |
| 187 |
|
| * connection is fully ready to accept queries. */ |
| 188 |
|
| |
| 189 |
|
| #ifdef HAVE_OPENSSL |
| 190 |
70 |
▶ | if (self->tls_enabled) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->tls_enabled) { |
1 |
69 |
70 |
|
| 191 |
|
| int ret; |
| 192 |
1 |
| self->ssl_ctx = SSL_CTX_new(TLS_client_method()); |
| 193 |
1 |
▶ | if (!self->ssl_ctx) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl_ctx) { |
0 |
1 |
1 |
|
| 194 |
0 |
| fail_connection(self, "SSL_CTX_new failed"); |
| 195 |
0 |
| return; |
| 196 |
|
| } |
| 197 |
1 |
| SSL_CTX_set_default_verify_paths(self->ssl_ctx); |
| 198 |
1 |
▶ | if (self->tls_skip_verify) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->tls_skip_verify) |
1 |
0 |
1 |
|
| 199 |
1 |
| SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_NONE, NULL); |
| 200 |
|
| else |
| 201 |
0 |
| SSL_CTX_set_verify(self->ssl_ctx, SSL_VERIFY_PEER, NULL); |
| 202 |
1 |
▶ | if (self->tls_ca_file) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->tls_ca_file) { |
0 |
1 |
1 |
|
| 203 |
0 |
▶ | if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_load_verify_locations(self->ssl_ctx, self->tls_ca_file, NULL) != 1) { |
0 |
0 |
0 |
|
| 204 |
0 |
| capture_tls_error(self); |
| 205 |
0 |
| fail_connection(self, "SSL_CTX_load_verify_locations failed"); |
| 206 |
0 |
| return; |
| 207 |
|
| } |
| 208 |
|
| } |
| 209 |
|
| /* Mutual TLS: load client certificate + private key when both |
| 210 |
|
| * are configured. SSL_CTX_check_private_key verifies that the |
| 211 |
|
| * private key matches the loaded certificate's public half. */ |
| 212 |
1 |
▶ | if (self->tls_cert_file && self->tls_key_file) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->tls_cert_file && self->tls_key_file) { |
0 |
1 |
1 |
| if (self->tls_cert_file && self->tls_key_file) { |
0 |
0 |
0 |
|
| 213 |
0 |
▶ | if (SSL_CTX_use_certificate_chain_file(self->ssl_ctx, self->tls_cert_file) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_use_certificate_chain_file(self->ssl_ctx, self->tls_cert_file) != 1) { |
0 |
0 |
0 |
|
| 214 |
0 |
| capture_tls_error(self); |
| 215 |
0 |
| fail_connection(self, "SSL_CTX_use_certificate_chain_file failed"); |
| 216 |
0 |
| return; |
| 217 |
|
| } |
| 218 |
0 |
▶ | if (SSL_CTX_use_PrivateKey_file(self->ssl_ctx, self->tls_key_file, SSL_FILETYPE_PEM) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_use_PrivateKey_file(self->ssl_ctx, self->tls_key_file, SSL_FILETYPE_PEM) != 1) { |
0 |
0 |
0 |
|
| 219 |
0 |
| capture_tls_error(self); |
| 220 |
0 |
| fail_connection(self, "SSL_CTX_use_PrivateKey_file failed"); |
| 221 |
0 |
| return; |
| 222 |
|
| } |
| 223 |
0 |
▶ | if (SSL_CTX_check_private_key(self->ssl_ctx) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SSL_CTX_check_private_key(self->ssl_ctx) != 1) { |
0 |
0 |
0 |
|
| 224 |
0 |
| capture_tls_error(self); |
| 225 |
0 |
| fail_connection(self, "TLS client cert / private key mismatch"); |
| 226 |
0 |
| return; |
| 227 |
|
| } |
| 228 |
1 |
▶ | } else if (self->tls_cert_file || self->tls_key_file) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (self->tls_cert_file || self->tls_key_file) { |
1 |
0 |
1 |
| } else if (self->tls_cert_file || self->tls_key_file) { |
0 |
1 |
1 |
|
| 229 |
0 |
| fail_connection(self, "tls_cert_file and tls_key_file must both be set"); |
| 230 |
0 |
| return; |
| 231 |
|
| } |
| 232 |
1 |
| self->ssl = SSL_new(self->ssl_ctx); |
| 233 |
1 |
▶ | if (!self->ssl) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->ssl) { |
0 |
1 |
1 |
|
| 234 |
0 |
| capture_tls_error(self); |
| 235 |
0 |
| fail_connection(self, "SSL_new failed"); |
| 236 |
0 |
| return; |
| 237 |
|
| } |
| 238 |
1 |
| SSL_set_fd(self->ssl, self->fd); |
| 239 |
|
| |
| 240 |
1 |
| int host_is_ip = is_ip_literal(self->host); |
| 241 |
|
| |
| 242 |
|
| /* SNI must not be sent for IP address literals (RFC 6066 s3) */ |
| 243 |
1 |
▶ | if (!host_is_ip) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!host_is_ip) |
0 |
1 |
1 |
|
| 244 |
0 |
| SSL_set_tlsext_host_name(self->ssl, self->host); |
| 245 |
|
| |
| 246 |
|
| /* Verify server certificate matches hostname or IP */ |
| 247 |
1 |
▶ | if (!self->tls_skip_verify) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->tls_skip_verify) { |
0 |
1 |
1 |
|
| 248 |
0 |
| X509_VERIFY_PARAM *param = SSL_get0_param(self->ssl); |
| 249 |
0 |
| X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); |
| 250 |
0 |
▶ | if (host_is_ip) |
Branch: 1 branch
| Branch | True | False | Total |
| if (host_is_ip) |
0 |
0 |
0 |
|
| 251 |
0 |
| X509_VERIFY_PARAM_set1_ip_asc(param, self->host); |
| 252 |
|
| else |
| 253 |
0 |
| X509_VERIFY_PARAM_set1_host(param, self->host, 0); |
| 254 |
|
| } |
| 255 |
|
| |
| 256 |
1 |
| ret = SSL_connect(self->ssl); |
| 257 |
1 |
▶ | if (ret == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 1) { |
0 |
1 |
1 |
|
| 258 |
|
| /* handshake done immediately */ |
| 259 |
0 |
| goto handshake_done; |
| 260 |
|
| } else { |
| 261 |
1 |
| int err = SSL_get_error(self->ssl, ret); |
| 262 |
1 |
▶ | if (err == SSL_ERROR_WANT_READ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_READ) { |
1 |
0 |
1 |
|
| 263 |
1 |
| start_reading(self); |
| 264 |
0 |
▶ | } else if (err == SSL_ERROR_WANT_WRITE) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (err == SSL_ERROR_WANT_WRITE) { |
0 |
0 |
0 |
|
| 265 |
0 |
| start_writing(self); |
| 266 |
|
| } else { |
| 267 |
0 |
▶ | if (err == SSL_ERROR_SYSCALL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_SYSCALL) |
0 |
0 |
0 |
|
| 268 |
0 |
| capture_tls_syscall_error(self, ret); |
| 269 |
|
| else |
| 270 |
0 |
| capture_tls_error(self); |
| 271 |
0 |
| fail_connection(self, "SSL_connect failed"); |
| 272 |
0 |
| return; |
| 273 |
|
| } |
| 274 |
|
| /* continue TLS handshake in io_cb */ |
| 275 |
1 |
| return; |
| 276 |
|
| } |
| 277 |
|
| } |
| 278 |
69 |
| handshake_done: |
| 279 |
|
| #endif |
| 280 |
|
| |
| 281 |
69 |
▶ | if (self->protocol == PROTO_NATIVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->protocol == PROTO_NATIVE) { |
27 |
42 |
69 |
|
| 282 |
|
| /* Send ClientHello and wait for ServerHello */ |
| 283 |
|
| size_t hello_len; |
| 284 |
27 |
| char *hello = build_native_hello(self, &hello_len); |
| 285 |
27 |
| send_replace(self, hello, hello_len); |
| 286 |
|
| |
| 287 |
27 |
| self->native_state = NATIVE_WAIT_HELLO; |
| 288 |
27 |
| start_writing(self); |
| 289 |
27 |
| return; |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
|
| /* HTTP protocol: connection is ready */ |
| 293 |
42 |
| (void)finish_connect(self); |
| 294 |
|
| } |
| 295 |
|
| |
| 296 |
|
| /* --- I/O callbacks --- */ |
| 297 |
|
| |
| 298 |
|
| /* Returns 1 if self was freed (caller must not access self). */ |
| 299 |
109 |
| static int try_write(ev_clickhouse_t *self) { |
| 300 |
218 |
▶ | while (self->send_pos < self->send_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (self->send_pos < self->send_len) { |
109 |
109 |
218 |
|
| 301 |
109 |
| ssize_t n = ch_write(self, self->send_buf + self->send_pos, |
| 302 |
109 |
| self->send_len - self->send_pos); |
| 303 |
109 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
109 |
109 |
|
| 304 |
0 |
▶ | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno == EAGAIN || errno == EWOULDBLOCK) { |
0 |
0 |
0 |
| if (errno == EAGAIN || errno == EWOULDBLOCK) { |
0 |
0 |
0 |
|
| 305 |
0 |
| start_writing(self); |
| 306 |
0 |
| return 0; |
| 307 |
|
| } |
| 308 |
0 |
| return teardown_io_error_reconnect(self, strerror(errno), "write error"); |
| 309 |
|
| } |
| 310 |
109 |
▶ | if (n == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) |
0 |
109 |
109 |
|
| 311 |
0 |
| return teardown_io_error_reconnect(self, "connection closed during write", |
| 312 |
|
| "connection closed"); |
| 313 |
109 |
| self->send_pos += n; |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| /* all sent */ |
| 317 |
109 |
| stop_writing(self); |
| 318 |
109 |
| self->send_len = 0; |
| 319 |
109 |
| self->send_pos = 0; |
| 320 |
|
| |
| 321 |
|
| /* start reading responses */ |
| 322 |
109 |
| start_reading(self); |
| 323 |
|
| |
| 324 |
|
| /* check if more to send */ |
| 325 |
109 |
▶ | if (!ngx_queue_empty(&self->send_queue)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ngx_queue_empty(&self->send_queue)) |
0 |
109 |
109 |
|
| 326 |
0 |
| return pipeline_advance(self); |
| 327 |
109 |
| return 0; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
80 |
| static void on_readable(ev_clickhouse_t *self) { |
| 331 |
|
| ssize_t n; |
| 332 |
|
| |
| 333 |
80 |
| ensure_recv_cap(self, self->recv_len + 4096); |
| 334 |
80 |
| n = ch_read(self, self->recv_buf + self->recv_len, |
| 335 |
80 |
| self->recv_cap - self->recv_len); |
| 336 |
|
| |
| 337 |
80 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
1 |
79 |
80 |
|
| 338 |
1 |
▶ | if (errno == EAGAIN || errno == EWOULDBLOCK) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno == EAGAIN || errno == EWOULDBLOCK) return; |
1 |
0 |
1 |
| if (errno == EAGAIN || errno == EWOULDBLOCK) return; |
0 |
1 |
1 |
|
| 339 |
1 |
| teardown_io_error_reconnect(self, strerror(errno), "read error"); |
| 340 |
1 |
| return; |
| 341 |
|
| } |
| 342 |
|
| |
| 343 |
79 |
▶ | if (n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) { |
28 |
51 |
79 |
|
| 344 |
|
| /* connection closed — fire on_error and drain pending if we |
| 345 |
|
| * have an in-flight request or haven't finished handshake */ |
| 346 |
28 |
▶ | int had_inflight = (self->send_count > 0 || !self->connected); |
Branch: 2 branches
| Branch | True | False | Total |
| int had_inflight = (self->send_count > 0 || !self->connected); |
0 |
28 |
28 |
| int had_inflight = (self->send_count > 0 || !self->connected); |
0 |
0 |
0 |
|
| 347 |
28 |
| int has_queued = !ngx_queue_empty(&self->send_queue); |
| 348 |
28 |
| int gen = self->connect_gen; |
| 349 |
|
| |
| 350 |
28 |
▶ | if (had_inflight) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (had_inflight) { |
28 |
0 |
28 |
|
| 351 |
28 |
| emit_error(self, "connection closed by server"); |
| 352 |
28 |
▶ | if (check_destroyed(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) return; |
0 |
28 |
28 |
|
| 353 |
28 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
0 |
28 |
28 |
|
| 354 |
|
| /* Only cancel in-flight cb_queue (irrecoverable). |
| 355 |
|
| * Keep send_queue if auto_reconnect — those haven't been sent yet. */ |
| 356 |
28 |
▶ | if (!self->auto_reconnect || !has_queued) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self->auto_reconnect || !has_queued) { |
24 |
4 |
28 |
| if (!self->auto_reconnect || !has_queued) { |
24 |
0 |
24 |
|
| 357 |
28 |
▶ | if (cancel_pending(self, "connection closed", |
Branch: 1 branch
| Branch | True | False | Total |
| if (cancel_pending(self, "connection closed", |
0 |
28 |
28 |
|
| 358 |
28 |
▶ | !(self->auto_reconnect && self->host))) |
Branch: 2 branches
| Branch | True | False | Total |
| !(self->auto_reconnect && self->host))) |
24 |
4 |
28 |
| !(self->auto_reconnect && self->host))) |
0 |
24 |
24 |
|
| 359 |
0 |
| return; |
| 360 |
|
| } else { |
| 361 |
|
| /* Cancel only the in-flight cb_queue entries */ |
| 362 |
0 |
| self->callback_depth++; |
| 363 |
0 |
| drain_cb_queue(self, "connection closed"); |
| 364 |
0 |
| self->callback_depth--; |
| 365 |
0 |
▶ | if (check_destroyed(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) return; |
0 |
0 |
0 |
|
| 366 |
|
| } |
| 367 |
28 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
0 |
28 |
28 |
|
| 368 |
|
| } |
| 369 |
28 |
▶ | if (cleanup_connection(self, 0)) return; /* on_disconnect freed self */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cleanup_connection(self, 0)) return; /* on_disconnect freed self */ |
0 |
28 |
28 |
|
| 370 |
|
| /* reset() from on_disconnect already reconnected — scheduling |
| 371 |
|
| * another reconnect would burn an attempt (inflating backoff, |
| 372 |
|
| * or tripping max_reconnect_attempts on a healthy link). */ |
| 373 |
28 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
13 |
15 |
28 |
|
| 374 |
|
| |
| 375 |
|
| /* Auto-reconnect if we have queued requests or flag is set */ |
| 376 |
15 |
▶ | if (self->auto_reconnect && self->host) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->auto_reconnect && self->host) { |
12 |
3 |
15 |
| if (self->auto_reconnect && self->host) { |
12 |
0 |
12 |
|
| 377 |
12 |
| schedule_reconnect(self); |
| 378 |
|
| } |
| 379 |
15 |
| return; |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
51 |
| self->recv_len += n; |
| 383 |
|
| |
| 384 |
|
| #ifdef HAVE_OPENSSL |
| 385 |
|
| /* TLS records already sitting in OpenSSL's buffer don't trip the |
| 386 |
|
| * socket watcher: a reply whose tail stays buffered while the |
| 387 |
|
| * socket runs dry would stall for lack of a new EV_READ. Drain |
| 388 |
|
| * everything buffered before parsing. No user code runs here, so |
| 389 |
|
| * self cannot be freed mid-loop (teardown paths return outright). */ |
| 390 |
51 |
▶ | while (self->ssl && SSL_pending(self->ssl) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (self->ssl && SSL_pending(self->ssl) > 0) { |
0 |
51 |
51 |
| while (self->ssl && SSL_pending(self->ssl) > 0) { |
0 |
0 |
0 |
|
| 391 |
0 |
| ensure_recv_cap(self, self->recv_len + 4096); |
| 392 |
0 |
| n = ch_read(self, self->recv_buf + self->recv_len, |
| 393 |
0 |
| self->recv_cap - self->recv_len); |
| 394 |
0 |
▶ | if (n < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) { |
0 |
0 |
0 |
|
| 395 |
0 |
▶ | if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
0 |
0 |
0 |
| if (errno == EAGAIN || errno == EWOULDBLOCK) break; |
0 |
0 |
0 |
|
| 396 |
0 |
| teardown_io_error_reconnect(self, strerror(errno), "read error"); |
| 397 |
0 |
| return; |
| 398 |
|
| } |
| 399 |
|
| /* Clean TLS close: the socket EOF follows on its own event; |
| 400 |
|
| * buffered bytes may still complete the response, so parse on. */ |
| 401 |
0 |
▶ | if (n == 0) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) break; |
0 |
0 |
0 |
|
| 402 |
0 |
| self->recv_len += n; |
| 403 |
|
| } |
| 404 |
|
| #endif |
| 405 |
|
| |
| 406 |
|
| /* Defensive ceiling: a runaway response (server stuck in a bad state, |
| 407 |
|
| * pathological row sizes, or a header-injection attack) shouldn't be |
| 408 |
|
| * able to consume unbounded memory. Caller opts in via max_recv_buffer; |
| 409 |
|
| * 0 (default) keeps the historical behaviour of growing without limit |
| 410 |
|
| * up to CH_MAX_DECOMPRESS_SIZE on compressed paths. */ |
| 411 |
51 |
▶ | if (self->max_recv_buffer > 0 && self->recv_len > self->max_recv_buffer) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->max_recv_buffer > 0 && self->recv_len > self->max_recv_buffer) { |
0 |
51 |
51 |
| if (self->max_recv_buffer > 0 && self->recv_len > self->max_recv_buffer) { |
0 |
0 |
0 |
|
| 412 |
|
| char errmsg[80]; |
| 413 |
0 |
| int gen = self->connect_gen; |
| 414 |
0 |
| snprintf(errmsg, sizeof(errmsg), |
| 415 |
|
| "recv buffer %lu exceeded max_recv_buffer %lu", |
| 416 |
0 |
| (unsigned long)self->recv_len, |
| 417 |
0 |
| (unsigned long)self->max_recv_buffer); |
| 418 |
0 |
▶ | if (teardown_io_error(self, errmsg, "recv buffer overflow")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (teardown_io_error(self, errmsg, "recv buffer overflow")) return; |
0 |
0 |
0 |
|
| 419 |
|
| /* The peer is healthy (it just answered too much), so |
| 420 |
|
| * re-establish the link like any other teardown — without |
| 421 |
|
| * this an auto_reconnect connection stays down forever after |
| 422 |
|
| * one oversize response. */ |
| 423 |
0 |
▶ | if (self->connect_gen == gen && self->auto_reconnect && self->host) |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->connect_gen == gen && self->auto_reconnect && self->host) |
0 |
0 |
0 |
| if (self->connect_gen == gen && self->auto_reconnect && self->host) |
0 |
0 |
0 |
| if (self->connect_gen == gen && self->auto_reconnect && self->host) |
0 |
0 |
0 |
|
| 424 |
0 |
| schedule_reconnect(self); |
| 425 |
0 |
| return; |
| 426 |
|
| } |
| 427 |
|
| |
| 428 |
51 |
▶ | if (self->protocol == PROTO_HTTP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->protocol == PROTO_HTTP) { |
1 |
50 |
51 |
|
| 429 |
1 |
| process_http_response(self); |
| 430 |
|
| } else { |
| 431 |
50 |
| process_native_response(self); |
| 432 |
|
| } |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
179 |
| static void io_cb(EV_P_ ev_io *w, int revents) { |
| 436 |
179 |
| ev_clickhouse_t *self = (ev_clickhouse_t *)w->data; |
| 437 |
|
| (void)loop; |
| 438 |
|
| |
| 439 |
179 |
▶ | if (self == NULL || self->magic != EV_CH_MAGIC) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (self == NULL || self->magic != EV_CH_MAGIC) return; |
179 |
0 |
179 |
| if (self == NULL || self->magic != EV_CH_MAGIC) return; |
0 |
179 |
179 |
|
| 440 |
|
| |
| 441 |
179 |
▶ | if (self->connecting) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connecting) { |
71 |
108 |
179 |
|
| 442 |
|
| /* check connect result */ |
| 443 |
71 |
| int err = 0; |
| 444 |
71 |
| socklen_t errlen = sizeof(err); |
| 445 |
|
| |
| 446 |
71 |
| stop_writing(self); |
| 447 |
|
| /* Don't stop the connect_timeout timer here — it must keep |
| 448 |
|
| * running across TLS handshake and native ServerHello stages. |
| 449 |
|
| * finish_connect() stops it once the connection is fully ready. */ |
| 450 |
|
| |
| 451 |
71 |
▶ | if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (getsockopt(self->fd, SOL_SOCKET, SO_ERROR, &err, &errlen) < 0) |
0 |
71 |
71 |
|
| 452 |
0 |
| err = errno; |
| 453 |
71 |
▶ | if (err != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err != 0) { |
1 |
70 |
71 |
|
| 454 |
|
| char errbuf[256]; |
| 455 |
1 |
| snprintf(errbuf, sizeof(errbuf), "connect: %s", strerror(err)); |
| 456 |
1 |
| fail_connection(self, errbuf); |
| 457 |
1 |
| return; |
| 458 |
|
| } |
| 459 |
|
| |
| 460 |
70 |
| on_connect_done(self); |
| 461 |
70 |
| return; |
| 462 |
|
| } |
| 463 |
|
| |
| 464 |
|
| #ifdef HAVE_OPENSSL |
| 465 |
108 |
▶ | if (self->ssl && !self->connected && self->native_state != NATIVE_WAIT_HELLO |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->ssl && !self->connected && self->native_state != NATIVE_WAIT_HELLO |
1 |
107 |
108 |
| if (self->ssl && !self->connected && self->native_state != NATIVE_WAIT_HELLO |
1 |
0 |
1 |
| if (self->ssl && !self->connected && self->native_state != NATIVE_WAIT_HELLO |
1 |
0 |
1 |
|
| 466 |
1 |
▶ | && self->native_state != NATIVE_WAIT_RESULT |
Branch: 1 branch
| Branch | True | False | Total |
| && self->native_state != NATIVE_WAIT_RESULT |
1 |
0 |
1 |
|
| 467 |
1 |
▶ | && self->native_state != NATIVE_WAIT_INSERT_META) { |
Branch: 1 branch
| Branch | True | False | Total |
| && self->native_state != NATIVE_WAIT_INSERT_META) { |
1 |
0 |
1 |
|
| 468 |
|
| /* TLS handshake in progress */ |
| 469 |
1 |
| int ret = SSL_connect(self->ssl); |
| 470 |
1 |
▶ | if (ret == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == 1) { |
0 |
1 |
1 |
|
| 471 |
0 |
| stop_reading(self); |
| 472 |
0 |
| stop_writing(self); |
| 473 |
|
| |
| 474 |
0 |
▶ | if (self->protocol == PROTO_NATIVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->protocol == PROTO_NATIVE) { |
0 |
0 |
0 |
|
| 475 |
|
| /* Send ClientHello over TLS, then wait for ServerHello */ |
| 476 |
|
| size_t hello_len; |
| 477 |
0 |
| char *hello = build_native_hello(self, &hello_len); |
| 478 |
0 |
| send_replace(self, hello, hello_len); |
| 479 |
0 |
| self->native_state = NATIVE_WAIT_HELLO; |
| 480 |
0 |
| start_writing(self); |
| 481 |
0 |
| return; |
| 482 |
|
| } |
| 483 |
|
| |
| 484 |
|
| /* HTTP protocol: fire on_connect */ |
| 485 |
0 |
| (void)finish_connect(self); |
| 486 |
0 |
| return; |
| 487 |
|
| } else { |
| 488 |
1 |
| int err = SSL_get_error(self->ssl, ret); |
| 489 |
1 |
| stop_reading(self); |
| 490 |
1 |
| stop_writing(self); |
| 491 |
1 |
▶ | if (err == SSL_ERROR_WANT_READ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_WANT_READ) { |
0 |
1 |
1 |
|
| 492 |
0 |
| start_reading(self); |
| 493 |
1 |
▶ | } else if (err == SSL_ERROR_WANT_WRITE) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (err == SSL_ERROR_WANT_WRITE) { |
0 |
1 |
1 |
|
| 494 |
0 |
| start_writing(self); |
| 495 |
|
| } else { |
| 496 |
1 |
▶ | if (err == SSL_ERROR_SYSCALL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (err == SSL_ERROR_SYSCALL) |
1 |
0 |
1 |
|
| 497 |
1 |
| capture_tls_syscall_error(self, ret); |
| 498 |
|
| else |
| 499 |
0 |
| capture_tls_error(self); |
| 500 |
1 |
| fail_connection(self, "SSL handshake failed"); |
| 501 |
|
| } |
| 502 |
1 |
| return; |
| 503 |
|
| } |
| 504 |
|
| } |
| 505 |
|
| #endif |
| 506 |
|
| |
| 507 |
107 |
▶ | if (revents & EV_WRITE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (revents & EV_WRITE) { |
27 |
80 |
107 |
|
| 508 |
27 |
▶ | if (try_write(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_write(self)) return; |
0 |
27 |
27 |
|
| 509 |
27 |
▶ | if (self->fd < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->fd < 0) return; |
0 |
27 |
27 |
|
| 510 |
27 |
▶ | if (self->pending_addendum_finish && self->send_pos >= self->send_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->pending_addendum_finish && self->send_pos >= self->send_len) { |
0 |
27 |
27 |
| if (self->pending_addendum_finish && self->send_pos >= self->send_len) { |
0 |
0 |
0 |
|
| 511 |
0 |
| self->pending_addendum_finish = 0; |
| 512 |
0 |
| self->native_state = NATIVE_IDLE; |
| 513 |
0 |
▶ | if (finish_connect(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (finish_connect(self)) return; |
0 |
0 |
0 |
|
| 514 |
|
| } |
| 515 |
|
| } |
| 516 |
|
| |
| 517 |
107 |
▶ | if (revents & EV_READ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (revents & EV_READ) { |
80 |
27 |
107 |
|
| 518 |
80 |
| on_readable(self); |
| 519 |
|
| } |
| 520 |
|
| } |
| 521 |
|
| |
| 522 |
9 |
| static void timer_cb(EV_P_ ev_timer *w, int revents) { |
| 523 |
9 |
| ev_clickhouse_t *self = (ev_clickhouse_t *)w->data; |
| 524 |
|
| (void)loop; |
| 525 |
|
| (void)revents; |
| 526 |
|
| |
| 527 |
9 |
▶ | if (self == NULL || self->magic != EV_CH_MAGIC) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (self == NULL || self->magic != EV_CH_MAGIC) return; |
9 |
0 |
9 |
| if (self == NULL || self->magic != EV_CH_MAGIC) return; |
0 |
9 |
9 |
|
| 528 |
|
| |
| 529 |
9 |
| self->timing = 0; |
| 530 |
|
| |
| 531 |
|
| /* Treat any pre-`connected=1` timeout as a connect timeout — covers |
| 532 |
|
| * TCP connect, TLS handshake, and native ServerHello stages. */ |
| 533 |
9 |
▶ | if (!self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!self->connected) { |
6 |
3 |
9 |
|
| 534 |
6 |
| stop_writing(self); |
| 535 |
6 |
| fail_connection(self, "connect timeout"); |
| 536 |
|
| } else { |
| 537 |
|
| /* query timeout */ |
| 538 |
3 |
▶ | CLEAR_SV(self->native_rows); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_rows); |
0 |
3 |
3 |
|
| 539 |
3 |
▶ | CLEAR_SV(self->native_col_names); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_names); |
0 |
3 |
3 |
|
| 540 |
3 |
▶ | CLEAR_SV(self->native_col_types); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_types); |
0 |
3 |
3 |
|
| 541 |
3 |
▶ | CLEAR_SV(self->native_totals); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_totals); |
0 |
3 |
3 |
|
| 542 |
3 |
▶ | CLEAR_SV(self->native_extremes); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_extremes); |
0 |
3 |
3 |
|
| 543 |
3 |
| lc_free_dicts(self); |
| 544 |
3 |
▶ | CLEAR_INSERT(self); free_insert_default_state(self); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(self); free_insert_default_state(self); |
0 |
3 |
3 |
| CLEAR_INSERT(self); free_insert_default_state(self); |
0 |
3 |
3 |
|
| 545 |
3 |
▶ | CLEAR_STR(self->insert_err); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->insert_err); |
0 |
3 |
3 |
|
| 546 |
3 |
| self->native_state = NATIVE_IDLE; |
| 547 |
3 |
▶ | if (self->send_count > 0) self->send_count--; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_count > 0) self->send_count--; |
3 |
0 |
3 |
|
| 548 |
|
| |
| 549 |
3 |
| int gen = self->connect_gen; |
| 550 |
|
| /* Must reconnect — server may still be processing */ |
| 551 |
3 |
▶ | if (teardown_after_deliver(self, "query timeout", "query timeout")) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (teardown_after_deliver(self, "query timeout", "query timeout")) return; |
0 |
3 |
3 |
|
| 552 |
3 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
0 |
3 |
3 |
|
| 553 |
3 |
▶ | if (self->auto_reconnect && self->host) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->auto_reconnect && self->host) |
3 |
0 |
3 |
| if (self->auto_reconnect && self->host) |
3 |
0 |
3 |
|
| 554 |
3 |
| schedule_reconnect(self); |
| 555 |
|
| } |
| 556 |
|
| } |
| 557 |
|
| |
| 558 |
|
| /* --- Keepalive timer callback --- */ |
| 559 |
|
| |
| 560 |
4 |
| static void ka_timer_cb(EV_P_ ev_timer *w, int revents) { |
| 561 |
4 |
| ev_clickhouse_t *self = (ev_clickhouse_t *)((char *)w - |
| 562 |
|
| offsetof(ev_clickhouse_t, ka_timer)); |
| 563 |
|
| (void)revents; |
| 564 |
|
| |
| 565 |
4 |
▶ | if (self->magic != EV_CH_MAGIC) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC) return; |
0 |
4 |
4 |
|
| 566 |
4 |
▶ | if (!self->connected || self->send_count > 0) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self->connected || self->send_count > 0) return; |
4 |
0 |
4 |
| if (!self->connected || self->send_count > 0) return; |
0 |
4 |
4 |
|
| 567 |
|
| |
| 568 |
|
| /* Native: enqueue like the HTTP branch below rather than writing into |
| 569 |
|
| * send_buf, so a queued query can never overwrite the unsent ping and the |
| 570 |
|
| * PONG is matched to its callback by the normal response path. The send |
| 571 |
|
| * entry carries a deadline so a server that never PONGs is detected. */ |
| 572 |
4 |
▶ | if (self->protocol == PROTO_NATIVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->protocol == PROTO_NATIVE) { |
0 |
4 |
4 |
|
| 573 |
|
| size_t ping_len; |
| 574 |
0 |
| char *ping = build_native_ping(&ping_len); |
| 575 |
0 |
| ev_ch_send_t *s = alloc_send(); |
| 576 |
0 |
| s->data = ping; |
| 577 |
0 |
| s->data_len = ping_len; |
| 578 |
0 |
| s->cb = SvREFCNT_inc(keepalive_noop_cb); |
| 579 |
0 |
| s->query_timeout = self->query_timeout > 0 |
| 580 |
0 |
▶ | ? self->query_timeout : self->keepalive; |
Branch: 1 branch
| Branch | True | False | Total |
| ? self->query_timeout : self->keepalive; |
0 |
0 |
0 |
|
| 581 |
0 |
| enqueue_send(self, s); |
| 582 |
|
| } else { |
| 583 |
|
| /* HTTP: enqueue a real ping with a no-op callback. ClickHouse's |
| 584 |
|
| * HTTP server closes idle connections after a few seconds, so |
| 585 |
|
| * relying on TCP keepalive (kernel default ~2h) is not enough. */ |
| 586 |
|
| size_t req_len; |
| 587 |
4 |
| char *req = build_http_ping_request(self, &req_len); |
| 588 |
4 |
| ev_ch_send_t *s = alloc_send(); |
| 589 |
4 |
| s->data = req; |
| 590 |
4 |
| s->data_len = req_len; |
| 591 |
4 |
| s->cb = SvREFCNT_inc(keepalive_noop_cb); |
| 592 |
8 |
| s->query_timeout = self->query_timeout > 0 |
| 593 |
4 |
▶ | ? self->query_timeout : self->keepalive; |
Branch: 1 branch
| Branch | True | False | Total |
| ? self->query_timeout : self->keepalive; |
0 |
4 |
4 |
|
| 594 |
4 |
| enqueue_send(self, s); |
| 595 |
|
| } |
| 596 |
|
| } |
| 597 |
|
| |
| 598 |
75 |
| static void start_keepalive(ev_clickhouse_t *self) { |
| 599 |
75 |
▶ | if (self->keepalive > 0 && !self->ka_timing && self->connected) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->keepalive > 0 && !self->ka_timing && self->connected) { |
4 |
71 |
75 |
| if (self->keepalive > 0 && !self->ka_timing && self->connected) { |
4 |
0 |
4 |
| if (self->keepalive > 0 && !self->ka_timing && self->connected) { |
4 |
0 |
4 |
|
| 600 |
4 |
| ev_now_update(self->loop); |
| 601 |
4 |
| ev_timer_init(&self->ka_timer, ka_timer_cb, self->keepalive, self->keepalive); |
| 602 |
4 |
| ev_timer_start(self->loop, &self->ka_timer); |
| 603 |
4 |
| self->ka_timing = 1; |
| 604 |
|
| } |
| 605 |
75 |
| } |
| 606 |
|
| |
| 607 |
242 |
| static void stop_keepalive(ev_clickhouse_t *self) { |
| 608 |
242 |
▶ | if (self->ka_timing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->ka_timing) { |
4 |
238 |
242 |
|
| 609 |
4 |
| ev_timer_stop(self->loop, &self->ka_timer); |
| 610 |
4 |
| self->ka_timing = 0; |
| 611 |
|
| } |
| 612 |
242 |
| } |
| 613 |
|
| |
| 614 |
|
| /* --- Reconnect with backoff --- */ |
| 615 |
|
| |
| 616 |
19 |
| static void reconnect_timer_cb(EV_P_ ev_timer *w, int revents) { |
| 617 |
19 |
| ev_clickhouse_t *self = (ev_clickhouse_t *)((char *)w - |
| 618 |
|
| offsetof(ev_clickhouse_t, reconnect_timer)); |
| 619 |
|
| (void)revents; (void)loop; |
| 620 |
19 |
| self->reconnect_timing = 0; |
| 621 |
19 |
▶ | if (self->magic != EV_CH_MAGIC || self->connected || self->connecting) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC || self->connected || self->connecting) return; |
19 |
0 |
19 |
| if (self->magic != EV_CH_MAGIC || self->connected || self->connecting) return; |
19 |
0 |
19 |
| if (self->magic != EV_CH_MAGIC || self->connected || self->connecting) return; |
0 |
19 |
19 |
|
| 622 |
19 |
| start_connect(self); |
| 623 |
|
| } |
| 624 |
|
| |
| 625 |
20 |
| static void schedule_reconnect(ev_clickhouse_t *self) { |
| 626 |
20 |
▶ | if (!self->auto_reconnect || !self->host || self->magic != EV_CH_MAGIC) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!self->auto_reconnect || !self->host || self->magic != EV_CH_MAGIC) return; |
20 |
0 |
20 |
| if (!self->auto_reconnect || !self->host || self->magic != EV_CH_MAGIC) return; |
20 |
0 |
20 |
| if (!self->auto_reconnect || !self->host || self->magic != EV_CH_MAGIC) return; |
0 |
20 |
20 |
|
| 627 |
20 |
▶ | if (self->reconnect_max_attempts > 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_max_attempts > 0 |
16 |
4 |
20 |
|
| 628 |
16 |
▶ | && self->reconnect_attempts >= self->reconnect_max_attempts) { |
Branch: 1 branch
| Branch | True | False | Total |
| && self->reconnect_attempts >= self->reconnect_max_attempts) { |
1 |
15 |
16 |
|
| 629 |
|
| /* Give up so the user isn't trapped in an infinite loop on a |
| 630 |
|
| * permanent failure (bad host, wrong creds). Drain any queries |
| 631 |
|
| * still in send_queue first so their callbacks see the failure |
| 632 |
|
| * instead of being silently orphaned. */ |
| 633 |
1 |
| int gen = self->connect_gen; |
| 634 |
1 |
| emit_error(self, "max reconnect attempts exceeded"); |
| 635 |
1 |
▶ | if (check_destroyed(self)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) return; |
0 |
1 |
1 |
|
| 636 |
|
| /* reset() from on_error won: the new generation owns the |
| 637 |
|
| * queues — failing them here would kill its queries. */ |
| 638 |
1 |
▶ | if (self->connect_gen != gen) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen) return; |
0 |
1 |
1 |
|
| 639 |
1 |
| (void)cancel_pending(self, "max reconnect attempts exceeded", 1); |
| 640 |
1 |
| return; |
| 641 |
|
| } |
| 642 |
|
| /* Always defer through ev_timer so a synchronous start_connect failure |
| 643 |
|
| * (e.g. getaddrinfo error) cannot cause unbounded fail_connection -> |
| 644 |
|
| * schedule_reconnect -> start_connect recursion on the C stack. A |
| 645 |
|
| * delay of 0 fires on the next event-loop iteration, not inline. */ |
| 646 |
19 |
▶ | double delay = self->reconnect_delay > 0 ? self->reconnect_delay : 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| double delay = self->reconnect_delay > 0 ? self->reconnect_delay : 0.0; |
19 |
0 |
19 |
|
| 647 |
|
| int i; |
| 648 |
22 |
▶ | for (i = 0; i < self->reconnect_attempts && i < 20; i++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < self->reconnect_attempts && i < 20; i++) |
3 |
19 |
22 |
| for (i = 0; i < self->reconnect_attempts && i < 20; i++) |
3 |
0 |
3 |
|
| 649 |
3 |
| delay *= 2; |
| 650 |
19 |
▶ | if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
0 |
19 |
19 |
| if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
0 |
0 |
0 |
|
| 651 |
0 |
| delay = self->reconnect_max_delay; |
| 652 |
|
| /* Apply jitter AFTER the cap so a configured ceiling isn't silently |
| 653 |
|
| * exceeded. rand() / RAND_MAX is uniform in [0, 1]; clamping to the |
| 654 |
|
| * cap again keeps the worst case bounded. */ |
| 655 |
19 |
▶ | if (self->reconnect_jitter > 0 && delay > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->reconnect_jitter > 0 && delay > 0) { |
0 |
19 |
19 |
| if (self->reconnect_jitter > 0 && delay > 0) { |
0 |
0 |
0 |
|
| 656 |
0 |
| double j = ((double)rand() / (double)RAND_MAX) * self->reconnect_jitter; |
| 657 |
0 |
| delay += delay * j; |
| 658 |
0 |
▶ | if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
0 |
0 |
0 |
| if (self->reconnect_max_delay > 0 && delay > self->reconnect_max_delay) |
0 |
0 |
0 |
|
| 659 |
0 |
| delay = self->reconnect_max_delay; |
| 660 |
|
| } |
| 661 |
19 |
| self->reconnect_attempts++; |
| 662 |
19 |
▶ | if (self->reconnect_timing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->reconnect_timing) { |
0 |
19 |
19 |
|
| 663 |
0 |
| ev_timer_stop(self->loop, &self->reconnect_timer); |
| 664 |
0 |
| self->reconnect_timing = 0; |
| 665 |
|
| } |
| 666 |
19 |
| ev_now_update(self->loop); |
| 667 |
19 |
| ev_timer_init(&self->reconnect_timer, reconnect_timer_cb, delay, 0); |
| 668 |
19 |
| ev_timer_start(self->loop, &self->reconnect_timer); |
| 669 |
19 |
| self->reconnect_timing = 1; |
| 670 |
|
| } |
| 671 |
|
| |
| 672 |
|
| /* Free LowCardinality cross-block dictionary state */ |
| 673 |
165 |
| static void lc_free_dicts(ev_clickhouse_t *self) { |
| 674 |
165 |
▶ | if (self->lc_dicts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->lc_dicts) { |
0 |
165 |
165 |
|
| 675 |
|
| int c; |
| 676 |
0 |
▶ | for (c = 0; c < self->lc_num_cols; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < self->lc_num_cols; c++) { |
0 |
0 |
0 |
|
| 677 |
0 |
▶ | if (self->lc_dicts[c]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->lc_dicts[c]) { |
0 |
0 |
0 |
|
| 678 |
|
| uint64_t j; |
| 679 |
0 |
▶ | for (j = 0; j < self->lc_dict_sizes[c]; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < self->lc_dict_sizes[c]; j++) |
0 |
0 |
0 |
|
| 680 |
0 |
| SvREFCNT_dec(self->lc_dicts[c][j]); |
| 681 |
0 |
| Safefree(self->lc_dicts[c]); |
| 682 |
|
| } |
| 683 |
|
| } |
| 684 |
0 |
| Safefree(self->lc_dicts); |
| 685 |
0 |
| Safefree(self->lc_dict_sizes); |
| 686 |
0 |
| self->lc_dicts = NULL; |
| 687 |
0 |
| self->lc_dict_sizes = NULL; |
| 688 |
0 |
| self->lc_num_cols = 0; |
| 689 |
|
| } |
| 690 |
165 |
| } |
| 691 |
|
| |
| 692 |
|
| /* --- Pipeline orchestrator --- */ |
| 693 |
|
| |
| 694 |
|
| /* Send one request at a time, wait for response, then send the next. |
| 695 |
|
| * Returns 1 if self was freed (caller must not access self). */ |
| 696 |
61 |
| static int pipeline_advance(ev_clickhouse_t *self) { |
| 697 |
61 |
▶ | if (!self->connected || self->in_dispatch) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self->connected || self->in_dispatch) return 0; |
61 |
0 |
61 |
| if (!self->connected || self->in_dispatch) return 0; |
0 |
61 |
61 |
|
| 698 |
|
| |
| 699 |
61 |
▶ | if (self->send_count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->send_count > 0) { |
61 |
0 |
61 |
|
| 700 |
0 |
| start_reading(self); |
| 701 |
0 |
| return 0; |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| /* A cancel() from on_query_start drops the staged entry below and |
| 705 |
|
| * loops back here so the next queued query dispatches instead of |
| 706 |
|
| * stranding. Each iteration shrinks the queue or dispatches, so |
| 707 |
|
| * the loop terminates. */ |
| 708 |
62 |
| again: |
| 709 |
|
| |
| 710 |
|
| /* Check drain callback when all pending work is done */ |
| 711 |
62 |
▶ | if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0 |
13 |
49 |
62 |
| if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0 |
13 |
0 |
13 |
|
| 712 |
13 |
▶ | && self->on_drain) { |
Branch: 1 branch
| Branch | True | False | Total |
| && self->on_drain) { |
0 |
13 |
13 |
|
| 713 |
0 |
| SV *drain_cb = self->on_drain; |
| 714 |
0 |
| self->on_drain = NULL; |
| 715 |
0 |
▶ | if (fire_zero_arg_cb(self, drain_cb, "drain")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fire_zero_arg_cb(self, drain_cb, "drain")) { |
0 |
0 |
0 |
|
| 716 |
0 |
| SvREFCNT_dec(drain_cb); |
| 717 |
0 |
| return 1; |
| 718 |
|
| } |
| 719 |
|
| /* Dropping the last ref to the drain CV can free a closure that |
| 720 |
|
| * captured $ch — DESTROY then runs. Guard with callback_depth so |
| 721 |
|
| * the free is deferred, then detect it before touching self. */ |
| 722 |
0 |
| self->callback_depth++; |
| 723 |
0 |
| SvREFCNT_dec(drain_cb); |
| 724 |
0 |
| self->callback_depth--; |
| 725 |
0 |
▶ | if (check_destroyed(self)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) return 1; |
0 |
0 |
0 |
|
| 726 |
|
| } |
| 727 |
|
| |
| 728 |
|
| /* Restart keepalive timer when idle (start_keepalive is a no-op if already |
| 729 |
|
| * timing or if keepalive disabled) */ |
| 730 |
62 |
▶ | if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0) |
13 |
49 |
62 |
| if (ngx_queue_empty(&self->send_queue) && self->pending_count == 0) |
13 |
0 |
13 |
|
| 731 |
13 |
| start_keepalive(self); |
| 732 |
|
| |
| 733 |
|
| /* send next request from queue */ |
| 734 |
62 |
▶ | if (!ngx_queue_empty(&self->send_queue)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ngx_queue_empty(&self->send_queue)) { |
49 |
13 |
62 |
|
| 735 |
|
| /* Stop keepalive during active query */ |
| 736 |
49 |
| stop_keepalive(self); |
| 737 |
49 |
| emit_trace(self, "dispatch query (pending=%d)", self->pending_count); |
| 738 |
|
| |
| 739 |
|
| /* on_trace is user code: it may have called finish()/reset() or |
| 740 |
|
| * dropped the last reference (DESTROY), any of which drains or |
| 741 |
|
| * frees the send queue. Re-validate the connection and re-derive |
| 742 |
|
| * the head entry AFTER the trace so we never touch a send struct |
| 743 |
|
| * that cancel_pending has already released to the freelist. */ |
| 744 |
49 |
▶ | if (self->magic != EV_CH_MAGIC || !self->connected |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC || !self->connected |
49 |
0 |
49 |
| if (self->magic != EV_CH_MAGIC || !self->connected |
49 |
0 |
49 |
|
| 745 |
49 |
▶ | || ngx_queue_empty(&self->send_queue)) |
Branch: 1 branch
| Branch | True | False | Total |
| || ngx_queue_empty(&self->send_queue)) |
0 |
49 |
49 |
|
| 746 |
0 |
| return check_destroyed(self); |
| 747 |
|
| |
| 748 |
49 |
| ngx_queue_t *q = ngx_queue_head(&self->send_queue); |
| 749 |
49 |
| ev_ch_send_t *send = ngx_queue_data(q, ev_ch_send_t, queue); |
| 750 |
|
| |
| 751 |
49 |
▶ | if (send->deferred_sql) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->deferred_sql) { |
0 |
49 |
49 |
|
| 752 |
|
| /* Pre-connect native entry: build now that server_revision |
| 753 |
|
| * is known. On old servers with query parameters the build |
| 754 |
|
| * fails — fail just this entry, the rest still dispatch. */ |
| 755 |
|
| char *req; |
| 756 |
|
| size_t req_len; |
| 757 |
|
| /* The build stringifies stashed setting SVs: overload/tie |
| 758 |
|
| * runs arbitrary Perl that may destroy/reset/finish/cancel |
| 759 |
|
| * this connection, freeing the send entry and its inputs |
| 760 |
|
| * mid-build. Hold local refs/copies across the call, then |
| 761 |
|
| * re-validate everything before touching send/self. */ |
| 762 |
0 |
| size_t dsql_len = send->deferred_sql_len; |
| 763 |
|
| char *dsql; |
| 764 |
0 |
| HV *ddef = send->deferred_defaults; |
| 765 |
0 |
| HV *dovr = send->deferred_overrides; |
| 766 |
0 |
| size_t dext_len = send->deferred_ext_len; |
| 767 |
0 |
| char *dext = NULL; |
| 768 |
|
| /* Counted copies (ext is a binary blob, not NUL-terminated). */ |
| 769 |
0 |
| Newx(dsql, dsql_len + 1, char); |
| 770 |
0 |
| Copy(send->deferred_sql, dsql, dsql_len, char); |
| 771 |
0 |
| dsql[dsql_len] = '\0'; |
| 772 |
0 |
▶ | if (send->deferred_ext) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->deferred_ext) { |
0 |
0 |
0 |
|
| 773 |
0 |
▶ | Newx(dext, dext_len ? dext_len : 1, char); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(dext, dext_len ? dext_len : 1, char); |
0 |
0 |
0 |
|
| 774 |
0 |
| Copy(send->deferred_ext, dext, dext_len, char); |
| 775 |
|
| } |
| 776 |
0 |
| int gen0 = self->connect_gen; |
| 777 |
0 |
▶ | if (ddef) SvREFCNT_inc((SV *)ddef); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ddef) SvREFCNT_inc((SV *)ddef); |
0 |
0 |
0 |
|
| 778 |
0 |
▶ | if (dovr) SvREFCNT_inc((SV *)dovr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dovr) SvREFCNT_inc((SV *)dovr); |
0 |
0 |
0 |
|
| 779 |
0 |
| self->callback_depth++; |
| 780 |
0 |
| req = build_native_query(self, dsql, dsql_len, ddef, dovr, |
| 781 |
|
| dext, dext_len, &req_len); |
| 782 |
|
| /* Drop pins BEFORE depth-- (a setting holding the last |
| 783 |
|
| * $ch ref must DESTROY into depth>0). */ |
| 784 |
0 |
▶ | if (ddef) SvREFCNT_dec((SV *)ddef); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ddef) SvREFCNT_dec((SV *)ddef); |
0 |
0 |
0 |
|
| 785 |
0 |
▶ | if (dovr) SvREFCNT_dec((SV *)dovr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dovr) SvREFCNT_dec((SV *)dovr); |
0 |
0 |
0 |
|
| 786 |
0 |
| Safefree(dsql); |
| 787 |
0 |
▶ | if (dext) Safefree(dext); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dext) Safefree(dext); |
0 |
0 |
0 |
|
| 788 |
0 |
| self->callback_depth--; |
| 789 |
0 |
▶ | if (self->magic != EV_CH_MAGIC) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->magic != EV_CH_MAGIC) { |
0 |
0 |
0 |
|
| 790 |
0 |
▶ | if (req) Safefree(req); |
Branch: 1 branch
| Branch | True | False | Total |
| if (req) Safefree(req); |
0 |
0 |
0 |
|
| 791 |
0 |
| check_destroyed(self); |
| 792 |
0 |
| return 1; |
| 793 |
|
| } |
| 794 |
|
| { |
| 795 |
|
| /* reset()/finish() own the queue now; cancel() already |
| 796 |
|
| * failed the entry. Either way our send is gone. */ |
| 797 |
|
| ngx_queue_t *qq; |
| 798 |
0 |
| int still_queued = 0; |
| 799 |
0 |
▶ | if (self->connect_gen == gen0 && self->connected) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->connect_gen == gen0 && self->connected) { |
0 |
0 |
0 |
| if (self->connect_gen == gen0 && self->connected) { |
0 |
0 |
0 |
|
| 800 |
0 |
| for (qq = ngx_queue_head(&self->send_queue); |
| 801 |
0 |
▶ | qq != ngx_queue_sentinel(&self->send_queue); |
Branch: 1 branch
| Branch | True | False | Total |
| qq != ngx_queue_sentinel(&self->send_queue); |
0 |
0 |
0 |
|
| 802 |
0 |
| qq = ngx_queue_next(qq)) { |
| 803 |
0 |
▶ | if (qq == q) { still_queued = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (qq == q) { still_queued = 1; break; } |
0 |
0 |
0 |
|
| 804 |
|
| } |
| 805 |
|
| } |
| 806 |
0 |
▶ | if (!still_queued) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!still_queued) { |
0 |
0 |
0 |
|
| 807 |
0 |
▶ | if (req) Safefree(req); |
Branch: 1 branch
| Branch | True | False | Total |
| if (req) Safefree(req); |
0 |
0 |
0 |
|
| 808 |
0 |
| return 0; |
| 809 |
|
| } |
| 810 |
|
| } |
| 811 |
0 |
| free_send_deferred(send); |
| 812 |
0 |
▶ | if (!req) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!req) { |
0 |
0 |
0 |
|
| 813 |
0 |
| SV *fail_cb = send->cb; |
| 814 |
0 |
| SV *fail_oqc = send->on_complete; |
| 815 |
|
| char errmsg[64]; |
| 816 |
0 |
| int gen0 = self->connect_gen; |
| 817 |
0 |
| unsigned seq0 = self->dispatch_seq; |
| 818 |
|
| /* Adopt the failed entry's own query_id for the fire so |
| 819 |
|
| * on_query_complete doesn't report a stale earlier id |
| 820 |
|
| * (same idiom as cancel_pending). */ |
| 821 |
0 |
| char *qid0 = self->last_query_id; |
| 822 |
0 |
| self->last_query_id = send->query_id; |
| 823 |
0 |
| send->query_id = NULL; |
| 824 |
0 |
| send->on_complete = NULL; |
| 825 |
0 |
| ngx_queue_remove(q); |
| 826 |
0 |
▶ | CLEAR_INSERT(send); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(send); |
0 |
0 |
0 |
| CLEAR_INSERT(send); |
0 |
0 |
0 |
|
| 827 |
0 |
▶ | CLEAR_SV(send->on_data); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(send->on_data); |
0 |
0 |
0 |
|
| 828 |
0 |
| release_send(send); |
| 829 |
0 |
| self->pending_count--; |
| 830 |
0 |
| snprintf(errmsg, sizeof(errmsg), |
| 831 |
|
| "query parameters require server revision >= %d", |
| 832 |
|
| DBMS_MIN_REVISION_WITH_PARAMETERS); |
| 833 |
0 |
| self->callback_depth++; |
| 834 |
0 |
| self->force_zero_dur = 1; |
| 835 |
0 |
| fire_err_and_complete(self, fail_cb, errmsg, fail_oqc); |
| 836 |
0 |
| self->force_zero_dur = 0; |
| 837 |
0 |
| self->callback_depth--; |
| 838 |
0 |
▶ | if (check_destroyed(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (check_destroyed(self)) { |
0 |
0 |
0 |
|
| 839 |
|
| /* Adopted id died with the struct; ours to free. */ |
| 840 |
0 |
▶ | if (qid0) Safefree(qid0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (qid0) Safefree(qid0); |
0 |
0 |
0 |
|
| 841 |
0 |
| return 1; |
| 842 |
|
| } |
| 843 |
0 |
▶ | if (self->dispatch_seq == seq0 && self->connect_gen == gen0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->dispatch_seq == seq0 && self->connect_gen == gen0) { |
0 |
0 |
0 |
| if (self->dispatch_seq == seq0 && self->connect_gen == gen0) { |
0 |
0 |
0 |
|
| 844 |
0 |
▶ | CLEAR_STR(self->last_query_id); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->last_query_id); |
0 |
0 |
0 |
|
| 845 |
0 |
| self->last_query_id = qid0; |
| 846 |
0 |
▶ | } else if (qid0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (qid0) { |
0 |
0 |
0 |
|
| 847 |
0 |
| Safefree(qid0); |
| 848 |
|
| } |
| 849 |
|
| /* A reset() from the handler owns the queue now — the |
| 850 |
|
| * inner frame dispatches it (same as cancel_pending). */ |
| 851 |
0 |
▶ | if (self->connect_gen != gen0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->connect_gen != gen0) return 0; |
0 |
0 |
0 |
|
| 852 |
0 |
| goto again; |
| 853 |
|
| } |
| 854 |
0 |
| send->data = req; |
| 855 |
0 |
| send->data_len = req_len; |
| 856 |
|
| } |
| 857 |
|
| |
| 858 |
|
| /* set up send buffer */ |
| 859 |
49 |
| ensure_send_cap(self, send->data_len); |
| 860 |
49 |
| Copy(send->data, self->send_buf, send->data_len, char); |
| 861 |
49 |
| self->send_len = send->data_len; |
| 862 |
49 |
| self->send_pos = 0; |
| 863 |
|
| |
| 864 |
|
| /* move cb to recv queue */ |
| 865 |
49 |
| SV *dispatched_cb = send->cb; |
| 866 |
49 |
| ngx_queue_remove(q); |
| 867 |
49 |
| push_cb_owned_ex(self, send->cb, send->raw, |
| 868 |
|
| send->on_data, send->on_complete, |
| 869 |
|
| send->query_timeout); |
| 870 |
49 |
▶ | CLEAR_SV(send->on_data); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(send->on_data); |
0 |
49 |
49 |
|
| 871 |
49 |
| send->on_complete = NULL; /* ownership transferred to cbt */ |
| 872 |
|
| /* Track query_id + dispatch start time (used by on_query_complete). */ |
| 873 |
49 |
▶ | CLEAR_STR(self->last_query_id); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->last_query_id); |
1 |
48 |
49 |
|
| 874 |
49 |
▶ | if (send->query_id) { self->last_query_id = send->query_id; send->query_id = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->query_id) { self->last_query_id = send->query_id; send->query_id = NULL; } |
2 |
47 |
49 |
|
| 875 |
49 |
▶ | CLEAR_STR(self->query_session_tz); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_STR(self->query_session_tz); |
0 |
49 |
49 |
|
| 876 |
49 |
| self->query_session_tz = send->session_tz; |
| 877 |
49 |
| send->session_tz = NULL; |
| 878 |
49 |
| ev_now_update(self->loop); |
| 879 |
49 |
| self->query_start_time = ev_now(self->loop); |
| 880 |
49 |
| self->dispatch_seq++; |
| 881 |
|
| |
| 882 |
|
| /* on_query_start: fire with the resolved query_id, just before |
| 883 |
|
| * the write side runs. Suppressed for keepalive PINGs to match |
| 884 |
|
| * on_query_complete semantics. */ |
| 885 |
49 |
▶ | if (self->on_query_start && !IS_KEEPALIVE_CB(dispatched_cb)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (self->on_query_start && !IS_KEEPALIVE_CB(dispatched_cb)) { |
2 |
47 |
49 |
| if (self->on_query_start && !IS_KEEPALIVE_CB(dispatched_cb)) { |
2 |
0 |
2 |
| if (self->on_query_start && !IS_KEEPALIVE_CB(dispatched_cb)) { |
2 |
0 |
2 |
|
| 886 |
2 |
| dSP; |
| 887 |
2 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
2 |
2 |
|
| 888 |
2 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
2 |
2 |
|
| 889 |
2 |
▶ | PUSHs(self->last_query_id |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(self->last_query_id |
2 |
0 |
2 |
|
| 890 |
|
| ? sv_2mortal(newSVpv(self->last_query_id, 0)) |
| 891 |
|
| : &PL_sv_undef); |
| 892 |
2 |
| PUTBACK; |
| 893 |
2 |
| int gen_before = self->connect_gen; |
| 894 |
2 |
| self->callback_depth++; |
| 895 |
|
| /* Visible to cancel(): the entry is dequeued (and its bytes |
| 896 |
|
| * staged in send_buf) but send_count isn't bumped yet. */ |
| 897 |
2 |
| self->in_dispatch = 1; |
| 898 |
2 |
| PINNED_CALL_SV(self->on_query_start, G_EVAL | G_VOID | G_DISCARD); |
| 899 |
2 |
| self->in_dispatch = 0; |
| 900 |
2 |
| self->callback_depth--; |
| 901 |
2 |
▶ | WARN_AND_CLEAR_ERRSV("on_query_start"); |
Branch: 4 branches
| Branch | True | False | Total |
| WARN_AND_CLEAR_ERRSV("on_query_start"); |
2 |
0 |
2 |
| WARN_AND_CLEAR_ERRSV("on_query_start"); |
0 |
2 |
2 |
| WARN_AND_CLEAR_ERRSV("on_query_start"); |
0 |
0 |
0 |
| WARN_AND_CLEAR_ERRSV("on_query_start"); |
0 |
0 |
0 |
|
| 902 |
2 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 903 |
|
| /* The handler may have torn the connection down: DESTROY |
| 904 |
|
| * (dropped the last ref), reset() (rotated it — connect_gen |
| 905 |
|
| * bumped), or finish() (closed it — connected cleared). In |
| 906 |
|
| * every case the cb was already delivered by cancel_pending; |
| 907 |
|
| * drop the local send entry (already dequeued, so cancel_pending |
| 908 |
|
| * never saw it) without dispatching it. Falling through on a |
| 909 |
|
| * !connected struct would re-arm a stray query-timeout timer |
| 910 |
|
| * and leave send_count stuck at 1. The `||` short-circuits |
| 911 |
|
| * before reading self->connect_gen when self has been freed. */ |
| 912 |
2 |
| int destroyed = check_destroyed(self); |
| 913 |
2 |
▶ | if (destroyed || self->connect_gen != gen_before |
Branch: 2 branches
| Branch | True | False | Total |
| if (destroyed || self->connect_gen != gen_before |
2 |
0 |
2 |
| if (destroyed || self->connect_gen != gen_before |
2 |
0 |
2 |
|
| 914 |
2 |
▶ | || !self->connected) { |
Branch: 1 branch
| Branch | True | False | Total |
| || !self->connected) { |
0 |
2 |
2 |
|
| 915 |
0 |
| Safefree(send->data); |
| 916 |
0 |
▶ | CLEAR_INSERT(send); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(send); |
0 |
0 |
0 |
| CLEAR_INSERT(send); |
0 |
0 |
0 |
|
| 917 |
0 |
| release_send(send); |
| 918 |
|
| /* A nested cancel() may have armed this before the |
| 919 |
|
| * handler tore the connection down — don't let the |
| 920 |
|
| * stale flag drop a healthy later dispatch. (When |
| 921 |
|
| * destroyed, self is already freed: touch nothing.) */ |
| 922 |
0 |
▶ | if (!destroyed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!destroyed) |
0 |
0 |
0 |
|
| 923 |
0 |
| self->dispatch_cancelled = 0; |
| 924 |
0 |
| return destroyed ? 1 : 0; |
| 925 |
|
| } |
| 926 |
2 |
▶ | if (self->dispatch_cancelled) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->dispatch_cancelled) { |
1 |
1 |
2 |
|
| 927 |
|
| /* cancel() from on_query_start: the callback was |
| 928 |
|
| * already failed with "query cancelled" — drop the |
| 929 |
|
| * staged entry without writing anything and take the |
| 930 |
|
| * next queued query. */ |
| 931 |
1 |
| self->dispatch_cancelled = 0; |
| 932 |
1 |
| Safefree(send->data); |
| 933 |
1 |
▶ | CLEAR_INSERT(send); |
Branch: 2 branches
| Branch | True | False | Total |
| CLEAR_INSERT(send); |
0 |
1 |
1 |
| CLEAR_INSERT(send); |
0 |
1 |
1 |
|
| 934 |
1 |
| release_send(send); |
| 935 |
1 |
| goto again; |
| 936 |
|
| } |
| 937 |
|
| } |
| 938 |
|
| |
| 939 |
|
| /* Clear per-query accumulated state so accessors don't return |
| 940 |
|
| * the previous query's data. native_rows is already NULL at |
| 941 |
|
| * EndOfStream; col_names/types are also cleared here so DDL |
| 942 |
|
| * (or any query that emits no DATA block) does not leave the |
| 943 |
|
| * previous SELECT's schema visible. */ |
| 944 |
48 |
▶ | CLEAR_SV(self->native_col_names); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_names); |
0 |
48 |
48 |
|
| 945 |
48 |
▶ | CLEAR_SV(self->native_col_types); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_col_types); |
0 |
48 |
48 |
|
| 946 |
48 |
▶ | CLEAR_SV(self->native_totals); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_totals); |
0 |
48 |
48 |
|
| 947 |
48 |
▶ | CLEAR_SV(self->native_extremes); |
Branch: 1 branch
| Branch | True | False | Total |
| CLEAR_SV(self->native_extremes); |
0 |
48 |
48 |
|
| 948 |
48 |
| self->cancelled = 0; |
| 949 |
48 |
| self->last_error_code = 0; |
| 950 |
48 |
| self->profile_rows = 0; |
| 951 |
48 |
| self->profile_bytes = 0; |
| 952 |
48 |
| self->profile_rows_before_limit = 0; |
| 953 |
48 |
▶ | if (self->progress_period > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->progress_period > 0) { |
0 |
48 |
48 |
|
| 954 |
0 |
| memset(self->progress_acc, 0, sizeof(self->progress_acc)); |
| 955 |
0 |
| self->progress_last = 0.0; |
| 956 |
|
| } |
| 957 |
|
| |
| 958 |
|
| /* transfer deferred insert data from send entry to self */ |
| 959 |
48 |
▶ | if (send->insert_data) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->insert_data) { |
0 |
48 |
48 |
|
| 960 |
0 |
| self->insert_data = send->insert_data; |
| 961 |
0 |
| self->insert_data_len = send->insert_data_len; |
| 962 |
0 |
| send->insert_data = NULL; |
| 963 |
|
| } |
| 964 |
48 |
▶ | if (send->insert_av) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->insert_av) { |
16 |
32 |
48 |
|
| 965 |
16 |
| self->insert_av = send->insert_av; |
| 966 |
16 |
| send->insert_av = NULL; |
| 967 |
|
| } |
| 968 |
48 |
▶ | if (send->insert_empty_as_default) |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->insert_empty_as_default) |
0 |
48 |
48 |
|
| 969 |
0 |
| self->insert_empty_as_default = 1; |
| 970 |
48 |
▶ | if (send->insert_defaults) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (send->insert_defaults) { |
0 |
48 |
48 |
|
| 971 |
0 |
| self->insert_defaults = send->insert_defaults; |
| 972 |
0 |
| send->insert_defaults = NULL; |
| 973 |
|
| } |
| 974 |
|
| |
| 975 |
48 |
| Safefree(send->data); |
| 976 |
48 |
| double qt = send->query_timeout; |
| 977 |
48 |
| release_send(send); |
| 978 |
48 |
| self->send_count++; |
| 979 |
|
| |
| 980 |
|
| /* Start query timeout timer */ |
| 981 |
48 |
▶ | double timeout = qt > 0 ? qt : self->query_timeout; |
Branch: 1 branch
| Branch | True | False | Total |
| double timeout = qt > 0 ? qt : self->query_timeout; |
4 |
44 |
48 |
|
| 982 |
48 |
▶ | if (timeout > 0 && !self->timing) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (timeout > 0 && !self->timing) { |
4 |
44 |
48 |
| if (timeout > 0 && !self->timing) { |
4 |
0 |
4 |
|
| 983 |
4 |
| ev_timer_set(&self->timer, (ev_tstamp)timeout, 0.0); |
| 984 |
4 |
| ev_timer_start(self->loop, &self->timer); |
| 985 |
4 |
| self->timing = 1; |
| 986 |
|
| } |
| 987 |
|
| |
| 988 |
48 |
▶ | if (self->protocol == PROTO_NATIVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->protocol == PROTO_NATIVE) { |
18 |
30 |
48 |
|
| 989 |
18 |
▶ | if (self->insert_data || self->insert_av) |
Branch: 2 branches
| Branch | True | False | Total |
| if (self->insert_data || self->insert_av) |
18 |
0 |
18 |
| if (self->insert_data || self->insert_av) |
16 |
2 |
18 |
|
| 990 |
16 |
| self->native_state = NATIVE_WAIT_INSERT_META; |
| 991 |
|
| else |
| 992 |
2 |
| self->native_state = NATIVE_WAIT_RESULT; |
| 993 |
|
| } |
| 994 |
|
| |
| 995 |
48 |
| return try_write(self); |
| 996 |
|
| } |
| 997 |
13 |
| return 0; |
| 998 |
|
| } |
| 999 |
|
| |
| 1000 |
|
| /* --- OpenSSL init (must be in plain C, not inside XS BOOT) --- */ |
| 1001 |
|
| |
| 1002 |
74 |
| static void ch_openssl_init(void) { |
| 1003 |
|
| #ifdef HAVE_OPENSSL |
| 1004 |
|
| #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 1005 |
74 |
| OPENSSL_init_ssl(0, NULL); |
| 1006 |
|
| #else |
| 1007 |
|
| SSL_library_init(); |
| 1008 |
|
| SSL_load_error_strings(); |
| 1009 |
|
| OpenSSL_add_all_algorithms(); |
| 1010 |
|
| #endif |
| 1011 |
|
| #endif |
| 1012 |
74 |
| } |