pgtoolkit/pgmanager.py
changeset 37 5b0eb4b11940
parent 36 e67101c22e83
child 41 6aad5e35efe8
equal deleted inserted replaced
36:e67101c22e83 37:5b0eb4b11940
   180         else:
   180         else:
   181             return super(Connection, self).cursor(name, cursor_factory=Cursor)
   181             return super(Connection, self).cursor(name, cursor_factory=Cursor)
   182 
   182 
   183     def keep_alive(self):
   183     def keep_alive(self):
   184         '''Set socket to keepalive mode. Must be called before any query.'''
   184         '''Set socket to keepalive mode. Must be called before any query.'''
   185         sock = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_STREAM) 
   185         sock = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_STREAM)
   186         sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
   186         sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
   187         try:
   187         try:
   188             # Maximum keep-alive probes before asuming the connection is lost
   188             # Maximum keep-alive probes before asuming the connection is lost
   189             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
   189             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
   190             # Interval (in seconds) between keep-alive probes
   190             # Interval (in seconds) between keep-alive probes
   191             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
   191             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 2)
   192             # Maximum idle time (in seconds) before start sending keep-alive probes
   192             # Maximum idle time (in seconds) before start sending keep-alive probes
   193             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10)
   193             sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 10)
   194         except socket.error:
   194         except socket.error:
   195             pass
   195             pass
       
   196         # close duplicated fd, options set for socket stays
       
   197         sock.close()
   196 
   198 
   197 
   199 
   198 class PgManager:
   200 class PgManager:
   199 
   201 
   200     def __init__(self):
   202     def __init__(self):