Commit dcd511c8 authored by Guido Trotter's avatar Guido Trotter

move http.WaitForSocketCondition to utils

Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 04cdf663
......@@ -311,44 +311,6 @@ class HttpJsonConverter: # pylint: disable-msg=W0232
return serializer.LoadJson(data)
def WaitForSocketCondition(sock, event, timeout):
"""Waits for a condition to occur on the socket.
@type sock: socket
@param sock: Wait for events on this socket
@type event: int
@param event: ORed condition (see select module)
@type timeout: float or None
@param timeout: Timeout in seconds
@rtype: int or None
@return: None for timeout, otherwise occured conditions
"""
check = (event | select.POLLPRI |
select.POLLNVAL | select.POLLHUP | select.POLLERR)
if timeout is not None:
# Poller object expects milliseconds
timeout *= 1000
poller = select.poll()
poller.register(sock, event)
try:
while True:
# TODO: If the main thread receives a signal and we have no timeout, we
# could wait forever. This should check a global "quit" flag or
# something every so often.
io_events = poller.poll(timeout)
if not io_events:
# Timeout
return None
for (_, evcond) in io_events:
if evcond & check:
return evcond
finally:
poller.unregister(sock)
def SocketOperation(sock, op, arg1, timeout):
"""Wrapper around socket functions.
......@@ -399,7 +361,7 @@ def SocketOperation(sock, op, arg1, timeout):
else:
wait_for_event = event_poll
event = WaitForSocketCondition(sock, wait_for_event, timeout)
event = utils.WaitForSocketCondition(sock, wait_for_event, timeout)
if event is None:
raise HttpSocketTimeout()
......
......@@ -37,6 +37,7 @@ import threading
from ganeti import workerpool
from ganeti import http
from ganeti import utils
HTTP_CLIENT_THREADS = 10
......@@ -249,8 +250,8 @@ class HttpClientRequestExecutor(http.HttpBase):
if not connected:
# Wait for connection
event = http.WaitForSocketCondition(self.sock, select.POLLOUT,
self.CONNECT_TIMEOUT)
event = utils.WaitForSocketCondition(self.sock, select.POLLOUT,
self.CONNECT_TIMEOUT)
if event is None:
raise http.HttpError("Timeout while connecting to server")
......
......@@ -1497,6 +1497,44 @@ except NameError:
return False
def WaitForSocketCondition(sock, event, timeout):
"""Waits for a condition to occur on the socket.
@type sock: socket
@param sock: Wait for events on this socket
@type event: int
@param event: ORed condition (see select module)
@type timeout: float or None
@param timeout: Timeout in seconds
@rtype: int or None
@return: None for timeout, otherwise occured conditions
"""
check = (event | select.POLLPRI |
select.POLLNVAL | select.POLLHUP | select.POLLERR)
if timeout is not None:
# Poller object expects milliseconds
timeout *= 1000
poller = select.poll()
poller.register(sock, event)
try:
while True:
# TODO: If the main thread receives a signal and we have no timeout, we
# could wait forever. This should check a global "quit" flag or
# something every so often.
io_events = poller.poll(timeout)
if not io_events:
# Timeout
return None
for (_, evcond) in io_events:
if evcond & check:
return evcond
finally:
poller.unregister(sock)
def partition(seq, pred=bool): # # pylint: disable-msg=W0622
"Partition a list in two, based on the given predicate"
return (list(itertools.ifilter(pred, seq)),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment