Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import time
- conn = psycopg2.connect("host='dbs' dbname='foo' user='joe' password='x'")
- time.sleep(10) # I manually turn VPN off during this sleep..
- cu = conn.cursor()
- cu.execute('SELECT 1') # <- hangs here
- print cu.fetchone()
- cu.commit()
- import socket
- socket.setdefaulttimeout(10)
- ..
- conn = psycopg2.connect(...
- s = socket.fromfd(conn.fileno(), socket.AF_INET, socket.SOCK_STREAM)
- s.settimeout(5)
- ..
- ...
- conn = psycopg2.connect(...
- s = socket.fromfd(conn.fileno(), socket.AF_INET, socket.SOCK_STREAM)
- s.settimeout(5)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
- ...
- s = socket.fromfd(connection.fileno(),
- socket.AF_INET, socket.SOCK_STREAM)
- # Enable sending of keep-alive messages
- s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
- # Time the connection needs to remain idle before start sending
- # keepalive probes
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, int(ceil(time)))
- # Time between individual keepalive probes
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 1)
- # The maximum number of keepalive probes should send before dropping
- # the connection
- s.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 3)
Add Comment
Please, Sign In to add comment