コード例 #1
0
        public async Task <AuthenticationResponse> AuthenticateAsync()
        {
            try
            {
                var response = await _splitioHttpClient.GetAsync(_url);

                if (response.statusCode == HttpStatusCode.OK)
                {
                    _log.Debug($"Success connection to: {_url}");

                    return(GetSuccessResponse(response.content));
                }
                else if (response.statusCode >= HttpStatusCode.BadRequest && response.statusCode < HttpStatusCode.InternalServerError)
                {
                    _log.Debug($"Problem to connect to : {_url}. Response status: {response.statusCode}");

                    return(new AuthenticationResponse {
                        PushEnabled = false, Retry = false
                    });
                }

                return(new AuthenticationResponse {
                    PushEnabled = false, Retry = true
                });
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message);

                return(new AuthenticationResponse {
                    PushEnabled = false, Retry = false
                });
            }
        }
コード例 #2
0
        private async Task ConnectAsync()
        {
            try
            {
                UpdateFinishedConnection(finished: false);

                _splitHttpClient = new SplitioHttpClient(new Dictionary <string, string> {
                    { "Accept", "text/event-stream" }
                });
                _cancellationTokenSource = new CancellationTokenSource();

                using (var response = await _splitHttpClient.GetAsync(_url, HttpCompletionOption.ResponseHeadersRead, _cancellationTokenSource.Token))
                {
                    _log.Debug($"Response from {_url}: {response.StatusCode}");

                    if (response.IsSuccessStatusCode)
                    {
                        try
                        {
                            using (var stream = await response.Content.ReadAsStreamAsync())
                            {
                                _log.Info($"Connected to {_url}");

                                UpdateStatus(connected: true);
                                DispatchConnected();
                                await ReadStreamAsync(stream);
                            }
                        }
                        catch (Exception ex)
                        {
                            _log.Error($"Error reading stream: {ex.Message}");
                            Disconnect(reconnect: true);
                            return;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Error connecting to {_url}: {ex.Message}");
            }
            finally
            {
                UpdateFinishedConnection(finished: true);
            }

            _log.Debug("Finished Event Source client ConnectAsync.");
            Disconnect();
        }
コード例 #3
0
        private async Task ConnectAsync(CancellationToken cancellationToken)
        {
            var action = SSEClientActions.DISCONNECT;

            try
            {
                using (var response = await _splitHttpClient.GetAsync(_url, HttpCompletionOption.ResponseHeadersRead, cancellationToken))
                {
                    _log.Debug($"Response from {_url}: {response.StatusCode}");

                    if (response.IsSuccessStatusCode)
                    {
                        try
                        {
                            using (var stream = await response.Content.ReadAsStreamAsync())
                            {
                                _log.Info($"Connected to {_url}");

                                await ReadStreamAsync(stream, cancellationToken);
                            }
                        }
                        catch (ReadStreamException ex)
                        {
                            _log.Debug(ex.Message);
                            action = ex.Action;
                        }
                        catch (Exception ex)
                        {
                            _log.Debug($"Error reading stream: {ex.Message}");
                            action = SSEClientActions.RETRYABLE_ERROR;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Debug($"Error connecting to {_url}.", ex);
            }
            finally
            {
                _disconnectSignal.Signal();
                Disconnect(action);

                _log.Debug("Finished Event Source client ConnectAsync.");
            }
        }
コード例 #4
0
        public async Task <AuthenticationResponse> AuthenticateAsync()
        {
            using (var clock = new Util.SplitStopwatch())
            {
                clock.Start();

                try
                {
                    var response = await _splitioHttpClient.GetAsync(_url);

                    if (response.statusCode == HttpStatusCode.OK)
                    {
                        _log.Debug($"Success connection to: {_url}");

                        _telemetryRuntimeProducer.RecordSyncLatency(ResourceEnum.TokenSync, Util.Metrics.Bucket(clock.ElapsedMilliseconds));
                        _telemetryRuntimeProducer.RecordSuccessfulSync(ResourceEnum.TokenSync, CurrentTimeHelper.CurrentTimeMillis());

                        return(GetSuccessResponse(response.content));
                    }
                    else if (response.statusCode >= HttpStatusCode.BadRequest && response.statusCode < HttpStatusCode.InternalServerError)
                    {
                        _log.Debug($"Problem to connect to : {_url}. Response status: {response.statusCode}");

                        _telemetryRuntimeProducer.RecordAuthRejections();
                        return(new AuthenticationResponse {
                            PushEnabled = false, Retry = false
                        });
                    }

                    _telemetryRuntimeProducer.RecordSyncError(ResourceEnum.TokenSync, (int)response.statusCode);
                    return(new AuthenticationResponse {
                        PushEnabled = false, Retry = true
                    });
                }
                catch (Exception ex)
                {
                    _log.Error(ex.Message);

                    return(new AuthenticationResponse {
                        PushEnabled = false, Retry = false
                    });
                }
            }
        }
コード例 #5
0
        private async Task ConnectAsync()
        {
            try
            {
                _wrapperAdapter.TaskDelay(Convert.ToInt32(_backOff.GetInterval()) * 1000).Wait();

                _splitHttpClient = new SplitioHttpClient(new Dictionary <string, string> {
                    { "Accept", "text/event-stream" }
                });
                _cancellationTokenSource = new CancellationTokenSource();

                using (var response = await _splitHttpClient.GetAsync(_url, HttpCompletionOption.ResponseHeadersRead, _cancellationTokenSource.Token))
                {
                    UpdateFinishedConnection(finished: false);
                    try
                    {
                        using (var stream = await response.Content.ReadAsStreamAsync())
                        {
                            _log.Info($"Connected to {_url}");

                            UpdateStatus(connected: true);
                            _backOff.Reset();
                            DispatchConnected();
                            await ReadStreamAsync(stream);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.Error($"Error reading stream: {ex.Message}");
                        ReconnectAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error($"Error connecting to {_url}: {ex.Message}");
            }

            _log.Debug("Finished Event Source client ConnectAsync.");
            Disconnect();
            UpdateFinishedConnection(finished: true);
        }