limiter.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:ssland 作者: laobubu 项目源码 文件源码
def check_user(u):
    '''
    Check if one user shall be suspended. Returning the reason why suspend, or None.
    @type u: user.User
    '''
    if u.suspended: return None

    msg = None

    for r in u.get_meta("limit", []):
        if not 'type'   in r : continue
        if not 'amount' in r : continue
        if not 'since'  in r : continue 
        type, amount, since = r['type'], r['amount'], r['since']

        now   = datetime.datetime.now()

        since = datetime.datetime(now.year, now.month, 1)                       if since == 'this-month'  else \
                datetime.datetime(now.year, now.month, now.day - now.weekday()) if since == 'this-week'   else \
                datetime.datetime.strptime(since, DATE_FORMAT)

        if   type == 'time':     # Time-to-expire rule. amount: useless
            if now >= since:
                msg = "Expired: %s" % r['since']
                break
        elif type == 'traffic':  # Traffic-limited rule. amount: traffic in bytes.
            tq = traffic.query(uid=u.id, min_time=since.strftime(DATE_FORMAT), sum=traffic.QS_ALL)
            if tq[0][2] > amount:
                msg = "Traffic: used %s, limit %s" % (utils.sizeof_fmt(tq[0][2]), utils.sizeof_fmt(amount))
                break

    return msg
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号