mirror of
https://github.com/lnbits/lnbits.git
synced 2025-05-30 17:49:55 +02:00
Fix token bug
Bug: code incorrectly set tokens = 0, then immediately consumed a token (setting it to -1) Fix: Update the last_refill timestamp after waiting (previously missing) Correctly credit exactly 1 token after waiting the appropriate time Then consume the token, leaving the balance at 0 (not negative)
This commit is contained in:
parent
3db1133e68
commit
e3edcbe2fe
@ -1,3 +1,4 @@
|
||||
|
||||
import asyncio
|
||||
import random
|
||||
import time
|
||||
@ -53,16 +54,21 @@ class TokenBucket:
|
||||
self.tokens = min(self.rate, self.tokens + new_tokens)
|
||||
self.last_refill = now
|
||||
|
||||
# If no tokens available, calculate wait time
|
||||
# If no tokens available, calculate wait time and wait for a token
|
||||
if self.tokens < 1:
|
||||
# Calculate time needed for one token
|
||||
wait_time = (self.period / self.rate) * (1 - self.tokens)
|
||||
await asyncio.sleep(wait_time)
|
||||
self.tokens = 0 # Reset after waiting
|
||||
|
||||
# Consume a token
|
||||
# After waiting, update time and add one token
|
||||
self.last_refill = time.monotonic()
|
||||
self.tokens = 1 # We now have exactly one token available
|
||||
|
||||
# Consume a token (will be 0 or more after consumption)
|
||||
self.tokens -= 1
|
||||
|
||||
|
||||
|
||||
class StrikeWallet(Wallet):
|
||||
"""
|
||||
https://developer.strike.me/api
|
||||
|
Loading…
x
Reference in New Issue
Block a user