Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Pr#38 fix offline message queuing #46

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Analytics.Xamarin.Pcl/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Client(string writeKey, Config config)
this._writeKey = writeKey;
this._config = config;

IRequestHandler requestHandler = new BlockingRequestHandler(config.Host, config.Timeout);
IRequestHandler requestHandler = new BlockingRequestHandler(this, config.Timeout);
IBatchFactory batchFactory = new SimpleBatchFactory(this._writeKey);

requestHandler.Succeeded += (action) =>
Expand Down
14 changes: 14 additions & 0 deletions Analytics.Xamarin.Pcl/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Config
/// </summary>
internal string Host { get; set; }

internal string Proxy { get; set; }

internal int MaxQueueSize { get; set; }

internal bool Async { get; set; }
Expand All @@ -40,6 +42,18 @@ public Config SetHost(string host)
return this;
}

/// <summary>
/// Set the proxy server Uri
/// </summary>
/// <param name="proxy">Proxy server Uri</param>
/// <returns></returns>
public Config SetProxy(string proxy)
{
this.Proxy = proxy;
return this;
}


/// <summary>
/// Sets the maximum amount of timeout on the HTTP request flushes to the server.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions Analytics.Xamarin.Pcl/Flush/AsyncFlushHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,13 @@ private void Loop()
{ "batch size", current.Count }
});

// make the request here
_requestHandler.SendBatch(batch);

// mark the current batch as null
current = new List<BaseAction>();
// send the batch async
Task<bool> res = Task.Run(() => _requestHandler.SendBatch(batch));
// clear the current only of the batch was sent successfully
if (res.Result)
{
current = new List<BaseAction>();
}
}
}
}
Expand Down
71 changes: 59 additions & 12 deletions Analytics.Xamarin.Pcl/Request/BlockingRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@

namespace Segment.Request
{
class WebProxy : System.Net.IWebProxy
{
private string _proxy;

public WebProxy(string proxy)
{
_proxy = proxy;
GetProxy(new Uri(proxy)); // ** What does this do?
}

public System.Net.ICredentials Credentials
{
get; set;
}

public Uri GetProxy(Uri destination)
{
if (!String.IsNullOrWhiteSpace(destination.ToString()))
return destination;
else
return new Uri("");
}

public bool IsBypassed(Uri host)
{
if (!String.IsNullOrWhiteSpace(host.ToString()))
return true;
else
return false;
}
}
internal class BlockingRequestHandler : IRequestHandler
{
/// <summary>
Expand All @@ -29,39 +60,53 @@ internal class BlockingRequestHandler : IRequestHandler
public event SucceededActionHandler Succeeded;

/// <summary>
/// Segment API endpoint.
/// Segment client
/// </summary>
private string _host;
private readonly Client _client;

/// <summary>
/// Http client
/// </summary>
private HttpClient _client;
private readonly HttpClient _httpClient;

internal BlockingRequestHandler(string host, TimeSpan timeout)


internal BlockingRequestHandler(Client client, TimeSpan timeout)
{
this._host = host;
this._client = new HttpClient();
this._client.Timeout = timeout;
this._client = client;

if (!string.IsNullOrEmpty(_client.Config.Proxy))
{
var handler = new HttpClientHandler
{
Proxy = new WebProxy(_client.Config.Proxy),
UseProxy = true
};
this._httpClient = new HttpClient(handler);
}
else
this._httpClient = new HttpClient();

this._httpClient.Timeout = timeout;
// do not use the expect 100-continue behavior
this._client.DefaultRequestHeaders.ExpectContinue = false;
this._httpClient.DefaultRequestHeaders.ExpectContinue = false;
}

public void SendBatch(Batch batch)
public bool SendBatch(Batch batch)
{
Dict props = new Dict {
{ "batch id", batch.MessageId },
{ "batch size", batch.batch.Count }
};

bool sentBatch = false;
try
{
// set the current request time
batch.SentAt = DateTime.Now.ToString("o");
string json = JsonConvert.SerializeObject(batch);
props["json size"] = json.Length;

Uri uri = new Uri(_host + "/v1/import");
Uri uri = new Uri(_client.Config.Host + "/v1/import");

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);

Expand All @@ -73,7 +118,7 @@ public void SendBatch(Batch batch)

var start = DateTime.Now;

var response = _client.SendAsync(request).Result;
var response = _httpClient.SendAsync(request).Result;

var duration = DateTime.Now - start;
props["success"] = response.IsSuccessStatusCode;
Expand All @@ -83,6 +128,7 @@ public void SendBatch(Batch batch)
{
Succeed(batch);
Logger.Info("Request successful", props);
sentBatch = true;
}
else
{
Expand All @@ -99,6 +145,7 @@ public void SendBatch(Batch batch)
Logger.Error("Request failed", props);
Fail(batch, e);
}
return sentBatch;
}

private void Fail(Batch batch, System.Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Analytics.Xamarin.Pcl/Request/IRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal interface IRequestHandler : IDisposable
/// Send an action batch to the Segment tracking API.
/// </summary>
/// <param name="batch">Batch.</param>
void SendBatch(Batch batch);
bool SendBatch(Batch batch);

/// <summary>
/// Occurs when an action fails.
Expand Down