28 lines
520 B
Python
28 lines
520 B
Python
import requests
|
|
|
|
|
|
LOCKS_URL = 'http://locks'
|
|
|
|
|
|
class Conflict(Exception):
|
|
pass
|
|
|
|
|
|
def acquire(name: str, ttl: int = 1):
|
|
resp = requests.post(f'{LOCKS_URL}/api/v1/acquire', json={
|
|
'name': name,
|
|
'ttl': ttl,
|
|
})
|
|
if resp.status_code == 409:
|
|
raise Conflict
|
|
if resp.status_code != 202:
|
|
raise Exception
|
|
|
|
|
|
def release(name: str):
|
|
resp = requests.post(f'{LOCKS_URL}/api/v1/release', json={
|
|
'name': name,
|
|
})
|
|
if resp.status_code != 202:
|
|
raise Exception
|