public async Task <IHttpActionResult> Post(TaskRequest request)
    {
        Logger.InfoFormat("Received task {taskJson}", request.TaskJson);

        TaskBase taskObject = request.TaskJson.FromJson <TaskBase>();

        // check error rate
        string cacheKey = $"error-rate:{taskObject.GetType().Name}";

        if (CurrentHttpContext.Instance().Cache.Get(cacheKey) is not RateLimit cacheItem)
        {
            // allow 1 error per hour
            cacheItem = RateLimit.Create(cacheKey, 1, 1, 3600);
            CurrentHttpContext.Instance().Cache.Insert(
                cacheKey,
                cacheItem,
                null,
                Cache.NoAbsoluteExpiration,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Default,
                (key, item, reason) =>
                Logger.InfoFormat(
                    "cache item '{key}' removed due to '{reason}'",
                    key,
                    reason));
        }

        _ = cacheItem.UpdateAllowance(DateTime.Now);
        if (cacheItem.Allowance < 1)
        {
            Logger.InfoFormat(
                "throttling {cacheKey} due to allowance = {allowance}",
                cacheKey,
                cacheItem.Allowance.ToString("N2"));
            return(Ok(
                       $"throttled due to unhandled exception (allowance = {cacheItem.Allowance:N2})"));
        }

        IDisposable scope = NLog.NestedDiagnosticsLogicalContext.Push(taskObject.BusinessKey);

        try
        {
            Logger.Info("begin");
            IHttpActionResult result = await Transact <IHttpActionResult>(async databases =>
            {
                // check for published task
                PublishedTask?publishedTask =
                    await databases.Snittlistan.PublishedTasks.SingleOrDefaultAsync(
                        x => x.MessageId == request.MessageId);
                if (publishedTask is null)
                {
                    return(BadRequest($"no published task found with message id {request.MessageId}"));
                }

                if (publishedTask.HandledDate.HasValue)
                {
                    return(Ok($"task with message id {publishedTask.MessageId} already handled"));
                }

                bool handled = await HandleMessage(
                    taskObject,
                    request.CorrelationId ?? default,
                    request.MessageId ?? default,
                    databases);
                if (handled)
                {
                    publishedTask.MarkHandled(DateTime.Now);
                }

                return(Ok());
            });

            return(result);
        }
        catch (Exception ex)
        {
            Logger.WarnFormat(
                ex,
                "decreasing allowance for {cacheKey}",
                cacheKey);
            _ = cacheItem.DecreaseAllowance();
            throw;
        }
        finally
        {
            Logger.Info("end");
            scope.Dispose();
        }
    }
Exemple #2
0
 private static string GetHostname()
 {
     return(CurrentHttpContext.Instance().Request.ServerVariables["SERVER_NAME"]);
 }
