/// <summary> /// decides if and when to retry sending the request /// </summary> /// <param name="request">the request object</param> private async void Retry(QueueRequest request) { request.Attempts++; if (request.Attempts < MaxAttempts) { int delay = (int)FixedRetryDelay.TotalMilliseconds; if (RetryStrategy == QueueRetryStrategy.ExponentialBackoff) { delay = (int)Math.Pow(2, request.Attempts) * 1000; } // add delay before the retry await Task.Delay(delay); // re-add the request into the queue requests.Enqueue(request); } }
/// <summary> /// Enqueue a message to be sent /// </summary> /// <param name="uri">Uri to be used, shouldn't start with / and will be prefixed with the base address</param> /// <param name="message">the JSON message to send, if null is passed HTTP GET will be used</param> public void Enqueue(string uri, string message = null) { QueueRequest request = new QueueRequest() { Message = message, RequestUri = new Uri(httpClient.BaseAddress.OriginalString + uri), Method = message == null ? HttpMethod.Get : HttpMethod.Post, Attempts = 0 }; requests.Enqueue(request); // throw away old messages in case the queue is full lock (syncObject) { while (requests.Count > MaxQueueSize) { requests.TryDequeue(out request); } } }