3636import ssl
3737import logging
3838
39+ import keepalive
40+
3941from threading import Lock
4042from types import SimpleNamespace
4143from dataclasses import dataclass
@@ -73,11 +75,9 @@ def valid(self) -> bool:
7375
7476class Connector (object ):
7577 """
76- .. _connector-label:
77-
7878 **Class to facilitate connections with an instance of aperturedb**
7979
80- It lets the client execute any query as per the `Native Query specs <https://docs.aperturedata.io/ >`_
80+ It lets the client execute any query as per the `Native Query specs </category/aperturedb-query-language >`_
8181
8282 Args:
8383 str (host): Address of the host to connect to.
@@ -86,12 +86,17 @@ class Connector(object):
8686 str (password): Password to specify while connecting to the db.
8787 str (token): Token to use while connecting to the database.
8888 bool (use_ssl): Use SSL to encrypt communication with the database.
89+ bool (use_keepalive): Set keepalive on the connection with the database.
90+ This has two benefits: It reduces the chance of disconnection for a long-running query,
91+ and it means that diconnections are detected sooner.
92+ Turn this off to reduce traffic on high-cost network connections.
8993 Configuration (config): Configuration object to use for connection.
9094 """
9195
9296 def __init__ (self , host = "localhost" , port = 55555 ,
9397 user = "" , password = "" , token = "" ,
9498 use_ssl = True , shared_data = None , authenticate = True ,
99+ use_keepalive = True ,
95100 config : Configuration = None ):
96101 self .connected = False
97102 self .last_response = ''
@@ -101,19 +106,22 @@ def __init__(self, host="localhost", port=55555,
101106 self .host = host
102107 self .port = port
103108 self .use_ssl = use_ssl
109+ self .use_keepalive = use_keepalive
104110 self .config = Configuration (
105111 host = self .host ,
106112 port = self .port ,
107113 use_ssl = self .use_ssl ,
108114 username = user ,
109115 password = password ,
110- name = "runtime"
116+ name = "runtime" ,
117+ use_keepalive = use_keepalive
111118 )
112119 else :
113120 self .config = config
114121 self .host = config .host
115122 self .port = config .port
116123 self .use_ssl = config .use_ssl
124+ self .use_keepalive = config .use_keepalive
117125
118126 self ._connect ()
119127
@@ -228,6 +236,8 @@ def _connect(self):
228236
229237 self .conn = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
230238 self .conn .setsockopt (socket .SOL_TCP , socket .TCP_NODELAY , 1 )
239+ if self .use_keepalive :
240+ keepalive .set (self .conn )
231241
232242 # TCP_QUICKACK only supported in Linux 2.4.4+.
233243 # We use startswith for checking the platform following Python's
0 commit comments