Skip to content

Commit

Permalink
Retry keep alive disconnects (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
genzgd authored Sep 28, 2022
1 parent ed6a863 commit a0c4fd5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@

* In the next minor release (0.3.0) the row_binary option for ClickHouse serialization will be removed. The performance is significantly lower than Native format and maintaining the option adds complexity with no corresponding benefit

### Release 0.2.10 2022-09-28

#### Bug Fix
* Add single retry for HTTP RemoteDisconnected errors from the ClickHouse Server. This prevents exception spam when requests (in particular inserts) are sent at approximately the same time as the ClickHouse server closes a keep alive connection.

### Release 0.2.9 2022-09-24

#### Bug Fix
* Fix incorrect validation errors in the Superset connection dialog


### Release 0.2.8 2022-09-21

#### Improvements
Expand Down
2 changes: 1 addition & 1 deletion clickhouse_connect/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.9
0.2.10
2 changes: 1 addition & 1 deletion clickhouse_connect/driver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def query(self,
use_none: bool = True,
context: QueryContext = None) -> QueryResult:
"""
Main query method for SELECT, DESCRIBE and other commands that result a result matrix
Main query method for SELECT, DESCRIBE and other SQL statements that return a result matrix
:param query: Query statement/format string
:param parameters: Optional dictionary used to format the query
:param settings: Optional dictionary of ClickHouse settings (key/string values)
Expand Down
12 changes: 12 additions & 0 deletions clickhouse_connect/driver/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import atexit
import re
import http as PyHttp
from http.client import RemoteDisconnected

from typing import Optional, Dict, Any, Sequence, Union, List
from requests import Session, Response, get as req_get
Expand Down Expand Up @@ -265,6 +266,17 @@ def _raw_request(self,
data=data,
params=params)
except RequestException as ex:
rex_context = ex.__context__
if rex_context and isinstance(rex_context.__context__, RemoteDisconnected):
# See https://github.com/psf/requests/issues/4664
# The server closed the connection and it is probably because the Keep Alive has expired
# We should be safe to retry, as ClickHouse should not have processed anything on a connection
# that it killed. We also only retry this once, as multiple disconnects are unlikely to be
# related to the Keep Alive settings
if attempts == 1:
logger.debug('Retrying remotely closed connection')
attempts = 0
continue
logger.exception('Unexpected Http Driver Exception')
raise OperationalError(f'Error executing HTTP request {self.url}') from ex
if 200 <= response.status_code < 300:
Expand Down

0 comments on commit a0c4fd5

Please sign in to comment.