Skip to content

Instantly share code, notes, and snippets.

@hkdsun
Last active August 12, 2016 23:05
Show Gist options
  • Save hkdsun/9a9e043eb901e92b3a10a91d1576d558 to your computer and use it in GitHub Desktop.
Save hkdsun/9a9e043eb901e92b3a10a91d1576d558 to your computer and use it in GitHub Desktop.
private ConcurrentHashMap<String, TNonblockingTransport> open_connections = new ConcurrentHashMap<String, TNonblockingTransport>();
private ConcurrentHashMap<String, KeyValueService.AsyncClient> open_clients = new ConcurrentHashMap<String, KeyValueService.AsyncClient>();
private TNonblockingTransport getConnection(Integer server) throws TException, IOException {
String threadKey = server.toString() + Thread.currentThread().getId();
if(open_connections.containsKey(threadKey))
return open_connections.get(threadKey);
String host = StorageNode.hosts.get(server);
Integer port = StorageNode.ports.get(server);
TNonblockingTransport transport = new TNonblockingSocket(host, port);
open_connections.put(threadKey, transport);
return transport;
}
private KeyValueService.AsyncClient getClient(Integer server, TNonblockingTransport transport) throws IOException {
String threadKey = server.toString() + Thread.currentThread().getId();
if(open_clients.containsKey(threadKey))
return open_clients.get(threadKey);
TAsyncClientManager clientManager = new TAsyncClientManager();
TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
KeyValueService.AsyncClient client = new KeyValueService.AsyncClient(protocolFactory, clientManager, transport);
open_clients.put(threadKey, client);
return client;
}
// somewhere else
try {
// use the client
} catch { // client is dead
// remove client from open_clients
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment