Exemplo n.º 1
0
        public void WhenApiKeysDontMatchEmptyResponseReturned()
        {
            var proxy = new ServerResponseProxy();

            proxy.SuccessResponseReturned(Some.ApiKey(), "this is never used");
            var response = proxy.GetResponseText(Some.ApiKey());

            Assert.Equal("{}", response);
        }
Exemplo n.º 2
0
        public void NullApiKeysAreConsideredMatching()
        {
            var proxy        = new ServerResponseProxy();
            var responseText = "some response";

            proxy.SuccessResponseReturned(null, responseText);
            var response = proxy.GetResponseText(null);

            Assert.Equal(responseText, response);
        }
Exemplo n.º 3
0
        public void WhenApiKeysMatchTheResponseIsReturned()
        {
            var proxy        = new ServerResponseProxy();
            var apiKey       = Some.ApiKey();
            var responseText = "some response";

            proxy.SuccessResponseReturned(apiKey, responseText);
            var response = proxy.GetResponseText(apiKey);

            Assert.Equal(responseText, response);
        }
Exemplo n.º 4
0
        async Task OnTickAsync()
        {
            try
            {
                var sendingSingles = 0;
                do
                {
                    var available = _logBuffer.Peek((int)_outputConfig.RawPayloadLimitBytes);
                    if (available.Length == 0)
                    {
                        if (DateTime.UtcNow < _nextRequiredLevelCheck || _connectionSchedule.LastConnectionFailed)
                        {
                            // For whatever reason, there's nothing waiting to send. This means we should try connecting again at the
                            // regular interval, so mark the attempt as successful.
                            _connectionSchedule.MarkSuccess();
                            break;
                        }
                    }

                    MakePayload(available, sendingSingles > 0, out Stream payload, out ulong lastIncluded);

                    var content = new StreamContent(new UnclosableStreamWrapper(payload));
                    content.Headers.ContentType = new MediaTypeHeaderValue("application/json")
                    {
                        CharSet = Encoding.UTF8.WebName
                    };

                    if (_apiKey != null)
                    {
                        content.Headers.Add(SeqApi.ApiKeyHeaderName, _apiKey);
                    }

                    var result = await _httpClient.PostAsync(BulkUploadResource, content);

                    if (result.IsSuccessStatusCode)
                    {
                        _connectionSchedule.MarkSuccess();
                        _logBuffer.Dequeue(lastIncluded);
                        if (sendingSingles > 0)
                        {
                            sendingSingles--;
                        }

                        _serverResponseProxy.SuccessResponseReturned(_apiKey, await result.Content.ReadAsStringAsync());
                        _nextRequiredLevelCheck = DateTime.UtcNow.Add(MaximumConnectionInterval);
                    }
                    else if (result.StatusCode == HttpStatusCode.BadRequest ||
                             result.StatusCode == HttpStatusCode.RequestEntityTooLarge)
                    {
                        // The connection attempt was successful - the payload we sent was the problem.
                        _connectionSchedule.MarkSuccess();

                        if (sendingSingles != 0)
                        {
                            payload.Position = 0;
                            var payloadText = new StreamReader(payload, Encoding.UTF8).ReadToEnd();
                            Log.Error("HTTP shipping failed with {StatusCode}: {Result}; payload was {InvalidPayload}", result.StatusCode, await result.Content.ReadAsStringAsync(), payloadText);
                            _logBuffer.Dequeue(lastIncluded);
                            sendingSingles = 0;
                        }
                        else
                        {
                            // Unscientific (should "binary search" in batches) but sending the next
                            // hundred events singly should flush out the problematic one.
                            sendingSingles = 100;
                        }
                    }
                    else
                    {
                        _connectionSchedule.MarkFailure();
                        Log.Error("Received failed HTTP shipping result {StatusCode}: {Result}", result.StatusCode, await result.Content.ReadAsStringAsync());
                        break;
                    }
                }while (true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Exception while sending a batch from the log shipper");
                _connectionSchedule.MarkFailure();
            }
            finally
            {
                lock (_stateLock)
                {
                    if (!_unloading)
                    {
                        SetTimer();
                    }
                }
            }
        }