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

Fix stonk deadlock by adding missing select_for_update #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions foo/stonks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ def index_view(request):

@transaction.atomic
def refresh_view(request):
for stonk in Stonk.objects.all():
for stonk in Stonk.objects.select_for_update():
fluctuate_stonk(stonk)
return HttpResponse('Stonks fluctuated')


@transaction.atomic
def top_view(request):
for stonk in Stonk.objects.filter(value__gt=25000):
bump_stonk(stonk)
stonks = Stonk.objects.exclude(value__exact=25000).select_for_update()

for stonk in Stonk.objects.filter(value__lt=25000):
hump_stonk(stonk)
for stonk in stonks:
if stonk.value > 25000:
bump_stonk(stonk)
else:
hump_stonk(stonk)

result = ''
for stonk in Stonk.objects.order_by('-score')[:10]:
Expand Down