Exemple #3
0
    public async Task Handle(HandlerContext <TCommand> context)
    {
        (string key, int rate, int perSeconds) = GetRate(context);

        // check cache
        if (CurrentHttpContext.Instance().Cache.Get(key) is not RateLimit cacheItem)
        {
            cacheItem = RateLimit.Create(key, 1, rate, perSeconds);
            Logger.InfoFormat(
                "add {key} to cache: {@cacheItem}",
                key,
                cacheItem);
            CurrentHttpContext.Instance().Cache.Insert(
                key,
                cacheItem,
                null,
                Cache.NoAbsoluteExpiration,
                Cache.NoSlidingExpiration,
                CacheItemPriority.Default,
                (key, item, reason) =>
                Logger.InfoFormat(
                    "cache item '{key}' removed due to '{reason}'",
                    key,
                    reason));
        }

        // check database
        KeyValueProperty?rateLimitProperty =
            await CompositionRoot.Databases.Snittlistan.KeyValueProperties
            .SingleOrDefaultAsync(x => x.Key == key);

        if (rateLimitProperty == null)
        {
            KeyValueProperty keyValueProperty = new(
                context.Tenant.TenantId,
                key,
                RateLimit.Create(key, 1, rate, perSeconds));
            rateLimitProperty =
                CompositionRoot.Databases.Snittlistan.KeyValueProperties.Add(keyValueProperty);
        }
        else
        {
            rateLimitProperty.ModifyValue <RateLimit>(
                r => r.SetRate(rate).SetPerSeconds(perSeconds),
                x => Logger.InfoFormat("before: {@x}", x),
                x => Logger.InfoFormat("after: {@x}", x));
        }

        DateTime now = DateTime.Now;

        rateLimitProperty.ModifyValue <RateLimit>(
            x => x.UpdateAllowance(now),
            x => Logger.InfoFormat("before: {@x}", x),
            x => Logger.InfoFormat("after: {@x}", x));
        _ = cacheItem.UpdateAllowance(now);
        double allowance = rateLimitProperty.GetValue <RateLimit, double>(x => x.Allowance);

        if (allowance < 1 || cacheItem.Allowance < 1)
        {
            string message =
                $"(db) allowance = {allowance:N2}, (cache) allowance = {cacheItem.Allowance:N2}, wait to reach 1";
            throw new HandledException(message);
        }

        TEmail email = await CreateEmail(context);

        EmailState state = email.State;

        _ = CompositionRoot.Databases.Snittlistan.SentEmails.Add(new(
                                                                     email.From,
                                                                     email.To,
                                                                     email.Bcc,
                                                                     email.Subject,
                                                                     state));
        Logger.InfoFormat("sending email {@email}", email);
        await CompositionRoot.EmailService.SendAsync(email);

        rateLimitProperty.ModifyValue <RateLimit>(
            x => x.DecreaseAllowance(),
            x => Logger.InfoFormat("before: {@x}", x),
            x => Logger.InfoFormat("after: {@x}", x));
        _ = cacheItem.DecreaseAllowance();
    }
        /// <summary>
        /// 在请求执行完后 记录请求的数据以及返回数据
        /// </summary>
        /// <param name="actionExecutedContext"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            object beginTime = null;

            if (actionExecutedContext.Request.Properties.TryGetValue(key, out beginTime))
            {
                var menuAction  = actionExecutedContext.ActionContext.ActionDescriptor.GetCustomAttributes <MenuAttribute>().OfType <MenuAttribute>().FirstOrDefault();
                var menuAction1 = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes <MenuAttribute>().OfType <MenuAttribute>().FirstOrDefault();

                HttpRequestBase request = CurrentHttpContext.Instance().Request;
                DateTime        time    = DateTime.FromBinary(Convert.ToInt64(beginTime));
                //var accessChannelInfo = ConfigurationHelper.AccessChannelSetting; // load AccessChannel.xml

                SysOperationLog apiActionLog = new SysOperationLog();

                //提取Identity
                var id = CurrentHttpContext.Instance().User.Identity as ClaimsIdentity;
                if (id != null)
                {
                    int accessChannelId = 0;
                    int.TryParse(id?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Actor)?.Value, out accessChannelId);

                    var appType = id?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.System)?.Value;
                    var token   = id?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Authentication)?.Value;
                    apiActionLog.SourceEquipment = appType;
                    apiActionLog.Token           = token;
                    var data = id?.Claims.FirstOrDefault(x => x.Type == ClaimTypes.UserData)?.Value;
                    if (data != null)
                    {
                        var user = JsonConvert.DeserializeObject <SysUser>(data);
                        if (user != null)
                        {
                            //获取用户token
                            apiActionLog.UserId = user.Id;
                        }
                    }
                }
                else
                {
                    apiActionLog.SourceEquipment = "未知";
                    //获取用户token
                    apiActionLog.UserId = 0;
                }

                //获取action名称
                apiActionLog.MethodAction = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
                //获取Controller 名称
                apiActionLog.FunctionController = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
                //获取action开始执行的时间
                apiActionLog.ExecutionTime = time;
                //获取执行action的耗时
                apiActionLog.ExecutionDuration = (DateTime.Now - time).Milliseconds;
                apiActionLog.Navigator         = request.UserAgent;
                //获取访问的ip
                ExploreHelper eh = new ExploreHelper(request);

                apiActionLog.ClientIpAddress = eh.ClientIP;
                //客户端名称
                apiActionLog.ClientName = eh.ClientMachineName;
                //Url来源
                apiActionLog.UrlReferrer = request.UrlReferrer != null ? request.UrlReferrer.AbsoluteUri : "";
                //浏览器信息
                apiActionLog.BrowserInfo = request.Browser.Browser + " - " + request.Browser.Version + " - " + request.Browser.Type;
                //获取request提交的参数
                apiActionLog.Parameters = GetRequestValues(actionExecutedContext) + " " + GetRequestActionValues(actionExecutedContext);

                //获取response响应的结果
                //apiActionLog.Exception = GetResponseValues(actionExecutedContext);
                // "",JsonConvert.SerializeObject(actionExecutedContext.Response.RequestMessage);
                try
                {
                    apiActionLog.IPNum = (int)StringHelper.IPToInt(eh.ClientIP);
                }
                catch
                {
                    apiActionLog.IPNum = 0;
                }
                apiActionLog.Description = msg;

                apiActionLog.RequestUri = request.Url.AbsoluteUri;
                apiActionLog.Enabled    = 1;
                WriteLogService.WriteLogOperate(apiActionLog);
            }
            return(base.OnActionExecutedAsync(actionExecutedContext, cancellationToken));
        }