سؤال

نحن نقوم بتجربة طرق مختلفة لتقييد إجراءات المستخدم في فترة زمنية معينة:

  • الحد من مشاركات الأسئلة والأجوبة
  • الحد من التعديلات
  • الحد من عمليات استرجاع الخلاصات

في الوقت الحالي، نستخدم ذاكرة التخزين المؤقت لإدراج سجل لنشاط المستخدم ببساطة - إذا كان هذا السجل موجودًا، وإذا/عندما يقوم المستخدم بنفس النشاط، فإننا نقوم بالاختناق.

يتيح لنا استخدام ذاكرة التخزين المؤقت تلقائيًا تنظيف البيانات القديمة وتحريك نوافذ نشاط المستخدمين، ولكن كيفية توسيع نطاقها قد يمثل مشكلة.

ما هي بعض الطرق الأخرى لضمان إمكانية التحكم في الطلبات/إجراءات المستخدم بشكل فعال (التأكيد على الاستقرار)؟

هل كانت مفيدة؟

المحلول

إليك نسخة عامة مما كنا نستخدمه في Stack Overflow خلال العام الماضي:

/// <summary>
/// Decorates any MVC route that needs to have client requests limited by time.
/// </summary>
/// <remarks>
/// Uses the current System.Web.Caching.Cache to store each client request to the decorated route.
/// </remarks>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class ThrottleAttribute : ActionFilterAttribute
{
    /// <summary>
    /// A unique name for this Throttle.
    /// </summary>
    /// <remarks>
    /// We'll be inserting a Cache record based on this name and client IP, e.g. "Name-192.168.0.1"
    /// </remarks>
    public string Name { get; set; }

    /// <summary>
    /// The number of seconds clients must wait before executing this decorated route again.
    /// </summary>
    public int Seconds { get; set; }

    /// <summary>
    /// A text message that will be sent to the client upon throttling.  You can include the token {n} to
    /// show this.Seconds in the message, e.g. "Wait {n} seconds before trying again".
    /// </summary>
    public string Message { get; set; }

    public override void OnActionExecuting(ActionExecutingContext c)
    {
        var key = string.Concat(Name, "-", c.HttpContext.Request.UserHostAddress);
        var allowExecute = false;

        if (HttpRuntime.Cache[key] == null)
        {
            HttpRuntime.Cache.Add(key,
                true, // is this the smallest data we can have?
                null, // no dependencies
                DateTime.Now.AddSeconds(Seconds), // absolute expiration
                Cache.NoSlidingExpiration,
                CacheItemPriority.Low,
                null); // no callback

            allowExecute = true;
        }

        if (!allowExecute)
        {
            if (String.IsNullOrEmpty(Message))
                Message = "You may only perform this action every {n} seconds.";

            c.Result = new ContentResult { Content = Message.Replace("{n}", Seconds.ToString()) };
            // see 409 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
            c.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict;
        }
    }
}

استخدام العينة:

[Throttle(Name="TestThrottle", Message = "You must wait {n} seconds before accessing this url again.", Seconds = 5)]
public ActionResult TestThrottle()
{
    return Content("TestThrottle executed");
}

تعمل ذاكرة التخزين المؤقت ASP.NET مثل البطل هنا - فباستخدامها، يمكنك الحصول على تنظيف تلقائي لإدخالات دواسة الوقود الخاصة بك.ومع تزايد حركة المرور لدينا، لا نرى أن هذه مشكلة على الخادم.

لا تتردد في تقديم ملاحظات حول هذه الطريقة؛عندما نجعل Stack Overflow أفضل، ستحصل على إصلاح ايوك حتى أسرع :)

نصائح أخرى

لدى Microsoft ملحق جديد لـ IIS 7 يسمى ملحق قيود IP الديناميكية لـ IIS 7.0 - Beta.

"إن قيود IP الديناميكية لـ IIS 7.0 هي وحدة توفر الحماية ضد رفض الخدمة وهجمات القوة الغاشمة على خادم الويب ومواقع الويب.يتم توفير هذه الحماية عن طريق الحظر المؤقت لعناوين IP لعملاء HTTP الذين يقدمون عددًا كبيرًا بشكل غير عادي من الطلبات المتزامنة أو الذين يقدمون عددًا كبيرًا من الطلبات خلال فترة زمنية صغيرة." http://learn.iis.net/page.aspx/548/using-dynamic-ip-restrictions/

مثال:

إذا قمت بتعيين معايير الحظر بعد ذلك X requests in Y milliseconds أو X concurrent connections in Y milliseconds سيتم حظر عنوان IP Y milliseconds ثم سيتم السماح بالطلبات مرة أخرى.

نحن نستخدم التقنية المستعارة من عنوان URL هذا http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx, ، ليس من أجل الاختناق، ولكن من أجل رفض الخدمة لرجل فقير (D.O.S).يعتمد هذا أيضًا على ذاكرة التخزين المؤقت، وقد يكون مشابهًا لما تفعله.هل أنت في حالة اختناق لمنع D.O.S.الهجمات؟يمكن بالتأكيد استخدام أجهزة التوجيه لتقليل D.O.S؛هل تعتقد أن جهاز التوجيه يمكنه التعامل مع الاختناق الذي تحتاجه؟

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top