-
Notifications
You must be signed in to change notification settings - Fork 29
Replace default delivery with HttpClient implementation #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f26969
Replace deilvery with HttpClient implementation
yousif-bugsnag 75c5c54
Use proxy from client configuration and remove proxy property from pa…
yousif-bugsnag faa93ea
Add unit tests with real payloads and test headers are set on requests
yousif-bugsnag 609a03d
Fix target frameworks for AspNet.Core tests
yousif-bugsnag 66ea225
restore TreatWarningsAsErrors configuration for non-test projects
yousif-bugsnag 744b37c
Fix formatting
yousif-bugsnag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using Bugsnag.Payload; | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Net.Http.Headers; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Bugsnag | ||
| { | ||
| public class DefaultDelivery : IDelivery | ||
| { | ||
| private static DefaultDelivery instance = null; | ||
| private static readonly object instanceLock = new object(); | ||
|
|
||
| private HttpClient _httpClient; | ||
| private readonly CancellationTokenSource _cancellationTokenSource; | ||
| private readonly Task _processingTask; | ||
| private readonly BlockingCollection<IPayload> _queue; | ||
|
|
||
| private DefaultDelivery() | ||
| { | ||
| _httpClient = new HttpClient(); | ||
| _queue = new BlockingCollection<IPayload>(new ConcurrentQueue<IPayload>()); | ||
| _cancellationTokenSource = new CancellationTokenSource(); | ||
| _processingTask = Task.Run(() => ProcessQueueAsync(_cancellationTokenSource.Token)); | ||
| } | ||
|
|
||
| public static DefaultDelivery Instance | ||
| { | ||
| get | ||
| { | ||
| lock (instanceLock) | ||
| { | ||
| if (instance == null) | ||
| { | ||
| instance = new DefaultDelivery(); | ||
| } | ||
|
|
||
| return instance; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void Configure(IConfiguration configuration) | ||
| { | ||
| if (configuration.Proxy != null) | ||
| { | ||
| _httpClient.Dispose(); | ||
| _httpClient = new HttpClient(new HttpClientHandler { Proxy = configuration.Proxy }); | ||
| } | ||
| } | ||
|
|
||
| public void Send(IPayload payload) | ||
| { | ||
| _queue.Add(payload); | ||
| } | ||
|
|
||
| internal void Stop() | ||
| { | ||
| Task.WaitAll(new[] { _processingTask }, TimeSpan.FromSeconds(5)); | ||
| _cancellationTokenSource.Cancel(); | ||
| _httpClient.Dispose(); | ||
| _cancellationTokenSource.Dispose(); | ||
| _queue.Dispose(); | ||
| } | ||
|
|
||
| private async Task ProcessQueueAsync(CancellationToken cancellationToken) | ||
| { | ||
| while (!cancellationToken.IsCancellationRequested) | ||
| { | ||
| IPayload payload = null; | ||
|
|
||
| try | ||
| { | ||
| // Take will block until an item is available or cancellation is requested | ||
| payload = _queue.Take(cancellationToken); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // Exit gracefully when cancellation is requested | ||
| break; | ||
| } | ||
|
|
||
| if (payload != null) | ||
| { | ||
| await SendPayloadAsync(payload); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private async Task SendPayloadAsync(IPayload payload) | ||
| { | ||
| try | ||
| { | ||
| byte[] serializedPayload = payload.Serialize(); | ||
| if (serializedPayload == null) | ||
| return; | ||
|
|
||
| using (var request = new HttpRequestMessage(HttpMethod.Post, payload.Endpoint)) | ||
| { | ||
| request.Content = new ByteArrayContent(serializedPayload); | ||
| request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); | ||
|
|
||
| // Add headers from payload | ||
| if (payload.Headers != null) | ||
| { | ||
| foreach (var header in payload.Headers) | ||
| { | ||
| request.Headers.TryAddWithoutValidation(header.Key, header.Value); | ||
| } | ||
| } | ||
|
|
||
| // Add the Bugsnag-Sent-At header | ||
| request.Headers.Add("Bugsnag-Sent-At", DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture)); | ||
|
|
||
| // Send the request and log any failures | ||
| var response = await _httpClient.SendAsync(request); | ||
|
|
||
| if (!response.IsSuccessStatusCode) { | ||
| Trace.WriteLine($"Failed to send payload to Bugsnag - received status code {response.StatusCode}"); | ||
| } | ||
yousif-bugsnag marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| catch (System.Exception ex) | ||
| { | ||
| Trace.WriteLine($"Error sending payload to Bugsnag: {ex}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.