Skip to content

Commit

Permalink
Reduce DefaultSubmissionClient allocations (#254)
Browse files Browse the repository at this point in the history
* Reduce DefaultSubmissionClient allocations

* Use invariant culture for .ToString()

Co-authored-by: Eric J. Smith <[email protected]>
  • Loading branch information
benaadams and ejsmith authored Apr 18, 2021
1 parent f8b29af commit 118b006
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
45 changes: 27 additions & 18 deletions src/Exceptionless/Submission/DefaultSubmissionClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -25,10 +26,10 @@ public DefaultSubmissionClient(ExceptionlessConfiguration config) {

public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessConfiguration config, IJsonSerializer serializer) {
if (!config.IsValid)
return new SubmissionResponse(500, message: "Invalid client configuration settings");
return SubmissionResponse.InvalidClientConfig500;

string data = serializer.Serialize(events);
string url = String.Format("{0}/events", GetServiceEndPoint(config));
string url = $"{GetServiceEndPoint(config)}/events";

HttpResponseMessage response;
try {
Expand All @@ -44,19 +45,23 @@ public SubmissionResponse PostEvents(IEnumerable<Event> events, ExceptionlessCon
return new SubmissionResponse(500, exception: ex);
}

int settingsVersion;
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out settingsVersion))
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
SettingsManager.CheckVersion(settingsVersion, config);

return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
var message = GetResponseMessage(response);
if ((int)response.StatusCode == 200 && "OK".Equals(message, StringComparison.OrdinalIgnoreCase)) {
return SubmissionResponse.Ok200;
}

return new SubmissionResponse((int)response.StatusCode, message);
}

public SubmissionResponse PostUserDescription(string referenceId, UserDescription description, ExceptionlessConfiguration config, IJsonSerializer serializer) {
if (!config.IsValid)
return new SubmissionResponse(500, message: "Invalid client configuration settings.");
return SubmissionResponse.InvalidClientConfig500;

string data = serializer.Serialize(description);
string url = String.Format("{0}/events/by-ref/{1}/user-description", GetServiceEndPoint(config), referenceId);
string url = $"{GetServiceEndPoint(config)}/events/by-ref/{referenceId}/user-description";

HttpResponseMessage response;
try {
Expand All @@ -72,18 +77,22 @@ public SubmissionResponse PostUserDescription(string referenceId, UserDescriptio
return new SubmissionResponse(500, exception: ex);
}

int settingsVersion;
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out settingsVersion))
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
SettingsManager.CheckVersion(settingsVersion, config);

return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
var message = GetResponseMessage(response);
if ((int)response.StatusCode == 200 && "OK".Equals(message, StringComparison.OrdinalIgnoreCase)) {
return SubmissionResponse.Ok200;
}

return new SubmissionResponse((int)response.StatusCode, message);
}

public SettingsResponse GetSettings(ExceptionlessConfiguration config, int version, IJsonSerializer serializer) {
if (!config.IsValid)
return new SettingsResponse(false, message: "Invalid client configuration settings.");
return SettingsResponse.InvalidClientConfig;

string url = String.Format("{0}/projects/config?v={1}", GetConfigServiceEndPoint(config), version);
string url = $"{GetConfigServiceEndPoint(config)}/projects/config?v={version.ToString(CultureInfo.InvariantCulture)}";

HttpResponseMessage response;
try {
Expand All @@ -95,14 +104,14 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
}

if (response != null && response.StatusCode == HttpStatusCode.NotModified)
return new SettingsResponse(false, message: "Settings have not been modified.");
return SettingsResponse.NotModified;

if (response == null || response.StatusCode != HttpStatusCode.OK)
return new SettingsResponse(false, message: String.Concat("Unable to retrieve configuration settings: ", GetResponseMessage(response)));

var json = GetResponseText(response);
if (String.IsNullOrWhiteSpace(json))
return new SettingsResponse(false, message: "Invalid configuration settings.");
return SettingsResponse.InvalidConfig;

var settings = serializer.Deserialize<ClientConfiguration>(json);
return new SettingsResponse(true, settings.Settings, settings.Version);
Expand All @@ -112,7 +121,7 @@ public void SendHeartbeat(string sessionIdOrUserId, bool closeSession, Exception
if (!config.IsValid)
return;

string url = String.Format("{0}/events/session/heartbeat?id={1}&close={2}", GetHeartbeatServiceEndPoint(config), sessionIdOrUserId, closeSession);
string url = $"{GetHeartbeatServiceEndPoint(config)}/events/session/heartbeat?id={sessionIdOrUserId}&close={closeSession.ToString(CultureInfo.InvariantCulture)}";
try {
_client.Value.AddAuthorizationHeader(config.ApiKey);
_client.Value.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
Expand Down Expand Up @@ -157,12 +166,12 @@ protected virtual HttpClient CreateHttpClient(ExceptionlessConfiguration config)
}

#if NET45
private bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
private static bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
var certData = new CertificateData(sender, certificate, chain, sslPolicyErrors);
return callback(certData);
}
#else
private bool Validate(HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
private static bool Validate(HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
var certData = new CertificateData(httpRequestMessage, certificate, chain, sslPolicyErrors);
return callback(certData);
}
Expand All @@ -188,7 +197,7 @@ private string GetResponseMessage(HttpResponseMessage response) {
} catch { }
}

return !String.IsNullOrEmpty(message) ? message : $"{statusCode} {response.ReasonPhrase}";
return !String.IsNullOrEmpty(message) ? message : $"{statusCode.ToString(CultureInfo.InvariantCulture)} {response.ReasonPhrase}";
}

private string GetResponseText(HttpResponseMessage response) {
Expand Down
4 changes: 4 additions & 0 deletions src/Exceptionless/Submission/SettingsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

namespace Exceptionless.Submission {
public class SettingsResponse {
internal static SettingsResponse NotModified { get; } = new(success: false, message: "Settings have not been modified.");
internal static SettingsResponse InvalidConfig { get; } = new(success: false, message: "Invalid configuration settings.");
internal static SettingsResponse InvalidClientConfig { get; } = new(success: false, message: "Invalid client configuration settings.");

public SettingsResponse(bool success, SettingsDictionary settings = null, int settingsVersion = -1, Exception exception = null, string message = null) {
Success = success;
Settings = settings;
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptionless/Submission/SubmissionResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Exceptionless.Submission {
public class SubmissionResponse {
internal static readonly SubmissionResponse Ok200 = new(200, "OK");
internal static SubmissionResponse Ok200 { get; } = new(200, "OK");
internal static SubmissionResponse InvalidClientConfig500 { get; } = new(500, "Invalid client configuration settings");

public SubmissionResponse(int statusCode, string message = null, Exception exception = null) {
StatusCode = statusCode;
Expand Down

0 comments on commit 118b006

Please sign in to comment.