Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IP Pool Allocator

A small Flask service that manages a pool of 10 host IPs (192.168.0.101192.168.0.110) and leases them out on request for a fixed duration. Built as a solution to a timed backend exercise; the original specification is in docs.txt, and the API keeps the endpoint and field names that spec prescribes (/get_slaves, slaves).

How it works

  • Each host tracks when its current lease ends; a host is free once that time has passed, so leases expire naturally with no background timers.
  • A request for n hosts is granted when at least n are free: the n hosts that freed up earliest are returned and marked busy for the requested duration.
  • If fewer than n are free, the response contains come_back — the number of seconds until n hosts will be free at once (the time until the n-th earliest lease expires, rounded up so clients never return too early).
  • Timing uses time.monotonic(), so the schedule is immune to wall-clock adjustments, and all pool access is guarded by a lock so concurrent requests can never be handed the same host.

Quick start

Requires Python 3 and Flask (see requirements.txt).

make run                # installs dependencies, serves on port 8080

or manually:

pip3 install -r requirements.txt
python3 app.py [port]   # port is optional, defaults to 8080

API

GET /get_slaves?amount=<n>&duration=<seconds>

Requests n hosts from the pool for duration seconds.

Enough hosts free — returns their IPs and marks them busy until the duration ends (HTTP 200):

{"slaves": ["192.168.0.101", "192.168.0.102"]}

Not enough hosts free — returns how many seconds to wait before retrying (HTTP 200):

{"slaves": [], "come_back": 7}

Invalid input — missing, non-integer, or non-positive parameters, or amount greater than the pool size of 10 (HTTP 400):

{"error": "amount and duration must be positive"}

Unknown routes and unsupported methods also return JSON errors (HTTP 404 / 405), so every response from the service is JSON.

Example session

$ curl 'http://localhost:8080/get_slaves?amount=2&duration=10'
{"slaves":["192.168.0.101","192.168.0.102"]}

$ curl 'http://localhost:8080/get_slaves?amount=9&duration=10'
{"come_back":10,"slaves":[]}

$ curl 'http://localhost:8080/get_slaves?amount=0&duration=10'
{"error":"amount and duration must be positive"}

Tests

make test

Runs a pytest suite (tests/test_app.py) covering the exact example timeline from the spec, lease expiry, wait-time rounding, input validation, and a 12-thread concurrency test that verifies no IP is ever handed out twice. Time-dependent tests use a fake monotonic clock, so the suite runs deterministically in well under a second.

About

Flask REST service that leases host IPs from a fixed pool for a requested duration, with thread-safe allocation, wait-time estimates, and a deterministic pytest suite

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages