From 96ee3d853be68ac7b2852aa8fdf4e0753125f5a7 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Thu, 14 Dec 2017 23:45:12 +0100 Subject: [PATCH] limit eventstore size --- mitmproxy/addons/eventstore.py | 11 +++++++++++ mitmproxy/tools/console/eventlog.py | 4 ---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/mitmproxy/addons/eventstore.py b/mitmproxy/addons/eventstore.py index ff8429d7e..415bd0177 100644 --- a/mitmproxy/addons/eventstore.py +++ b/mitmproxy/addons/eventstore.py @@ -5,6 +5,8 @@ import blinker from mitmproxy import command from mitmproxy.log import LogEntry +EVENTLOG_SIZE = 10000 + class EventStore: def __init__(self): @@ -15,6 +17,15 @@ class EventStore: def log(self, entry: LogEntry) -> None: self.data.append(entry) self.sig_add.send(self, entry=entry) + # Instead of removing one log row for every row > EVENTLOG_SIZE we add, + # we accept an overhead of 10% and only purge then to simplify the API. + if len(self.data) / EVENTLOG_SIZE >= 1.1: + self.purge() + + def purge(self): + """Purge event store size to EVENTLOG_SIZE""" + self.data = self.data[len(self.data) - EVENTLOG_SIZE:] + self.sig_refresh.send(self) @command.command("eventstore.clear") def clear(self) -> None: diff --git a/mitmproxy/tools/console/eventlog.py b/mitmproxy/tools/console/eventlog.py index 3fc34fa25..49e10dbcc 100644 --- a/mitmproxy/tools/console/eventlog.py +++ b/mitmproxy/tools/console/eventlog.py @@ -2,8 +2,6 @@ import urwid from mitmproxy.tools.console import layoutwidget from mitmproxy import log -EVENTLOG_SIZE = 10000 - class LogBufferWalker(urwid.SimpleListWalker): pass @@ -47,8 +45,6 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget): else: e = urwid.Text(txt) self.walker.append(e) - if len(self.walker) > EVENTLOG_SIZE: - self.walker.pop(0) if self.master.options.console_focus_follow: self.walker.set_focus(len(self.walker) - 1)