Throttle abusive logging
This commit is contained in:
parent
01c2fad534
commit
50f02747de
@ -215,6 +215,7 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
|
||||
refund = int(elapsed / self.bandwidth_interval * self.bandwidth_limit)
|
||||
refund = min(refund, self.bandwidth_used)
|
||||
self.bandwidth_used += amount - refund
|
||||
self.throttled = max(0, self.throttled - int(elapsed) // 60)
|
||||
|
||||
def data_received(self, data):
|
||||
'''Handle incoming data (synchronously).
|
||||
|
||||
11
lib/util.py
11
lib/util.py
@ -22,8 +22,17 @@ class LoggedClass(object):
|
||||
self.logger = logging.getLogger(self.__class__.__name__)
|
||||
self.logger.setLevel(logging.INFO)
|
||||
self.log_prefix = ''
|
||||
self.throttled = 0
|
||||
|
||||
def log_info(self, msg):
|
||||
def log_info(self, msg, throttle=False):
|
||||
# Prevent annoying log messages by throttling them if there
|
||||
# are too many in a short period
|
||||
if throttle:
|
||||
self.throttled += 1
|
||||
if self.throttled > 3:
|
||||
return
|
||||
if self.throttled == 3:
|
||||
msg += ' (throttling later logs)'
|
||||
self.logger.info(self.log_prefix + msg)
|
||||
|
||||
def log_warning(self, msg):
|
||||
|
||||
@ -339,7 +339,7 @@ class ServerManager(util.LoggedClass):
|
||||
group = self.groups[int(session.start - self.start) // 900]
|
||||
group.add(session)
|
||||
self.sessions[session] = group
|
||||
session.log_info('{} from {}, {:,d} total'
|
||||
session.log_info('{} {}, {:,d} total'
|
||||
.format(session.kind, session.peername(),
|
||||
len(self.sessions)))
|
||||
if (len(self.sessions) >= self.max_sessions
|
||||
@ -920,7 +920,8 @@ class ElectrumX(Session):
|
||||
except DaemonError as e:
|
||||
error = e.args[0]
|
||||
message = error['message']
|
||||
self.log_info('sendrawtransaction: {}'.format(message))
|
||||
self.log_info('sendrawtransaction: {}'.format(message),
|
||||
throttle=True)
|
||||
if 'non-mandatory-script-verify-flag' in message:
|
||||
return (
|
||||
'Your client produced a transaction that is not accepted '
|
||||
|
||||
Loading…
Reference in New Issue
Block a user