def _connect(self): err = None for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): family, socktype, proto, canonname, socket_address = res sock = None try: sock = socket.socket(family, socktype, proto) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) if self.socket_keepalive: sock.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) for k, v in self.socket_keepalive_options.iteritems(): sock.setsockopt(socket.SOL_TCP, k, v) sock.settimeout(self.socket_connect_timeout) sock.connect(socket_address) sock.settimeout(self.socket_timeout) return sock except socket.error as _: err = _ if sock is not None: sock.close() if err is not None: raise err # pylint: disable=raising-bad-type raise socket.error("socket.getaddrinfo returned empty list")
def check(self): result = False sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: sock.connect(self.addr) self._communicate(sock) result = True finally: if sock: sock.close() return result
def check(self): result = False sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: sock.connect(self.addr) self._communicate(sock) result = True finally: if sock: try: sock.shutdown(socket.SHUT_RDWR) sock.close() except socket.error as err: self.logger.warn('Close error: %s', err) return result
def _check(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: sock.connect(self.addr) self._communicate(sock) self.last_check_success = True except Exception as err: # Avoid spamming the logs if self.last_check_success: self.logger.warn('ERROR performing %s check (%s:%d): %s', self.checker_type, self.host, self.port, err) self.last_check_success = False finally: if sock: try: sock.shutdown(socket.SHUT_RDWR) sock.close() except socket.error as err: pass if not self.last_check_success: self.logger.warn('%s check failed', self.name) return self.last_check_success