Example #1
0
def throttle_concurrent(remote_addr):
    throttled = False
    try:
        if xmlrpc_redis:
            dogstatsd.increment('xmlrpc.rate-limit.invoke')
            statsd_reporter.incr('rpc-rl.invoke')
            pipeline = xmlrpc_redis.pipeline()
            pipeline.incr(remote_addr)
            pipeline.expire(remote_addr, 60)
            current = pipeline.execute()[0]
            if current >= conf.xmlrpc_concurrent_requests:
                dogstatsd.increment('xmlrpc.rate-limit.over')
                statsd_reporter.incr('rpc-rl.over')
                log_xmlrpc_throttle(remote_addr, conf.xmlrpc_enforce)
                if conf.xmlrpc_enforce:
                    dogstatsd.increment('xmlrpc.rate-limit.enforce')
                    statsd_reporter.incr('rpc-rl.enforce')
                    throttled = True
    except Exception:
        dogstatsd.increment('xmlrpc.rate-limit.context.before.error')
        statsd_reporter.incr('rpc-rl.context.before.error')
        pass
    yield throttled
    try:
        if xmlrpc_redis:
            xmlrpc_redis.decr(remote_addr)
    except Exception:
        dogstatsd.increment('xmlrpc.rate-limit.context.after.error')
        statsd_reporter.incr('rpc-rl.context.after.error')
        pass
Example #2
0
def log_xmlrpc_response(remote_addr, user_agent, data, response_size):
    if conf.xmlrpc_request_log_file:
        try:
            with open(conf.xmlrpc_request_log_file, 'a') as f:
                params, method = xmlrpclib.loads(data)
                dogstatsd.increment('xmlrpc.response',
                                    tags=['method:{}'.format(method)])
                dogstatsd.histogram('xmlrpc.response.size',
                                    response_size,
                                    tags=['method:{}'.format(method)])
                record = json.dumps({
                    'timestamp':
                    datetime.datetime.utcnow().isoformat(),
                    'remote_addr':
                    remote_addr,
                    'user_agent':
                    user_agent,
                    'method':
                    method,
                    'params':
                    params,
                    'response_size':
                    response_size,
                    'type':
                    'response',
                })
                f.write(record + '\n')
        except Exception:
            pass
Example #3
0
def throttle_concurrent(remote_addr):
    throttled = False
    try:
        if xmlrpc_redis:
            dogstatsd.increment('xmlrpc.rate-limit.invoke')
            statsd_reporter.incr('rpc-rl.invoke')
            pipeline = xmlrpc_redis.pipeline()
            pipeline.incr(remote_addr)
            pipeline.expire(remote_addr, 60)
            current = pipeline.execute()[0]
            if current >= conf.xmlrpc_concurrent_requests:
                dogstatsd.increment('xmlrpc.rate-limit.over')
                statsd_reporter.incr('rpc-rl.over')
                log_xmlrpc_throttle(remote_addr, conf.xmlrpc_enforce)
                if conf.xmlrpc_enforce:
                    dogstatsd.increment('xmlrpc.rate-limit.enforce')
                    statsd_reporter.incr('rpc-rl.enforce')
                    throttled = True
    except Exception:
        dogstatsd.increment('xmlrpc.rate-limit.context.before.error')
        statsd_reporter.incr('rpc-rl.context.before.error')
        pass
    yield throttled
    try:
        if xmlrpc_redis:
            xmlrpc_redis.decr(remote_addr)
    except Exception:
        dogstatsd.increment('xmlrpc.rate-limit.context.after.error')
        statsd_reporter.incr('rpc-rl.context.after.error')
        pass
Example #4
0
def log_xmlrpc_throttle(remote_addr, enforced):
    if conf.xmlrpc_request_log_file:
        try:
            with open(conf.xmlrpc_request_log_file, 'a') as f:
                dogstatsd.increment('xmlrpc.throttled', tags=['remote_addr:{}'.format(remote_addr), 'enforced:{}'.format(enforced)])
                record = json.dumps({
                    'timestamp': datetime.datetime.utcnow().isoformat(),
                    'remote_addr': remote_addr,
                    'method': 'throttled',
                    'throttle_enforced': enforced,
                })
                f.write(record + '\n')
        except Exception:
            pass
Example #5
0
def log_xmlrpc_request(remote_addr, user_agent, data):
    if conf.xmlrpc_request_log_file:
        try:
            with open(conf.xmlrpc_request_log_file, 'a') as f:
                params, method = xmlrpclib.loads(data)
                dogstatsd.increment('xmlrpc.request', tags=['method:{}'.format(method)])
                record = json.dumps({
                    'timestamp': datetime.datetime.utcnow().isoformat(),
                    'remote_addr': remote_addr,
                    'user_agent': user_agent,
                    'method': method,
                    'params': params,
                    'type': 'request',
                })
                f.write(record + '\n')
        except Exception:
            pass
Example #6
0
def log_xmlrpc_throttle(remote_addr, enforced):
    if conf.xmlrpc_request_log_file:
        try:
            with open(conf.xmlrpc_request_log_file, 'a') as f:
                dogstatsd.increment('xmlrpc.throttled',
                                    tags=[
                                        'remote_addr:{}'.format(remote_addr),
                                        'enforced:{}'.format(enforced)
                                    ])
                record = json.dumps({
                    'timestamp':
                    datetime.datetime.utcnow().isoformat(),
                    'remote_addr':
                    remote_addr,
                    'method':
                    'throttled',
                    'throttle_enforced':
                    enforced,
                })
                f.write(record + '\n')
        except Exception:
            pass