Skip to content

Commit

Permalink
Make garbage collection filter more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Jan 28, 2018
1 parent f08eaf4 commit 73e585e
Showing 1 changed file with 11 additions and 4 deletions.
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

0 comments on commit 73e585e

Please sign in to comment.