Skip to content

Commit

Permalink
send gzipped requests (and uncompress them in the test server)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Toshok committed Jun 5, 2017
1 parent 4c2750f commit 0deebff
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Src/LibHoney.Tests/HttpTestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -76,7 +77,8 @@ public void ServeOne (int statusCode, string body, TimeSpan timeout)
Thread.Sleep (timeout);

// Save the payload
var reader = new StreamReader (request.InputStream);
var gzipStream = new GZipStream(request.InputStream, CompressionMode.Decompress);
var reader = new StreamReader (gzipStream);
payloadItems.Add (reader.ReadToEnd ());
headers.Add (request.Headers);

Expand Down
84 changes: 50 additions & 34 deletions Src/LibHoney/Transmission.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -160,42 +162,56 @@ void DoSend (Event ev)
{
var uri = new Uri (ev.ApiHost + HoneyEventsUrl + ev.DataSet);

var req = new HttpRequestMessage () {
RequestUri = uri,
Method = HttpMethod.Post,
Content = new StringContent (ev.ToJSON (), Encoding.UTF8, "application/json")
};
req.Headers.Add (HoneyTeamKey, ev.WriteKey);
req.Headers.Add (HoneySamplerate, ev.SampleRate.ToString ());
req.Headers.Add (HoneyEventTime, ev.CreatedAtISO);

HttpResponseMessage result = null;
DateTime start = DateTime.Now;
string errorMessage = null;

try {
// Get the Result right away to hint the scheduler to run the task inline.
result = client.SendAsync (req).Result;
} catch (AggregateException exc) {
// Ignore network errors, but report them as responses.
if (!IsNetworkError (exc.InnerException, out errorMessage))
throw;
}
using (MemoryStream ms = new MemoryStream()) {
using (GZipStream gzip = new GZipStream(ms,
CompressionMode.Compress, true))
{
var evBytes = Encoding.UTF8.GetBytes(ev.ToJSON());
gzip.Write(evBytes, 0, evBytes.Length);
}

Response res;
if (result != null)
res = new Response () {
StatusCode = result.StatusCode,
Body = result.Content.ReadAsStringAsync ().Result
};
else
res = new Response () {
ErrorMessage = "Error while sending the event: " + errorMessage,
};
ms.Position = 0;

res.Duration = DateTime.Now - start;
res.Metadata = ev.Metadata;
EnqueueResponse (res);
var req = new HttpRequestMessage () {
RequestUri = uri,
Method = HttpMethod.Post,
Content = new StreamContent(ms)
};
req.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
req.Content.Headers.ContentEncoding.Add ("gzip");
req.Headers.Add (HoneyTeamKey, ev.WriteKey);
req.Headers.Add (HoneySamplerate, ev.SampleRate.ToString ());
req.Headers.Add (HoneyEventTime, ev.CreatedAtISO);

HttpResponseMessage result = null;
DateTime start = DateTime.Now;
string errorMessage = null;

try {
// Get the Result right away to hint the scheduler to run the task inline.
result = client.SendAsync (req).Result;
} catch (AggregateException exc) {
// Ignore network errors, but report them as responses.
if (!IsNetworkError (exc.InnerException, out errorMessage))
throw;
}

Response res;
if (result != null) {
res = new Response () {
StatusCode = result.StatusCode,
Body = result.Content.ReadAsStringAsync ().Result
};
} else {
res = new Response () {
ErrorMessage = "Error while sending the event: " + errorMessage,
};
}

res.Duration = DateTime.Now - start;
res.Metadata = ev.Metadata;
EnqueueResponse (res);
}
}

static bool IsNetworkError (Exception exc, out string errorMessage)
Expand Down

0 comments on commit 0deebff

Please sign in to comment.