public void TestException() { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new AsyncService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IAsyncService>(manager, server, new TBinaryProtocol.Factory())) { SimpleStruct structObj = CreateSimpleStructWithData(); try { var task = client.ExceptionMethod(); task.GetAwaiter().GetResult(); Assert.True(false); } catch (ThriftyApplicationException e) { Assert.NotNull(e); Assert.Contains(AsyncService.ExceptionMessage, e.Message); } } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public object Invoke(MethodInfo method, object[] args) { if (this._disposed) { throw new ObjectDisposedException(this.GetType().Name); } if (args == null && string.Equals(nameof(IDisposable.Dispose), method.Name)) { this.Channel.CloseAsync().GetAwaiter().GetResult(); this.Dispose(); return(null); } if (args == null && string.Equals($"get_{nameof(INiftyClientChannelAware.ClientChannel)}", method.Name)) { return(this.Channel as INiftyClientChannel); } try { return(Process(method, args)); } catch (TException e) { throw ThriftClientManager.WrapTException(e); } }
public ThriftyClient(ThriftyClientOptions swiftyClientOptions) { _swiftyClientOptions = swiftyClientOptions ?? new ThriftyClientOptions(); var loggerFactory = _swiftyClientOptions.LoggerFactory; _logger = loggerFactory?.CreateLogger(typeof(ThriftyClient)) ?? NullLogger.Instance; _thriftClientManager = new ThriftClientManager(new ThriftCodecManager(), new NiftyClient(swiftyClientOptions.NettyClientConfig ?? new NettyClientConfig(), _swiftyClientOptions.LoggerFactory), Enumerable.Empty <ThriftClientEventHandler>(), loggerFactory: _swiftyClientOptions.LoggerFactory); _swiftyPing = new ThriftyPing(_thriftClientManager); _timeoutTimer = new Lazy <HashedWheelEvictionTimer>(() => { var timer = Utils.Timer; return(new HashedWheelEvictionTimer(timer)); }, true); _channelPools = swiftyClientOptions.ConnectionPoolEnabled ? new NiftyClientChannelPool(_thriftClientManager, _timeoutTimer.Value, swiftyClientOptions) : (IClientConnectionPool) new NoneClientChannelPool(_thriftClientManager, swiftyClientOptions); if (_swiftyClientOptions.EurekaEnabled) { var eureka = _swiftyClientOptions.Eureka; DiscoveryManager.Instance.Initialize(eureka, loggerFactory); var interval = eureka.RegistryFetchIntervalSeconds * 1000 / 2;//TODO last的事件处理 _timeoutTimer.Value.Schedule(this.RefreshServers, TimeSpan.FromMilliseconds(interval)); } }
public NoneClientChannelPool(ThriftClientManager thriftClientManager, ThriftyClientOptions options) { Guard.ArgumentNotNull(options, nameof(options)); Guard.ArgumentNotNull(thriftClientManager, nameof(thriftClientManager)); _thriftClientManager = thriftClientManager; _options = options; }
public void TestOneWay() { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new AsyncService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IAsyncService>(manager, server, new TBinaryProtocol.Factory())) { client.OneWayMethod().GetAwaiter().GetResult(); client.VerifyConnectionState(); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public void TestOneWayException() { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new OneWayService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IOneWayService>(manager, server, new TBinaryProtocol.Factory())) { //client.OneWayThrow(); client.VerifyConnectionState(); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public void TestServerException() { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new ExceptionService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IExceptionService>(manager, server, new TBinaryProtocol.Factory())) { ThriftyException ex = Assert.Throws <ThriftyApplicationException>(() => client.ThrowArgumentException()); Assert.Contains(ExceptionService.ExceptionMessage, ex.Message); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public T CreateScribeClient <T>( ThriftClientManager manager, ScopedServer server, TProtocolFactory protocolFactory) where T : class { ThriftClientConfig config = new ThriftClientConfig(); config.ConnectTimeout = TimeSpan.FromSeconds(600); config.ReceiveTimeout = TimeSpan.FromSeconds(600); config.ReadTimeout = TimeSpan.FromSeconds(600); config.WriteTimeout = TimeSpan.FromSeconds(600); var thriftClient = new ThriftClient(manager, typeof(T), config, typeof(T).Name); return(thriftClient.OpenAsync( new FramedClientConnector("localhost", server.Port, protocolFactory)) .GetAwaiter().GetResult() as T); }
public void Dispose() { if (_disposed) { _disposed = true; if (_timeoutTimer.IsValueCreated) { _timeoutTimer.Value.Dispose(); } _channelPools?.Dispose(); _thriftClientManager?.Dispose(); _channelPools = null; _thriftClientManager = null; } }
public void TestTwoWay() { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new AsyncService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IAsyncService>(manager, server, new TBinaryProtocol.Factory())) { SimpleStruct structObj = CreateSimpleStructWithData(); var result = client.TwoWayMethod(structObj).GetAwaiter().GetResult(); Assert.Equal(structObj, result); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public NiftyClientChannelFactory(ThriftClientManager thriftClientManager, ThriftyClientOptions options) { Guard.ArgumentNotNull(options, nameof(options)); Guard.ArgumentNotNull(thriftClientManager, nameof(thriftClientManager)); _createChannel = key => { var connector = new FramedClientConnector(key.IpEndPoint.Address.ToString(), key.IpEndPoint.Port, _options.LoggerFactory); var channel = thriftClientManager.CreateChannelAsync(connector, TimeSpan.FromMilliseconds(key.ConnectionTimeout), TimeSpan.FromMilliseconds(key.ReceiveTimeout), TimeSpan.FromMilliseconds(key.ReadTimeout), TimeSpan.FromMilliseconds(key.WriteTimeout), _options.MaxFrameSize, key.SslConfig, _options.SocketProxy); return(channel.GetAwaiter().GetResult()); }; _options = options; }
public void ByteTest(String content) { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new MultipleParameterService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IMultipleParameterService>(manager, server, new TBinaryProtocol.Factory())) { var test = Encoding.UTF8.GetBytes(content); var bytesString = client.BytesToString(test); Assert.Equal(content, bytesString); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
public void TestCompactProtocolClient() { using (ScopedServer server = new ScopedServer(new TCompactProtocol.Factory(), new ScribeTest())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IScribe>(manager, server, new TCompactProtocol.Factory())) { var code = client.log(new List <LogEntry> { new LogEntry("testCategory1", "testMessage1"), new LogEntry("testCategory2", "testMessage2") }); Assert.Equal(ResultCode.TRY_LATER, code); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }
static void Main(string[] args) { //using (TTransport transport = new TSocket("127.0.0.1", 9001)) //using (TProtocol protocol = new TBinaryProtocol(transport)) //using (GameService.Client client = new GameService.Client(protocol)) //{ // transport.Open(); // var info = client.GetGameInfo(1001); // Console.WriteLine($"{info.GameId},{info.GameName}"); //} //记录信息与错误日志 Thrift.Client.ThriftLog._eventInfo = (x) => { Console.WriteLine("info:" + x); }; Thrift.Client.ThriftLog._eventError = (x) => { Console.WriteLine("error:" + x); }; //使用连接池,保持连接 using (var svc = ThriftClientManager <ThriftTestThrift.Client> .GetClient("ThriftTestThrift")) { try { var info = svc.Client.GetTestInfo(); Console.WriteLine("true:" + Newtonsoft.Json.JsonConvert.SerializeObject(info)); } catch (Exception ex) { Console.WriteLine(ex.Message); if (svc != null) { svc.Destroy(); } } } //不保持连接 using (var svc = ThriftClientManager <ThriftTestThrift.Client> .GetClientNoPool("ThriftTestThrift")) { try { var info = svc.Client.GetTestInfo(); Console.WriteLine("true:" + Newtonsoft.Json.JsonConvert.SerializeObject(info)); } catch (Exception ex) { Console.WriteLine(ex.Message); if (svc != null) { svc.Destroy(); } } } Console.Read(); int test_console = int.Parse(ConfigurationManager.AppSettings["test_console"]); int test_count = int.Parse(ConfigurationManager.AppSettings["test_count"]); int test_th = int.Parse(ConfigurationManager.AppSettings["test_th"]); int test_sleep = int.Parse(ConfigurationManager.AppSettings["test_sleep"]); while (_count++ < test_count) { try { using (var svc = ThriftClientManager <ThriftTestThrift.Client> .GetClient("ThriftTestThrift")) { var info = svc.Client.GetTestInfo(); Console.WriteLine("true:" + Newtonsoft.Json.JsonConvert.SerializeObject(info)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } System.Threading.Thread.Sleep(test_sleep); } Console.Read(); ThreadPool.SetMinThreads(100, 100); var stopwatch = new Stopwatch(); stopwatch.Start(); var threads = new List <Thread>(); for (int i = 0; i < test_th; i++) { threads.Add(new Thread(() => { while (_count++ < test_count) { System.Threading.Thread.Sleep(test_sleep); try { using (var svc = ThriftClientManager <ThriftTestThrift.Client> .GetClient("ThriftTestThrift")) { var info = svc.Client.GetTestInfo(); // Console.WriteLine("true:"+DateTime.Now.ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } })); } for (int i = 0; i < test_th; i++) { System.Threading.Thread.Sleep(100); threads[i].Start(); } while (_count < test_count) { ; } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds); Console.WriteLine("over:" + _count); Console.ReadLine(); }
public NiftyClientChannelPool(ThriftClientManager thriftClientManager, IEvictionTimer timer, ThriftyClientOptions options) : base(new NiftyClientChannelFactory(thriftClientManager, options), options.ConnectionPool, timer, options.LoggerFactory) { }
internal ThriftyPing(ThriftClientManager thriftClientManager) { _thriftClientManager = thriftClientManager; }
public void ParametersTest(String args1, int args2, float args3, double args4, bool args5, byte args6, TestEnum args7, long args8, short args9, int?nargs2, float?nargs3, double?nargs4, bool?nargs5, byte?nargs6, TestEnum?nargs7, long?nargs8, short?nargs9) { using (ScopedServer server = new ScopedServer(new TBinaryProtocol.Factory(), new MultipleParameterService())) { using (ThriftClientManager manager = new ThriftClientManager()) { using (var client = server.CreateScribeClient <IMultipleParameterService>(manager, server, new TBinaryProtocol.Factory())) { Random random = new Random(); DateTime date = RandomDateTime(random); DateTime?date2 = RandomDateTime(random); String bytesString = Guid.NewGuid().ToString(); byte[] bytes = Encoding.UTF8.GetBytes(bytesString); string expected = $@"{args1}{args2}{args3}{args4}{args5}{args6}{args7}{args8}{args9}" + $@"{nargs2}{nargs3}{nargs4}{nargs5}{nargs6}{nargs7}{nargs8}{nargs9}{date}{date2}{bytesString}"; string result = client.MergeString( args1, args2, args3, args4, args5, args6, args7, args8, args9, nargs2, nargs3, nargs4, nargs5, nargs6, nargs7, nargs8, nargs9, date, date2, bytes); Assert.Equal(expected, result); } manager.CloseAsync(TimeSpan.FromSeconds(3)); } } }