Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make garbage collection filter more efficient #267

Merged
merged 1 commit into from
Jan 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions silk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,21 @@ def garbage_collect(cls, force=False):
if check_percent < random.random() and not force:
return
target_count = SilkyConfig().SILKY_MAX_RECORDED_REQUESTS

# Since garbage collection is probabilistic, the target count should
# be lowered to account for requests before the next garbage collection
if check_percent != 0:
target_count -= int(1 / check_percent)
prune_count = max(cls.objects.count() - target_count, 0)
prune_rows = cls.objects.order_by('start_time') \
.values_list('id', flat=True)[:prune_count]
cls.objects.filter(id__in=list(prune_rows)).delete()

# Make sure we can delete everything if needed by settings
if target_count <= 0:
cls.objects.all().delete()
return
requests = cls.objects.order_by('-start_time')
if not requests:
return
time_cutoff = requests[target_count].start_time
cls.objects.filter(start_time__lte=time_cutoff).delete()

def save(self, *args, **kwargs):
# sometimes django requests return the body as 'None'
Expand Down