Beispiel #1
0
        public Task AddListenerAsync(AddListenerRequest request)
        {
            if (request == null)
            {
                throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "request param invalid");
            }

            if (string.IsNullOrWhiteSpace(request.Tenant))
            {
                request.Tenant = _options.Namespace;
            }
            if (string.IsNullOrWhiteSpace(request.Group))
            {
                request.Group = ConstValue.DefaultGroup;
            }

            request.CheckParam();

            var name = BuildName(request.Tenant, request.Group, request.DataId);

            if (listeners.Any(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
            {
                _logger.LogWarning($"[add-listener] error, {name} has been added.");
                return(Task.CompletedTask);
            }

            Timer timer = new Timer(
                async x =>
            {
                await PollingAsync(x);
#if !DEBUG
            }, request, 0, _options.ListenInterval);
#else
            }, request, 0, 8000);
        private async Task ConfigChangeAsync(string content, AddListenerRequest request)
        {
            // config was changed
            if (!string.IsNullOrWhiteSpace(content))
            {
                var config = await DoGetConfigAsync(new GetConfigRequest
                {
                    DataId = request.DataId,
                    Group  = request.Group,
                    Tenant = request.Tenant
                }).ConfigureAwait(false);

                // update local cache
                await GetProcessor().SaveSnapshotAsync(GetAgent().GetName(), request.DataId, request.Group, request.Tenant, config).ConfigureAwait(false);

                // callback
                foreach (var cb in request.Callbacks)
                {
                    try
                    {
                        cb(config);
                    }
                    catch (Exception ex)
                    {
                        _logger?.LogError(ex, $"[listener] call back throw exception, dataId={request.DataId}, group={request.Group}, tenant={request.Tenant}");
                    }
                }
            }
        }
        public Task AddListenerAsync(AddListenerRequest request)
        {
            if (request == null)
            {
                throw new NacosException(NacosException.CLIENT_INVALID_PARAM, "request param invalid");
            }

            if (string.IsNullOrWhiteSpace(request.Tenant))
            {
                request.Tenant = _options.Namespace;
            }
            if (string.IsNullOrWhiteSpace(request.Group))
            {
                request.Group = ConstValue.DefaultGroup;
            }

            request.CheckParam();

            var name = BuildName(request.Tenant, request.Group, request.DataId);

            if (listeners.Any(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
            {
                _logger?.LogWarning($"[add-listener] error, {name} has been added.");
                return(Task.CompletedTask);
            }

            var cts = new CancellationTokenSource();

            _ = PollingAsync(request, cts.Token);

            listeners.Add(new Listener(name, cts));

            return(Task.CompletedTask);
        }
        private async Task PollingAsync(AddListenerRequest request, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                // read the last config
                var lastConfig = await GetProcessor().GetSnapshotAync(GetAgent().GetName(), request.DataId, request.Group, request.Tenant).ConfigureAwait(false);

                request.Content = lastConfig;

                try
                {
                    var headers = new Dictionary <string, string>()
                    {
                        { "Long-Pulling-Timeout", (ConstValue.LongPullingTimeout * 1000).ToString() }
                    };

                    var responseMessage = await GetAgent().PostAsync(RequestPathValue.CONFIGS_LISTENER, headers, request.ToDict(), cancellationToken).ConfigureAwait(false);

                    switch (responseMessage.StatusCode)
                    {
                    case System.Net.HttpStatusCode.OK:
                        SetHealthServer(true);
                        var content = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
                        await ConfigChangeAsync(content, request).ConfigureAwait(false);

                        break;

                    case System.Net.HttpStatusCode.Forbidden:
                        SetHealthServer(false);
                        _logger?.LogWarning($"[listener] error, dataId={request.DataId}, group={request.Group}, tenant={request.Tenant}, code={(int)responseMessage.StatusCode} msg={responseMessage.StatusCode.ToString()}");
                        throw new NacosException(NacosException.NO_RIGHT, $"Insufficient privilege.");

                    default:
                        SetHealthServer(false);
                        _logger?.LogWarning($"[listener] error, dataId={request.DataId}, group={request.Group}, tenant={request.Tenant}, code={(int)responseMessage.StatusCode} msg={responseMessage.StatusCode.ToString()}");
                        throw new NacosException((int)responseMessage.StatusCode, responseMessage.StatusCode.ToString());
                    }
                }
                catch (Exception ex)
                {
                    SetHealthServer(false);
                    _logger?.LogError(ex, $"[listener] error, dataId={request.DataId}, group={request.Group}, tenant={request.Tenant}");
                }
            }
        }