2019-06-17 10:06:03 +00:00
|
|
|
import enum, queue, os, queue, select, socket, sys, threading, time, traceback
|
2019-06-06 16:05:44 +00:00
|
|
|
import typing, uuid
|
2018-10-07 06:54:10 +00:00
|
|
|
from src import EventManager, Exports, IRCServer, Logging, ModuleManager
|
|
|
|
from src import Socket, utils
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2019-06-09 16:34:45 +00:00
|
|
|
VERSION = "v1.9.2"
|
2019-05-15 09:30:36 +00:00
|
|
|
SOURCE = "https://git.io/bitbot"
|
2019-02-24 08:37:42 +00:00
|
|
|
|
2019-05-01 16:13:41 +00:00
|
|
|
class TriggerResult(enum.Enum):
|
|
|
|
Return = 1
|
|
|
|
Exception = 2
|
2018-11-27 15:06:10 +00:00
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
class TriggerEventType(enum.Enum):
|
|
|
|
Action = 1
|
|
|
|
Kill = 2
|
|
|
|
class TriggerEvent(object):
|
|
|
|
def __init__(self, type: TriggerEventType,
|
|
|
|
callback: typing.Callable[[], None]=None):
|
|
|
|
self.type = type
|
|
|
|
self.callback = callback
|
|
|
|
|
2019-06-17 10:11:35 +00:00
|
|
|
class BitBotPanic(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class Bot(object):
|
2018-09-29 11:53:39 +00:00
|
|
|
def __init__(self, directory, args, cache, config, database, events,
|
2018-09-30 18:43:20 +00:00
|
|
|
exports, log, modules, timers):
|
2018-09-29 08:24:26 +00:00
|
|
|
self.directory = directory
|
2018-09-28 15:51:36 +00:00
|
|
|
self.args = args
|
2018-09-29 11:53:39 +00:00
|
|
|
self.cache = cache
|
2018-09-28 15:51:36 +00:00
|
|
|
self.config = config
|
|
|
|
self.database = database
|
|
|
|
self._events = events
|
|
|
|
self._exports = exports
|
|
|
|
self.log = log
|
|
|
|
self.modules = modules
|
2018-10-12 17:07:23 +00:00
|
|
|
self._timers = timers
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2018-09-11 17:24:34 +00:00
|
|
|
self.start_time = time.time()
|
2019-06-17 10:35:45 +00:00
|
|
|
self.running = False
|
2018-10-06 14:37:05 +00:00
|
|
|
self.servers = {}
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
self._event_queue = queue.Queue() # type: typing.Queue[TriggerEvent]
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll = select.poll()
|
|
|
|
self._write_poll = select.poll()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
self._rtrigger_server, self._rtrigger_client = socket.socketpair()
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll.register(self._rtrigger_server.fileno(), select.POLLIN)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 18:21:18 +00:00
|
|
|
self._rtrigger_lock = threading.Lock()
|
|
|
|
self._rtriggered = False
|
2019-06-07 09:39:19 +00:00
|
|
|
self._write_condition = threading.Condition()
|
2019-06-06 18:21:18 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
self._read_thread = None
|
|
|
|
self._write_thread = None
|
2018-10-06 14:45:56 +00:00
|
|
|
|
2018-11-05 12:34:18 +00:00
|
|
|
self._events.on("timer.reconnect").hook(self._timed_reconnect)
|
2018-10-07 07:01:54 +00:00
|
|
|
|
2019-06-06 16:58:35 +00:00
|
|
|
def _trigger_both(self):
|
|
|
|
self.trigger_read()
|
|
|
|
self.trigger_write()
|
|
|
|
def trigger_read(self):
|
2019-06-06 18:21:18 +00:00
|
|
|
with self._rtrigger_lock:
|
|
|
|
if not self._rtriggered:
|
|
|
|
self._rtriggered = True
|
|
|
|
self._rtrigger_client.send(b"TRIGGER")
|
2019-06-06 16:58:35 +00:00
|
|
|
def trigger_write(self):
|
2019-06-07 09:39:19 +00:00
|
|
|
with self._write_condition:
|
|
|
|
self._write_condition.notify()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2018-11-27 14:25:12 +00:00
|
|
|
def trigger(self,
|
2019-06-06 22:23:46 +00:00
|
|
|
func: typing.Optional[typing.Callable[[], typing.Any]]=None,
|
|
|
|
trigger_threads=True) -> typing.Any:
|
2018-11-27 14:25:12 +00:00
|
|
|
func = func or (lambda: None)
|
2019-02-25 10:36:17 +00:00
|
|
|
|
|
|
|
if utils.is_main_thread():
|
2018-11-27 14:25:12 +00:00
|
|
|
returned = func()
|
2019-06-06 22:23:46 +00:00
|
|
|
if trigger_threads:
|
|
|
|
self._trigger_both()
|
2018-11-27 14:25:12 +00:00
|
|
|
return returned
|
|
|
|
|
2018-12-02 10:08:58 +00:00
|
|
|
func_queue = queue.Queue(1) # type: queue.Queue[str]
|
2018-11-27 14:25:12 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _action():
|
|
|
|
try:
|
|
|
|
returned = func()
|
|
|
|
type = TriggerResult.Return
|
|
|
|
except Exception as e:
|
|
|
|
returned = e
|
|
|
|
type = TriggerResult.Exception
|
|
|
|
func_queue.put([type, returned])
|
2019-06-09 13:59:28 +00:00
|
|
|
event_item = TriggerEvent(TriggerEventType.Action, _action)
|
|
|
|
self._event_queue.put(event_item)
|
2018-11-27 14:25:12 +00:00
|
|
|
|
2018-12-10 13:34:53 +00:00
|
|
|
type, returned = func_queue.get(block=True)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 22:23:46 +00:00
|
|
|
if trigger_threads:
|
|
|
|
self._trigger_both()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-05-01 16:13:41 +00:00
|
|
|
if type == TriggerResult.Exception:
|
2018-11-27 15:06:10 +00:00
|
|
|
raise returned
|
2019-05-01 16:13:41 +00:00
|
|
|
elif type == TriggerResult.Return:
|
2018-11-27 15:06:10 +00:00
|
|
|
return returned
|
2018-10-06 14:45:56 +00:00
|
|
|
|
2019-06-17 10:06:03 +00:00
|
|
|
def panic(self, reason=None):
|
|
|
|
callback = None
|
|
|
|
|
|
|
|
if not reason == None:
|
2019-06-17 10:12:04 +00:00
|
|
|
self.log.critical("panic() called: %s", [reason])
|
2019-06-17 10:06:03 +00:00
|
|
|
|
|
|
|
exception = sys.exc_info()[1]
|
|
|
|
if exception:
|
|
|
|
def _raise():
|
|
|
|
raise exception
|
|
|
|
callback = _raise
|
|
|
|
|
|
|
|
self._event_queue.put(TriggerEvent(TriggerEventType.Kill, callback))
|
2019-06-17 10:11:35 +00:00
|
|
|
raise BitBotPanic()
|
2019-06-17 10:06:03 +00:00
|
|
|
|
2019-05-12 13:10:59 +00:00
|
|
|
def load_modules(self, safe: bool=False
|
|
|
|
) -> typing.Tuple[typing.List[str], typing.List[str]]:
|
2019-06-01 14:06:57 +00:00
|
|
|
db_blacklist = set(self.get_setting("module-blacklist", []))
|
|
|
|
db_whitelist = set(self.get_setting("module-whitelist", []))
|
|
|
|
|
|
|
|
conf_blacklist = self.config.get("module-blacklist", "").split(",")
|
|
|
|
conf_whitelist = self.config.get("module-whitelist", "").split(",")
|
|
|
|
|
|
|
|
conf_blacklist = set(filter(None, conf_blacklist))
|
|
|
|
conf_whitelist = set(filter(None, conf_whitelist))
|
|
|
|
|
|
|
|
blacklist = db_blacklist|conf_blacklist
|
|
|
|
whitelist = db_whitelist|conf_whitelist
|
|
|
|
|
2019-05-12 13:10:59 +00:00
|
|
|
return self.modules.load_modules(self, whitelist=whitelist,
|
|
|
|
blacklist=blacklist, safe=safe)
|
|
|
|
|
2018-11-05 18:23:02 +00:00
|
|
|
def add_server(self, server_id: int, connect: bool = True,
|
2019-04-30 11:05:36 +00:00
|
|
|
connection_param_args: typing.Dict[str, str]={}
|
|
|
|
) -> IRCServer.Server:
|
|
|
|
connection_params = utils.irc.IRCConnectionParameters(
|
|
|
|
*self.database.servers.get(server_id))
|
|
|
|
connection_params.args = connection_param_args
|
2018-11-05 18:23:02 +00:00
|
|
|
|
|
|
|
new_server = IRCServer.Server(self, self._events,
|
2018-11-05 18:30:14 +00:00
|
|
|
connection_params.id, connection_params.alias, connection_params)
|
2018-09-19 12:25:12 +00:00
|
|
|
self._events.on("new.server").call(server=new_server)
|
2018-11-05 11:53:33 +00:00
|
|
|
|
2018-11-24 12:14:36 +00:00
|
|
|
if not connect:
|
2018-11-05 11:53:33 +00:00
|
|
|
return new_server
|
|
|
|
|
|
|
|
self.connect(new_server)
|
|
|
|
|
2016-07-05 11:18:13 +00:00
|
|
|
return new_server
|
2018-09-30 12:28:26 +00:00
|
|
|
|
2019-04-24 11:07:30 +00:00
|
|
|
def get_server_by_id(self, id: int) -> typing.Optional[IRCServer.Server]:
|
2018-09-30 12:28:26 +00:00
|
|
|
for server in self.servers.values():
|
|
|
|
if server.id == id:
|
|
|
|
return server
|
2018-10-31 15:12:46 +00:00
|
|
|
return None
|
2019-04-24 11:07:30 +00:00
|
|
|
def get_server_by_alias(self, alias: str) -> typing.Optional[IRCServer.Server]:
|
|
|
|
alias_lower = alias.lower()
|
|
|
|
for server in self.servers.values():
|
|
|
|
if server.alias.lower() == alias_lower:
|
|
|
|
return server
|
|
|
|
return None
|
2018-09-30 12:28:26 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def connect(self, server: IRCServer.Server) -> bool:
|
2016-03-29 11:56:58 +00:00
|
|
|
try:
|
|
|
|
server.connect()
|
2019-03-08 10:12:01 +00:00
|
|
|
except Exception as e:
|
2019-03-09 20:21:15 +00:00
|
|
|
self.log.warn("Failed to connect to %s: %s",
|
2019-03-08 10:12:01 +00:00
|
|
|
[str(server), str(e)])
|
2016-03-29 11:56:58 +00:00
|
|
|
return False
|
2018-07-02 11:08:26 +00:00
|
|
|
self.servers[server.fileno()] = server
|
2019-06-07 14:07:31 +00:00
|
|
|
self._read_poll.register(server.fileno(), select.POLLIN)
|
2016-03-29 11:56:58 +00:00
|
|
|
return True
|
2017-01-27 21:41:47 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def next_send(self) -> typing.Optional[float]:
|
2018-08-28 13:36:16 +00:00
|
|
|
next = None
|
|
|
|
for server in self.servers.values():
|
2019-02-11 10:00:41 +00:00
|
|
|
timeout = server.socket.send_throttle_timeout()
|
2019-06-06 16:05:44 +00:00
|
|
|
if (server.socket.waiting_throttled_send() and
|
2019-02-11 10:00:41 +00:00
|
|
|
(next == None or timeout < next)):
|
2018-08-28 13:36:16 +00:00
|
|
|
next = timeout
|
|
|
|
return next
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def next_ping(self) -> typing.Optional[float]:
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts = []
|
|
|
|
for server in self.servers.values():
|
2018-09-11 17:25:27 +00:00
|
|
|
timeout = server.until_next_ping()
|
|
|
|
if not timeout == None:
|
|
|
|
timeouts.append(timeout)
|
2018-08-30 16:20:55 +00:00
|
|
|
if not timeouts:
|
|
|
|
return None
|
2018-08-29 13:33:27 +00:00
|
|
|
return min(timeouts)
|
2018-10-30 14:58:48 +00:00
|
|
|
|
|
|
|
def next_read_timeout(self) -> typing.Optional[float]:
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts = []
|
|
|
|
for server in self.servers.values():
|
|
|
|
timeouts.append(server.until_read_timeout())
|
2018-08-30 16:20:55 +00:00
|
|
|
if not timeouts:
|
|
|
|
return None
|
2018-08-29 13:33:27 +00:00
|
|
|
return min(timeouts)
|
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_poll_timeout(self) -> float:
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts = []
|
2018-10-12 17:07:23 +00:00
|
|
|
timeouts.append(self._timers.next())
|
2018-08-29 13:33:27 +00:00
|
|
|
timeouts.append(self.next_send())
|
|
|
|
timeouts.append(self.next_ping())
|
|
|
|
timeouts.append(self.next_read_timeout())
|
2018-09-29 11:53:39 +00:00
|
|
|
timeouts.append(self.cache.next_expiration())
|
2019-06-06 22:45:33 +00:00
|
|
|
min_secs = min([timeout for timeout in timeouts if not timeout == None])
|
|
|
|
return min_secs*1000 # return milliseconds
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def disconnect(self, server: IRCServer.Server):
|
2016-03-30 18:32:14 +00:00
|
|
|
del self.servers[server.fileno()]
|
2019-06-06 21:36:56 +00:00
|
|
|
self._trigger_both()
|
2016-03-30 18:32:14 +00:00
|
|
|
|
2018-11-05 12:27:11 +00:00
|
|
|
def _timed_reconnect(self, event: EventManager.Event):
|
2018-11-14 13:08:57 +00:00
|
|
|
if not self.reconnect(event["server_id"],
|
|
|
|
event.get("connection_params", None)):
|
2018-11-05 12:27:11 +00:00
|
|
|
event["timer"].redo()
|
2018-11-05 18:23:02 +00:00
|
|
|
def reconnect(self, server_id: int, connection_params: typing.Optional[
|
|
|
|
utils.irc.IRCConnectionParameters]=None) -> bool:
|
2019-05-01 16:13:55 +00:00
|
|
|
args = {} # type: typing.Dict[str, str]
|
|
|
|
if not connection_params == None:
|
|
|
|
args = typing.cast(utils.irc.IRCConnectionParameters,
|
|
|
|
connection_params).args
|
|
|
|
|
|
|
|
server = self.add_server(server_id, False, args)
|
2019-06-17 12:53:17 +00:00
|
|
|
server.reconnected = True
|
2016-03-30 18:32:14 +00:00
|
|
|
if self.connect(server):
|
|
|
|
self.servers[server.fileno()] = server
|
2018-11-05 12:27:11 +00:00
|
|
|
return True
|
|
|
|
return False
|
2016-04-14 15:48:17 +00:00
|
|
|
|
2018-10-30 14:58:48 +00:00
|
|
|
def set_setting(self, setting: str, value: typing.Any):
|
2018-08-05 21:41:38 +00:00
|
|
|
self.database.bot_settings.set(setting, value)
|
2018-10-30 14:58:48 +00:00
|
|
|
def get_setting(self, setting: str, default: typing.Any=None) -> typing.Any:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.get(setting, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_settings(self, pattern: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.find(pattern, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def find_settings_prefix(self, prefix: str, default: typing.Any=[]
|
|
|
|
) -> typing.List[typing.Any]:
|
2018-08-05 21:41:38 +00:00
|
|
|
return self.database.bot_settings.find_prefix(
|
2018-08-03 12:43:45 +00:00
|
|
|
prefix, default)
|
2018-10-30 14:58:48 +00:00
|
|
|
def del_setting(self, setting: str):
|
2018-08-06 13:10:14 +00:00
|
|
|
self.database.bot_settings.delete(setting)
|
2016-04-14 15:48:17 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _daemon_thread(self, target: typing.Callable[[], None]):
|
|
|
|
thread = threading.Thread(target=target)
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def run(self):
|
2019-06-17 10:35:45 +00:00
|
|
|
self.running = True
|
2019-06-13 16:11:46 +00:00
|
|
|
self._read_thread = self._daemon_thread(
|
2019-06-14 23:39:52 +00:00
|
|
|
lambda: self._loop_catch("read", self._read_loop))
|
2019-06-13 16:11:46 +00:00
|
|
|
self._write_thread = self._daemon_thread(
|
2019-06-14 23:39:52 +00:00
|
|
|
lambda: self._loop_catch("write", self._write_loop))
|
2019-06-06 16:05:44 +00:00
|
|
|
self._event_loop()
|
|
|
|
|
2019-06-09 13:59:28 +00:00
|
|
|
def _kill(self):
|
|
|
|
self.running = False
|
|
|
|
self._trigger_both()
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _event_loop(self):
|
2019-06-09 13:59:28 +00:00
|
|
|
while self.running or not self._event_queue.empty():
|
|
|
|
if not self.servers:
|
|
|
|
self._kill()
|
2019-06-17 13:07:06 +00:00
|
|
|
self.log.warn("No servers, exiting")
|
2019-06-17 15:19:20 +00:00
|
|
|
break
|
2019-06-09 13:59:28 +00:00
|
|
|
|
|
|
|
kill = False
|
2019-06-06 16:05:44 +00:00
|
|
|
item = self._event_queue.get(block=True, timeout=None)
|
2019-06-09 13:59:28 +00:00
|
|
|
|
|
|
|
if item.type == TriggerEventType.Action:
|
|
|
|
try:
|
|
|
|
item.callback()
|
|
|
|
except:
|
|
|
|
self._kill()
|
|
|
|
raise
|
|
|
|
elif item.type == TriggerEventType.Kill:
|
|
|
|
self._kill()
|
2019-06-17 10:06:03 +00:00
|
|
|
if not item.callback == None:
|
|
|
|
item.callback()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
2019-06-06 20:03:25 +00:00
|
|
|
def _post_send_factory(self, server, lines):
|
|
|
|
return lambda: server._post_send(lines)
|
|
|
|
def _post_read_factory(self, server, lines):
|
|
|
|
return lambda: server._post_read(lines)
|
|
|
|
|
2019-06-14 23:39:52 +00:00
|
|
|
def _loop_catch(self, name: str, loop: typing.Callable[[], None]):
|
2019-06-13 16:11:46 +00:00
|
|
|
try:
|
|
|
|
loop()
|
2019-06-17 10:11:35 +00:00
|
|
|
except BitBotPanic:
|
|
|
|
return
|
2019-06-17 10:06:03 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.panic("Exception on '%s' thread" % name)
|
2019-06-13 16:11:46 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
def _write_loop(self):
|
|
|
|
while self.running:
|
2019-06-07 09:39:19 +00:00
|
|
|
with self._write_condition:
|
|
|
|
writeable = False
|
|
|
|
for fd, server in self.servers.items():
|
|
|
|
if server.socket.waiting_immediate_send():
|
2019-06-07 14:07:31 +00:00
|
|
|
self._write_poll.register(fd, select.POLLOUT)
|
2019-06-07 09:39:19 +00:00
|
|
|
writeable = True
|
|
|
|
|
|
|
|
if not writeable:
|
|
|
|
self._write_condition.wait()
|
|
|
|
continue
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
events = self._write_poll.poll()
|
2019-06-07 09:39:19 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
for fd, event in events:
|
2019-06-07 14:07:31 +00:00
|
|
|
if event & select.POLLOUT:
|
2019-06-06 16:05:44 +00:00
|
|
|
self._write_poll.unregister(fd)
|
2019-06-06 21:36:56 +00:00
|
|
|
if fd in self.servers:
|
|
|
|
server = self.servers[fd]
|
|
|
|
|
|
|
|
try:
|
|
|
|
lines = server._send()
|
|
|
|
except:
|
|
|
|
self.log.error("Failed to write to %s",
|
|
|
|
[str(server)])
|
|
|
|
raise
|
2019-06-09 13:59:28 +00:00
|
|
|
event_item = TriggerEvent(TriggerEventType.Action,
|
|
|
|
self._post_send_factory(server, lines))
|
|
|
|
self._event_queue.put(event_item)
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
def _read_loop(self):
|
2016-03-29 11:56:58 +00:00
|
|
|
while self.running:
|
2019-06-06 16:05:44 +00:00
|
|
|
events = self._read_poll.poll(self.get_poll_timeout())
|
2018-09-28 15:51:36 +00:00
|
|
|
|
2019-06-07 09:39:31 +00:00
|
|
|
self.trigger(self._check, False)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
for fd, event in events:
|
2019-06-06 16:05:44 +00:00
|
|
|
if fd == self._rtrigger_server.fileno():
|
|
|
|
# throw away data from trigger socket
|
2019-06-06 18:21:18 +00:00
|
|
|
with self._rtrigger_lock:
|
2019-06-09 13:59:28 +00:00
|
|
|
self._rtrigger_server.recv(1024)
|
2019-06-06 18:21:18 +00:00
|
|
|
self._rtriggered = False
|
2019-06-06 16:05:44 +00:00
|
|
|
else:
|
2019-06-06 21:36:56 +00:00
|
|
|
if not fd in self.servers:
|
|
|
|
self._read_poll.unregister(fd)
|
|
|
|
continue
|
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
server = self.servers[fd]
|
2019-06-07 14:07:31 +00:00
|
|
|
if event & select.POLLIN:
|
2019-06-06 16:05:44 +00:00
|
|
|
lines = server.read()
|
|
|
|
if lines == None:
|
|
|
|
server.disconnect()
|
2018-10-08 22:03:49 +00:00
|
|
|
continue
|
|
|
|
|
2019-06-07 10:05:55 +00:00
|
|
|
self.trigger(self._post_read_factory(server, lines),
|
|
|
|
False)
|
2019-06-06 19:26:05 +00:00
|
|
|
elif event & select.POLLHUP:
|
2019-06-07 14:07:31 +00:00
|
|
|
self.log.warn("Recieved POLLHUP for %s", [str(server)])
|
2019-06-06 16:05:44 +00:00
|
|
|
server.disconnect()
|
|
|
|
|
2019-06-06 19:22:56 +00:00
|
|
|
def _check(self):
|
|
|
|
self._timers.call()
|
|
|
|
self.cache.expire()
|
2019-06-06 16:05:44 +00:00
|
|
|
|
|
|
|
throttle_filled = False
|
|
|
|
for server in list(self.servers.values()):
|
|
|
|
if server.read_timed_out():
|
|
|
|
self.log.warn("Pinged out from %s", [str(server)])
|
|
|
|
server.disconnect()
|
|
|
|
elif server.ping_due() and not server.ping_sent:
|
|
|
|
server.send_ping()
|
|
|
|
server.ping_sent = True
|
2019-06-06 22:19:57 +00:00
|
|
|
|
2019-06-06 16:05:44 +00:00
|
|
|
if not server.socket.connected:
|
|
|
|
self._events.on("server.disconnect").call(server=server)
|
|
|
|
self.disconnect(server)
|
|
|
|
|
|
|
|
if not self.get_server_by_id(server.id):
|
|
|
|
reconnect_delay = self.config.get("reconnect-delay", 10)
|
|
|
|
self._timers.add("reconnect", reconnect_delay,
|
|
|
|
server_id=server.id)
|
|
|
|
self.log.warn(
|
|
|
|
"Disconnected from %s, reconnecting in %d seconds",
|
|
|
|
[str(server), reconnect_delay])
|
|
|
|
elif (server.socket.waiting_throttled_send() and
|
|
|
|
server.socket.throttle_done()):
|
|
|
|
server.socket._fill_throttle()
|
|
|
|
throttle_filled = True
|
|
|
|
|
|
|
|
if throttle_filled:
|
2019-06-06 22:19:57 +00:00
|
|
|
self.trigger_write()
|