Example #1
0
        public async Task <HttpResponseMessage> Plugin(
            string token,
            List <HttpEventCollectorEventInfo> events,
            HttpEventCollectorSender.HttpEventCollectorHandler next
            )
        {
            HttpResponseMessage response     = null;
            HttpStatusCode      statusCode   = HttpStatusCode.OK;
            Exception           webException = null;
            string serverReply = null;
            int    retryDelay  = 1000;

            for (int retriesCount = 0; retriesCount <= retriesOnError; retriesCount++)
            {
                try
                {
                    response = await next(token, events);

                    statusCode = response.StatusCode;
                    if (statusCode == HttpStatusCode.OK)
                    {
                        webException = null;
                        break;
                    }
                    if (Array.IndexOf(HttpEventCollectorApplicationErrors, statusCode) >= 0)
                    {
                        if (response.Content != null)
                        {
                            serverReply = await response.Content.ReadAsStringAsync();
                        }
                        break;
                    }
                }
                catch (Exception e)
                {
                    webException = e;
                }

                await Task.Delay(retryDelay);

                retryDelay = Math.Min(RetryDelayCeiling, retryDelay * 2);
            }

            if (statusCode != HttpStatusCode.OK || webException != null)
            {
                throw new HttpEventCollectorException(
                          code: statusCode,
                          webException: webException,
                          reply: serverReply,
                          response: response
                          );
            }

            return(response);
        }
Example #2
0
        /// <summary>
        /// Callback that should be used as middleware in HttpEventCollectorSender
        /// </summary>
        /// <param name="request"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> Plugin(
            string token,
            byte[] serializedEvents,
            HttpEventCollectorSender.HttpEventCollectorHandler next)
        {
            HttpResponseMessage response     = null;
            HttpStatusCode      statusCode   = HttpStatusCode.OK;
            Exception           webException = null;
            string serverReply = null;
            int    retryDelay  = 1000; // start with 1 second

            // retry sending data until success
            for (int retriesCount = 0; retriesCount <= retriesOnError; retriesCount++)
            {
                try
                {
                    response = await next(token, serializedEvents);

                    statusCode = response.StatusCode;
                    if (statusCode == HttpStatusCode.OK)
                    {
                        // the data has been sent successfully
                        webException = null;
                        break;
                    }
                    else if (Array.IndexOf(HttpEventCollectorApplicationErrors, statusCode) >= 0)
                    {
                        // HTTP event collector application error detected - resend wouldn't help
                        // in this case. Record server reply and break.
                        if (response.Content != null)
                        {
                            serverReply = await response.Content.ReadAsStringAsync();
                        }
                        break;
                    }
                    else
                    {
                        // retry
                    }
                }
                catch (Exception e)
                {
                    // connectivity problem - record exception and retry
                    webException = e;
                }
                // wait before next retry
                await Task.Delay(retryDelay);

                // increase delay exponentially
                retryDelay = Math.Min(RetryDelayCeiling, retryDelay * 2);
            }
            if (statusCode != HttpStatusCode.OK || webException != null)
            {
                throw new HttpEventCollectorException(
                          code: statusCode == HttpStatusCode.OK ? HttpStatusCode.InternalServerError : statusCode,
                          webException: webException,
                          reply: serverReply,
                          response: response,
                          serializedEvents: System.Text.Encoding.UTF8.GetString(serializedEvents)
                          );
            }
            return(response);
        }