Skip to content

Commit 118b006

Browse files
benaadamsejsmith
andauthored
Reduce DefaultSubmissionClient allocations (#254)
* Reduce DefaultSubmissionClient allocations * Use invariant culture for .ToString() Co-authored-by: Eric J. Smith <[email protected]>
1 parent f8b29af commit 118b006

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

src/Exceptionless/Submission/DefaultSubmissionClient.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Net;
56
using System.Net.Http;
@@ -25,10 +26,10 @@ public DefaultSubmissionClient(ExceptionlessConfiguration config) {
2526

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

3031
string data = serializer.Serialize(events);
31-
string url = String.Format("{0}/events", GetServiceEndPoint(config));
32+
string url = $"{GetServiceEndPoint(config)}/events";
3233

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

47-
int settingsVersion;
48-
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out settingsVersion))
48+
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
4949
SettingsManager.CheckVersion(settingsVersion, config);
5050

51-
return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
51+
var message = GetResponseMessage(response);
52+
if ((int)response.StatusCode == 200 && "OK".Equals(message, StringComparison.OrdinalIgnoreCase)) {
53+
return SubmissionResponse.Ok200;
54+
}
55+
56+
return new SubmissionResponse((int)response.StatusCode, message);
5257
}
5358

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

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

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

75-
int settingsVersion;
76-
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out settingsVersion))
80+
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
7781
SettingsManager.CheckVersion(settingsVersion, config);
7882

79-
return new SubmissionResponse((int)response.StatusCode, GetResponseMessage(response));
83+
var message = GetResponseMessage(response);
84+
if ((int)response.StatusCode == 200 && "OK".Equals(message, StringComparison.OrdinalIgnoreCase)) {
85+
return SubmissionResponse.Ok200;
86+
}
87+
88+
return new SubmissionResponse((int)response.StatusCode, message);
8089
}
8190

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

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

8897
HttpResponseMessage response;
8998
try {
@@ -95,14 +104,14 @@ public SettingsResponse GetSettings(ExceptionlessConfiguration config, int versi
95104
}
96105

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

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

103112
var json = GetResponseText(response);
104113
if (String.IsNullOrWhiteSpace(json))
105-
return new SettingsResponse(false, message: "Invalid configuration settings.");
114+
return SettingsResponse.InvalidConfig;
106115

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

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

159168
#if NET45
160-
private bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
169+
private static bool Validate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
161170
var certData = new CertificateData(sender, certificate, chain, sslPolicyErrors);
162171
return callback(certData);
163172
}
164173
#else
165-
private bool Validate(HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
174+
private static bool Validate(HttpRequestMessage httpRequestMessage, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors, Func<CertificateData, bool> callback) {
166175
var certData = new CertificateData(httpRequestMessage, certificate, chain, sslPolicyErrors);
167176
return callback(certData);
168177
}
@@ -188,7 +197,7 @@ private string GetResponseMessage(HttpResponseMessage response) {
188197
} catch { }
189198
}
190199

191-
return !String.IsNullOrEmpty(message) ? message : $"{statusCode} {response.ReasonPhrase}";
200+
return !String.IsNullOrEmpty(message) ? message : $"{statusCode.ToString(CultureInfo.InvariantCulture)} {response.ReasonPhrase}";
192201
}
193202

194203
private string GetResponseText(HttpResponseMessage response) {

src/Exceptionless/Submission/SettingsResponse.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44
namespace Exceptionless.Submission {
55
public class SettingsResponse {
6+
internal static SettingsResponse NotModified { get; } = new(success: false, message: "Settings have not been modified.");
7+
internal static SettingsResponse InvalidConfig { get; } = new(success: false, message: "Invalid configuration settings.");
8+
internal static SettingsResponse InvalidClientConfig { get; } = new(success: false, message: "Invalid client configuration settings.");
9+
610
public SettingsResponse(bool success, SettingsDictionary settings = null, int settingsVersion = -1, Exception exception = null, string message = null) {
711
Success = success;
812
Settings = settings;

src/Exceptionless/Submission/SubmissionResponse.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

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

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

0 commit comments

Comments
 (0)