A :code:`Client` class manages a set of remote server to connect and do a round-robin sample between those GraphLearn clients.
| 22 | |
| 23 | |
| 24 | class Client(object): |
| 25 | """ A :code:`Client` class manages a set of remote server to connect |
| 26 | and do a round-robin sample between those GraphLearn clients. |
| 27 | """ |
| 28 | |
| 29 | def __init__(self, |
| 30 | client_id = 0, |
| 31 | in_memory = True, |
| 32 | **kwargs): # pylint: disable=unused-argument |
| 33 | """ Create GraphLearn server instance with given cluster env info. |
| 34 | Args: |
| 35 | client_id: An int, client index, starts from 0. |
| 36 | in_memory: An bool, is in memory client or rpc client. |
| 37 | """ |
| 38 | self._client_id = client_id |
| 39 | self._in_memory = in_memory |
| 40 | self._client_cache = [] |
| 41 | self._client_own = kwargs.get("client_own", True) |
| 42 | if in_memory: |
| 43 | self._own_servers = None |
| 44 | self._client_cache.append(pywrap.in_memory_client()) |
| 45 | self._current_index = 0 |
| 46 | else: |
| 47 | server_id = kwargs.get("server_id", -1) |
| 48 | # auto select |
| 49 | client = pywrap.rpc_client(server_id, self._client_own) |
| 50 | self._own_servers = client.get_own_servers() |
| 51 | self._client_cache = [None] * len(self._own_servers) |
| 52 | self._client_cache[0] = pywrap.rpc_client(self._own_servers[0], self._client_own) |
| 53 | self._current_index = 0 |
| 54 | |
| 55 | def __len__(self): |
| 56 | """ The capacity of the clients inside the client wrapper. |
| 57 | """ |
| 58 | return len(self._client_cache) |
| 59 | |
| 60 | def is_in_memory(self): |
| 61 | return self._in_memory |
| 62 | |
| 63 | @property |
| 64 | def current_client(self): |
| 65 | return self._client_cache[self._current_index] |
| 66 | |
| 67 | def connect_to_next_server(self): |
| 68 | if self._in_memory: |
| 69 | return False |
| 70 | |
| 71 | if len(self._client_cache) == 1: # the client has only one own server |
| 72 | return False |
| 73 | |
| 74 | # move to the next server |
| 75 | next_index = (self._current_index + 1) % len(self._own_servers) |
| 76 | if next_index < self._current_index: |
| 77 | # one epoch finish, start next epoch |
| 78 | self._current_index = next_index |
| 79 | return False |
| 80 | else: |
| 81 | # one server finish, start to connect next server |
no outgoing calls
no test coverage detected