def set_servers(self, servers): """ Iter to a list of servers and instantiate Protocol class. :param servers: A list of servers :type servers: list :return: Returns nothing :rtype: None """ if isinstance(servers, six.string_types): servers = [servers] assert servers, "No memcached servers supplied" self._servers = [ Protocol( server=server, username=self.username, password=self.password, compression=self.compression, socket_timeout=self.socket_timeout, pickle_protocol=self.pickle_protocol, pickler=self.pickler, unpickler=self.unpickler, ) for server in servers ]
def run(self): listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listen_sock.setblocking(False) listen_sock.bind((os.environ['MEMCACHED_HOST'], self._listen_port or 0)) listen_sock.listen(1) # Tell our caller the (host, port) that we're listening on. self.pipe.send(listen_sock.getsockname()) # Open a connection to the real memcache server. if not self.server.startswith('/'): host, port = Protocol.split_host_port(self.server) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.connect((host, port)) else: server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server_sock.connect(self.server) # The connection to this server above is blocking, but reads and writes below are nonblocking. server_sock.setblocking(False) # listen_sock is the socket we're listening for connections on. We only handle # a single connection at a time. # client_sock is the connection we've accepted from listen_sock. # server_sock is the connection to the actual server. client_sock = None # Data waiting to be sent to client_sock: data_for_client = b'' # Data waiting to be sent to server_sock: data_for_server = b'' while True: read_sockets = [listen_sock] write_sockets = [] if client_sock: # Only add client_sock to read_sockets if we don't already have data # from it waiting to be sent to the real server. if not data_for_server: read_sockets.append(client_sock) # Only add client_sock to write_sockets if we have data to send. if data_for_client: write_sockets.append(client_sock) if not data_for_client: read_sockets.append(server_sock) if data_for_server: write_sockets.append(server_sock) r, w, _ = select.select(read_sockets, write_sockets, []) if listen_sock in r: if client_sock: client_sock.close() client_sock, client_addr = listen_sock.accept() client_sock.setblocking(False) if server_sock in r: data_for_client += server_sock.recv(1024) if client_sock in r: data_for_server += client_sock.recv(1024) if server_sock in w: bytes_written = server_sock.send(data_for_server) data_for_server = data_for_server[bytes_written:] if client_sock in w: bytes_written = client_sock.send(data_for_client) data_for_client = data_for_client[bytes_written:]
def run(self): listen_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listen_sock.setblocking(False) listen_sock.bind(('127.0.0.1', self._listen_port or 0)) listen_sock.listen(1) # Tell our caller the (host, port) that we're listening on. self.pipe.send(listen_sock.getsockname()) # Open a connection to the real memcache server. if not self.server.startswith('/'): host, port = Protocol.split_host_port(self.server) server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.connect((host, port)) else: server_sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) server_sock.connect(self.server) # The connection to this server above is blocking, but reads and writes below are nonblocking. server_sock.setblocking(False) # listen_sock is the socket we're listening for connections on. We only handle # a single connection at a time. # client_sock is the connection we've accepted from listen_sock. # server_sock is the connection to the actual server. client_sock = None # Data waiting to be sent to client_sock: data_for_client = b'' # Data waiting to be sent to server_sock: data_for_server = b'' while True: read_sockets = [listen_sock] write_sockets = [] sockets = [listen_sock] if client_sock: # Only add client_sock to read_sockets if we don't already have data # from it waiting to be sent to the real server. if not data_for_server: read_sockets.append(client_sock) # Only add client_sock to write_sockets if we have data to send. if data_for_client: write_sockets.append(client_sock) if not data_for_client: read_sockets.append(server_sock) if data_for_server: write_sockets.append(server_sock) r, w, _ = select.select(read_sockets, write_sockets, []) if listen_sock in r: if client_sock: client_sock.close() client_sock, client_addr = listen_sock.accept() client_sock.setblocking(False) if server_sock in r: data_for_client += server_sock.recv(1024) if client_sock in r: data_for_server += client_sock.recv(1024) if server_sock in w: bytes_written = server_sock.send(data_for_server) data_for_server = data_for_server[bytes_written:] if client_sock in w: bytes_written = client_sock.send(data_for_client) data_for_client = data_for_client[bytes_written:]
def servers(self): for server in self._servers: yield Protocol(server, self.username, self.password, self.compression)