Esempio n. 1
0
        private IExchangeClient InitClient(URL url)
        {
            // client type setting.
            var str = url.GetParameter(Constants.ClientKey, url.GetParameter(Constants.ServerKey, Constants.DefaultRemotingClient));

            var version    = url.GetParameter(Constants.DubboVersionKey, "2.6.0");
            var compatible = (version != null && version.StartsWith("1.0."));

            url = url.AddParameter(Constants.CodecKey, DubboCodec.Name);
            // enable heartbeat by default
            url = url.AddParameterIfAbsent(Constants.HeartbeatKey, Constants.DefaultHeartbeat.ToString());

            // BIO is not allowed since it has severe performance issue.
            var transporter = ObjectFactory.GetInstance <ITransporter>(str);

            if (transporter == null)
            {
                throw new Exception("Unsupported client type: " + str + "," +
                                    " supported client type is " + ObjectFactory.GetTypeKeys <ITransporter>() + " ");
            }

            IExchangeClient client;

            try
            {
                // connection should be lazy
                if (url.GetParameter(Constants.LazyConnectKey, false))
                {
                    client = new LazyConnectExchangeClient(url, RequestHandler());
                }
                else
                {
                    client = Exchangers.ConnectAsync(url, RequestHandler()).Result;
                }
            }
            catch (RemotingException e)
            {
                throw new Exception("Fail to create remoting client for service(" + url + "): " + e.Message, e);
            }
            return(client);
        }
        private LazyConnectExchangeClient ReplaceWithLazyClient()
        {
            URL lazyUrl = Url.AddParameter(Constants.LazyConnectInitialStateKey, false)
                          .AddParameter(Constants.ReconnectKey, false)
                          .AddParameter(Constants.SendReconnectKey, true)
                          .AddParameter("warning", true)
                          .AddParameter(LazyConnectExchangeClient.RequestWithWarningKey, true)
                          .AddParameter("_client_memo", "referencecounthandler.replacewithlazyclient");

            var key = Url.GetAddress();
            // in worst case there's only one ghost connection.
            LazyConnectExchangeClient gclient = null;

            if (_ghostClientMap.ContainsKey(key))
            {
                _ghostClientMap.TryGetValue(key, out gclient);
            }
            if (gclient == null || gclient.IsClosed)
            {
                gclient = new LazyConnectExchangeClient(lazyUrl, _client.GetExchangeHandler());
                _ghostClientMap.TryAdd(key, gclient);
            }
            return(gclient);
        }