public async Task ConfigServerTest()
    {
        var moq = new Mock <IApolloOptions>();

        moq.SetupGet(o => o.ConfigServer).Returns(new[] { "http://106.54.227.205:8080/" });

        var options = moq.Object;

        var locator = new ConfigServiceLocator(new HttpUtil(options), options);

        var services = await locator.GetConfigServices().ConfigureAwait(false);

        Assert.Equal(1, services.Count);
        Assert.Equal(options.ConfigServer.FirstOrDefault(), services[0].HomepageUrl);
    }
Ejemplo n.º 2
0
        public async Task MetaServerTest()
        {
            var moq = new Mock <IApolloOptions>();

            moq.SetupGet(o => o.AppId).Returns("apollo-client");
            moq.SetupGet(o => o.MetaServer).Returns("http://106.54.227.205:8080/");
            moq.SetupGet(o => o.ConfigServer).Returns(new string[0]);

            var options = moq.Object;

            var locator = new ConfigServiceLocator(new HttpUtil(options), options);

            var services = await locator.GetConfigServices().ConfigureAwait(false);

            Assert.Equal(1, services.Count);
            Assert.Equal(options.MetaServer, services[0].HomepageUrl);
        }
    private async Task DoLongPollingRefresh(string appId, string cluster, string?dataCenter, CancellationToken cancellationToken)
    {
        var        random         = new Random();
        ServiceDto?lastServiceDto = null;

        while (!cancellationToken.IsCancellationRequested)
        {
            var sleepTime = 50; //default 50 ms
            Uri?url       = null;
            try
            {
                if (lastServiceDto == null)
                {
                    var configServices = await _serviceLocator.GetConfigServices().ConfigureAwait(false);

                    lastServiceDto = configServices[random.Next(configServices.Count)];
                }

                url = AssembleLongPollRefreshUrl(lastServiceDto.HomepageUrl, appId, cluster, dataCenter);

                Logger().Debug($"Long polling from {url}");
#if NET40
                var response = await _httpUtil.DoGetAsync <ICollection <ApolloConfigNotification> >(url, 600000).ConfigureAwait(false);
#else
                var response = await _httpUtil.DoGetAsync <IReadOnlyCollection <ApolloConfigNotification> >(url, 600000).ConfigureAwait(false);
#endif
                Logger().Debug($"Long polling response: {response.StatusCode}, url: {url}");
                if (response.StatusCode == HttpStatusCode.OK && response.Body != null)
                {
                    UpdateNotifications(response.Body);
                    UpdateRemoteNotifications(response.Body);
                    Notify(lastServiceDto, response.Body);
                    _longPollSuccessSchedulePolicyInMs.Success();
                }
                else
                {
                    sleepTime = _longPollSuccessSchedulePolicyInMs.Fail();
                }

                //try to load balance
                if (response.StatusCode == HttpStatusCode.NotModified && random.NextDouble() >= 0.5)
                {
                    lastServiceDto = null;
                }

                _longPollFailSchedulePolicyInSecond.Success();
            }
            catch (Exception ex)
            {
                lastServiceDto = null;

                var sleepTimeInSecond = _longPollFailSchedulePolicyInSecond.Fail();
                Logger().Warn($"Long polling failed, will retry in {sleepTimeInSecond} seconds. appId: {appId}, cluster: {cluster}, namespace: {string.Join(ConfigConsts.ClusterNamespaceSeparator, _longPollNamespaces.Keys)}, long polling url: {url}, reason: {ex.GetDetailMessage()}");

                sleepTime = sleepTimeInSecond * 1000;
            }
            finally
            {
#if NET40
                await TaskEx.Delay(sleepTime, cancellationToken).ConfigureAwait(false);
#else
                await Task.Delay(sleepTime, cancellationToken).ConfigureAwait(false);
#endif
            }
        }
    }