A small Flask service that manages a pool of 10 host IPs
(192.168.0.101–192.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).
- 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
nhosts is granted when at leastnare free: thenhosts that freed up earliest are returned and marked busy for the requested duration. - If fewer than
nare free, the response containscome_back— the number of seconds untilnhosts will be free at once (the time until then-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.
Requires Python 3 and Flask (see requirements.txt).
make run # installs dependencies, serves on port 8080or manually:
pip3 install -r requirements.txt
python3 app.py [port] # port is optional, defaults to 8080Requests 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.
$ 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"}make testRuns 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.