|
| 1 | +using Bugsnag.Payload; |
| 2 | +using System; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Text; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace Bugsnag |
| 14 | +{ |
| 15 | + public class DefaultDelivery : IDelivery |
| 16 | + { |
| 17 | + private static DefaultDelivery instance = null; |
| 18 | + private static readonly object instanceLock = new object(); |
| 19 | + |
| 20 | + private HttpClient _httpClient; |
| 21 | + private readonly CancellationTokenSource _cancellationTokenSource; |
| 22 | + private readonly Task _processingTask; |
| 23 | + private readonly BlockingCollection<IPayload> _queue; |
| 24 | + |
| 25 | + private DefaultDelivery() |
| 26 | + { |
| 27 | + _httpClient = new HttpClient(); |
| 28 | + _queue = new BlockingCollection<IPayload>(new ConcurrentQueue<IPayload>()); |
| 29 | + _cancellationTokenSource = new CancellationTokenSource(); |
| 30 | + _processingTask = Task.Run(() => ProcessQueueAsync(_cancellationTokenSource.Token)); |
| 31 | + } |
| 32 | + |
| 33 | + public static DefaultDelivery Instance |
| 34 | + { |
| 35 | + get |
| 36 | + { |
| 37 | + lock (instanceLock) |
| 38 | + { |
| 39 | + if (instance == null) |
| 40 | + { |
| 41 | + instance = new DefaultDelivery(); |
| 42 | + } |
| 43 | + |
| 44 | + return instance; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public void Configure(IConfiguration configuration) |
| 50 | + { |
| 51 | + if (configuration.Proxy != null) |
| 52 | + { |
| 53 | + _httpClient.Dispose(); |
| 54 | + _httpClient = new HttpClient(new HttpClientHandler { Proxy = configuration.Proxy }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public void Send(IPayload payload) |
| 59 | + { |
| 60 | + _queue.Add(payload); |
| 61 | + } |
| 62 | + |
| 63 | + internal void Stop() |
| 64 | + { |
| 65 | + Task.WaitAll(new[] { _processingTask }, TimeSpan.FromSeconds(5)); |
| 66 | + _cancellationTokenSource.Cancel(); |
| 67 | + _httpClient.Dispose(); |
| 68 | + _cancellationTokenSource.Dispose(); |
| 69 | + _queue.Dispose(); |
| 70 | + } |
| 71 | + |
| 72 | + private async Task ProcessQueueAsync(CancellationToken cancellationToken) |
| 73 | + { |
| 74 | + while (!cancellationToken.IsCancellationRequested) |
| 75 | + { |
| 76 | + IPayload payload = null; |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + // Take will block until an item is available or cancellation is requested |
| 81 | + payload = _queue.Take(cancellationToken); |
| 82 | + } |
| 83 | + catch (OperationCanceledException) |
| 84 | + { |
| 85 | + // Exit gracefully when cancellation is requested |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + if (payload != null) |
| 90 | + { |
| 91 | + await SendPayloadAsync(payload); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private async Task SendPayloadAsync(IPayload payload) |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + byte[] serializedPayload = payload.Serialize(); |
| 101 | + if (serializedPayload == null) |
| 102 | + { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + using (var request = new HttpRequestMessage(HttpMethod.Post, payload.Endpoint)) |
| 107 | + { |
| 108 | + request.Content = new ByteArrayContent(serializedPayload); |
| 109 | + request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); |
| 110 | + |
| 111 | + // Add headers from payload |
| 112 | + if (payload.Headers != null) |
| 113 | + { |
| 114 | + foreach (var header in payload.Headers) |
| 115 | + { |
| 116 | + request.Headers.TryAddWithoutValidation(header.Key, header.Value); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Add the Bugsnag-Sent-At header |
| 121 | + request.Headers.Add("Bugsnag-Sent-At", DateTime.UtcNow.ToString("o", System.Globalization.CultureInfo.InvariantCulture)); |
| 122 | + |
| 123 | + // Send the request and log any failures |
| 124 | + var response = await _httpClient.SendAsync(request); |
| 125 | + |
| 126 | + if (!response.IsSuccessStatusCode) |
| 127 | + { |
| 128 | + Trace.WriteLine($"Failed to send payload to Bugsnag - received status code {response.StatusCode}"); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + catch (System.Exception ex) |
| 133 | + { |
| 134 | + Trace.WriteLine($"Error sending payload to Bugsnag: {ex}"); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments