private ConnectionStatus Execute(RestRequest restRequest) { //RestResponse result = GetClient().execute(restRequest); // if (!this._resourceLock.WaitOne(this._timeout)) { var m = "Could not start the thrift operation before the timeout of " + this._timeout + "ms completed while waiting for the semaphore"; return(new ConnectionStatus(this._connectionSettings, new TimeoutException(m))); } try { Rest.Client client = null; if (!this._clients.TryDequeue(out client)) { var m = string.Format("Could dequeue a thrift client from internal socket pool of size {0}", this._poolSize); var status = new ConnectionStatus(this._connectionSettings, new Exception(m)); return(status); } try { if (!client.InputProtocol.Transport.IsOpen) { client.InputProtocol.Transport.Open(); } var result = client.execute(restRequest); if (result.Status == Status.OK || result.Status == Status.CREATED || result.Status == Status.ACCEPTED) { return(new ConnectionStatus(this._connectionSettings, DecodeStr(result.Body))); } else { var connectionException = new ConnectionException( msg: Enum.GetName(typeof(Status), result.Status), statusCode: (int)result.Status, response: DecodeStr(result.Body) ); return(new ConnectionStatus(this._connectionSettings, connectionException)); } } catch { throw; } finally { //make sure we make the client available again. this._clients.Enqueue(client); } } catch (Exception e) { return(new ConnectionStatus(this._connectionSettings, e)); } finally { this._resourceLock.Release(); } }
/// <summary> /// /// </summary> public ThriftConnection(IConnectionSettings connectionSettings) { Created = DateTime.Now; var tsocket = new TSocket(connectionSettings.Host, connectionSettings.Port); _transport = new TBufferedTransport(tsocket, 1024); _protocol = new TBinaryProtocol(_transport); _client = new Rest.Client(_protocol); }
public ThriftConnection(IConnectionSettings connectionSettings) { this._timeout = connectionSettings.Timeout; this._poolSize = connectionSettings.MaximumAsyncConnections; this._resourceLock = new Semaphore(_poolSize, _poolSize); for (var i = 0; i <= connectionSettings.MaximumAsyncConnections; i++) { var tsocket = new TSocket(connectionSettings.Host, connectionSettings.Port); var transport = new TBufferedTransport(tsocket, 1024); var protocol = new TBinaryProtocol(transport); var client = new Rest.Client(protocol); _clients.Enqueue(client); } }