|
28 | 28 | IPADDR_SAFE as _IPADDR_SAFE)
|
29 | 29 |
|
30 | 30 | from bson import DEFAULT_CODEC_OPTIONS
|
31 |
| -from bson.py3compat import imap, itervalues, _unicode, integer_types |
| 31 | +from bson.py3compat import imap, itervalues, _unicode |
32 | 32 | from bson.son import SON
|
33 | 33 | from pymongo import auth, helpers, thread_util, __version__
|
34 | 34 | from pymongo.client_session import _validate_session_write_concern
|
@@ -132,31 +132,36 @@ def _set_non_inheritable_non_atomic(dummy):
|
132 | 132 | except ImportError:
|
133 | 133 | import winreg
|
134 | 134 |
|
| 135 | + def _query(key, name, default): |
| 136 | + try: |
| 137 | + value, _ = winreg.QueryValueEx(key, name) |
| 138 | + # Ensure the value is a number or raise ValueError. |
| 139 | + return int(value) |
| 140 | + except (OSError, ValueError): |
| 141 | + # QueryValueEx raises OSError when the key does not exist (i.e. |
| 142 | + # the system is using the Windows default value). |
| 143 | + return default |
| 144 | + |
135 | 145 | try:
|
136 | 146 | with winreg.OpenKey(
|
137 | 147 | winreg.HKEY_LOCAL_MACHINE,
|
138 | 148 | r"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters") as key:
|
139 |
| - _DEFAULT_TCP_IDLE_MS, _ = winreg.QueryValueEx(key, "KeepAliveTime") |
140 |
| - _DEFAULT_TCP_INTERVAL_MS, _ = winreg.QueryValueEx( |
141 |
| - key, "KeepAliveInterval") |
142 |
| - # Make sure these are integers. |
143 |
| - if not isinstance(_DEFAULT_TCP_IDLE_MS, integer_types): |
144 |
| - raise ValueError |
145 |
| - if not isinstance(_DEFAULT_TCP_INTERVAL_MS, integer_types): |
146 |
| - raise ValueError |
147 |
| - except (OSError, ValueError): |
148 |
| - # We could not check the default values so do not attempt to override. |
149 |
| - def _set_keepalive_times(dummy): |
150 |
| - pass |
151 |
| - else: |
152 |
| - def _set_keepalive_times(sock): |
153 |
| - idle_ms = min(_DEFAULT_TCP_IDLE_MS, _MAX_TCP_KEEPIDLE * 1000) |
154 |
| - interval_ms = min(_DEFAULT_TCP_INTERVAL_MS, |
155 |
| - _MAX_TCP_KEEPINTVL * 1000) |
156 |
| - if (idle_ms < _DEFAULT_TCP_IDLE_MS or |
157 |
| - interval_ms < _DEFAULT_TCP_INTERVAL_MS): |
158 |
| - sock.ioctl(socket.SIO_KEEPALIVE_VALS, |
159 |
| - (1, idle_ms, interval_ms)) |
| 149 | + _WINDOWS_TCP_IDLE_MS = _query(key, "KeepAliveTime", 7200000) |
| 150 | + _WINDOWS_TCP_INTERVAL_MS = _query(key, "KeepAliveInterval", 1000) |
| 151 | + except OSError: |
| 152 | + # We could not check the default values because winreg.OpenKey failed. |
| 153 | + # Assume the system is using the default values. |
| 154 | + _WINDOWS_TCP_IDLE_MS = 7200000 |
| 155 | + _WINDOWS_TCP_INTERVAL_MS = 1000 |
| 156 | + |
| 157 | + def _set_keepalive_times(sock): |
| 158 | + idle_ms = min(_WINDOWS_TCP_IDLE_MS, _MAX_TCP_KEEPIDLE * 1000) |
| 159 | + interval_ms = min(_WINDOWS_TCP_INTERVAL_MS, |
| 160 | + _MAX_TCP_KEEPINTVL * 1000) |
| 161 | + if (idle_ms < _WINDOWS_TCP_IDLE_MS or |
| 162 | + interval_ms < _WINDOWS_TCP_INTERVAL_MS): |
| 163 | + sock.ioctl(socket.SIO_KEEPALIVE_VALS, |
| 164 | + (1, idle_ms, interval_ms)) |
160 | 165 | else:
|
161 | 166 | def _set_tcp_option(sock, tcp_option, max_value):
|
162 | 167 | if hasattr(socket, tcp_option):
|
|
0 commit comments