How to Build a Flow Control Credit Window in Python

A Python class that reserves, confirms, releases, and settles credit to limit message flow and prevent overload in streaming pipelines.

Medium Python 3.9+ Aug 9, 2026 Streaming & messaging 14 views 0 copies

Python code

46 lines
Python 3.9+
class CreditWindow:
    def __init__(self, max_credit=1000):
        self.max_credit = max_credit
        self.used_credit = 0
        self.pending_credit = 0
    
    def try_reserve(self, amount):
        available = self.max_credit - self.used_credit - self.pending_credit
        if available >= amount:
            self.pending_credit += amount
            return True
        return False
    
    def confirm(self, amount):
        self.pending_credit -= amount
        self.used_credit += amount
    
    def release(self, amount):
        self.pending_credit -= amount
    
    def settle(self, amount):
        self.used_credit = max(0, self.used_credit - amount)
    
    def available(self):
        return self.max_credit - self.used_credit - self.pending_credit
    
    def __repr__(self):
        return (f"CreditWindow(max={self.max_credit}, used={self.used_credit}, "
                f"pending={self.pending_credit}, available={self.available()})")


if __name__ == "__main__":
    window = CreditWindow(max_credit=1000)
    print(f"Initial: {window}")
    
    print(f"Reserve 300: {window.try_reserve(300)}")
    window.confirm(300)
    print(f"After confirm: {window}")
    
    print(f"Reserve 700: {window.try_reserve(700)}")
    print(f"Reserve 500: {window.try_reserve(500)}")
    window.release(500)
    print(f"After release: {window}")
    
    window.settle(200)
    print(f"After settle: {window}")

Output

stdout
Initial: CreditWindow(max=1000, used=0, pending=0, available=1000)
Reserve 300: True
After confirm: CreditWindow(max=1000, used=300, pending=0, available=700)
Reserve 700: True
Reserve 500: False
After release: CreditWindow(max=1000, used=300, pending=200, available=500)
After settle: CreditWindow(max=1000, used=100, pending=200, available=700)

How it works

The CreditWindow class implements a credit-based flow control mechanism common in messaging systems. try_reserve checks available credit and places a hold on pending_credit before a message is sent. confirm moves a reserved amount from pending to used after successful delivery, while release cancels an unused reservation. settle reduces used credit over time, allowing the window to reopen under backpressure. The available method computes actual headroom at any instant, preventing overcommit of the credit budget.

Common mistakes

  • Confirming an amount that was never reserved, causing negative pending credit
  • Releasing credit after confirmation, double-freeing the budget
  • Ignoring the pending state when computing available credit, leading to over-allocation
  • Using mutable defaults or shared class variables for per-window state

Variations

  1. Add a `__enter__`/`__exit__` context manager to auto-release credit on exceptions
  2. Implement a time-based `settle` with a background thread for automatic credit replenishment

Real-world use cases

  • Rate-limiting downstream consumers in a Kafka or RabbitMQ pipeline when brokers run low on memory.
  • Controlling concurrent webhook deliveries from a queue so a slow third-party API isn't overwhelmed.
  • Throttling database writes from high-volume event streams by tracking a sliding credit budget.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Streaming & messaging

Related tutorials and quizzes for this topic.