|
| 1 | +import asyncio |
| 2 | +from contextlib import asynccontextmanager |
| 3 | +from typing import AsyncGenerator |
| 4 | +from ..client import Client |
| 5 | +from ..room import Room, event |
| 6 | + |
| 7 | + |
| 8 | +async def setup(client: Client, collection: str = 'lock'): |
| 9 | + has_collection = await client.query("""//ti |
| 10 | + has_collection(name); |
| 11 | + """, name=collection, scope='/t') |
| 12 | + |
| 13 | + if has_collection: |
| 14 | + return |
| 15 | + |
| 16 | + await client.query("""//ti |
| 17 | + new_collection(name); |
| 18 | + """, name=collection, scope='/t') |
| 19 | + |
| 20 | + await client.query("""//ti |
| 21 | +
|
| 22 | + set_type('Inner', { |
| 23 | + room: 'room', |
| 24 | + task: 'task', |
| 25 | + timeout: 'int', |
| 26 | + set_task: |this, lock_id| { |
| 27 | + this.task = task( |
| 28 | + datetime().move('seconds', this.timeout), |
| 29 | + |_, lock_id, room_id| wse(Lock(lock_id).release(room_id)), |
| 30 | + [lock_id, this.room.id()], |
| 31 | + ); |
| 32 | + nil; |
| 33 | + }, |
| 34 | + }); |
| 35 | +
|
| 36 | + set_type('Lock', { |
| 37 | + queue: '[Inner]', |
| 38 | + go: |this| { |
| 39 | + if (!this.queue) return nil; |
| 40 | + inner = this.queue.first(); |
| 41 | + inner.set_task(this.id()); |
| 42 | + inner.room.set_name('go'); |
| 43 | + inner.room.emit('go'); |
| 44 | + }, |
| 45 | + acquire: |this, timeout| { |
| 46 | + size = this.queue.len(); |
| 47 | + immediately = size == 0; |
| 48 | + inner = Inner{timeout:,}; |
| 49 | + this.queue.push(inner); |
| 50 | + immediately && inner.set_task(this.id()); |
| 51 | + [immediately, inner.room.id(), size]; |
| 52 | + }, |
| 53 | + release: |this, room_id| try({ |
| 54 | + if (this.queue.first().room.id() == room_id) { |
| 55 | + this.queue.first().task.del(); |
| 56 | + this.queue.shift(); |
| 57 | + this.go(); |
| 58 | + } else { |
| 59 | + this.queue |
| 60 | + .remove(|inner| inner.room.id() == room_id) |
| 61 | + .each(|inner| inner.task.del()); |
| 62 | + }; |
| 63 | + nil; |
| 64 | + }), |
| 65 | + }); |
| 66 | +
|
| 67 | + set_type('Root', { |
| 68 | + lock: 'thing<Lock>', |
| 69 | + version: 'int' |
| 70 | + }); |
| 71 | +
|
| 72 | + new_procedure('acquire', |name, timeout| { |
| 73 | + .lock.get(name, .lock[name] = Lock{}).acquire(timeout); |
| 74 | + }); |
| 75 | +
|
| 76 | + new_procedure('test', |room_id| { |
| 77 | + room(room_id).name() == 'go'; |
| 78 | + }); |
| 79 | +
|
| 80 | + new_procedure('locked', |name| { |
| 81 | + bool(.lock[name].queue); |
| 82 | + }); |
| 83 | +
|
| 84 | + new_procedure('release', |name, room_id| { |
| 85 | + wse(.lock[name].release(room_id)); |
| 86 | + }); |
| 87 | +
|
| 88 | + .to_type('Root'); |
| 89 | + """, scope=f'//{collection}') |
| 90 | + |
| 91 | + |
| 92 | +class _InnerRoom(Room): |
| 93 | + |
| 94 | + future: asyncio.Future |
| 95 | + |
| 96 | + def on_init(self) -> None: |
| 97 | + self.future = asyncio.Future() |
| 98 | + |
| 99 | + async def on_join(self) -> None: |
| 100 | + # We might have missed the event during the join. If so, set the |
| 101 | + # future result to continue. |
| 102 | + ok = await self.client.run('test', self.id, scope=self.scope) |
| 103 | + if ok and not self.future.done(): |
| 104 | + self.future.set_result(None) |
| 105 | + |
| 106 | + @event('go') |
| 107 | + def on_go(self): |
| 108 | + if not self.future.done(): |
| 109 | + self.future.set_result(None) |
| 110 | + |
| 111 | + |
| 112 | +@asynccontextmanager |
| 113 | +async def lock(client: Client, name: str, |
| 114 | + scope: str = '//lock', |
| 115 | + timeout: int = 60) -> AsyncGenerator[int, None]: |
| 116 | + |
| 117 | + res: tuple[bool, int, int] = \ |
| 118 | + await client.run('acquire', name, timeout, scope=scope) |
| 119 | + |
| 120 | + immediately, room_id, size = res |
| 121 | + if not immediately: |
| 122 | + room = _InnerRoom(room_id, scope=scope) |
| 123 | + await room.join(client, wait=None) |
| 124 | + try: |
| 125 | + await asyncio.wait_for(room.future, timeout=timeout*size) |
| 126 | + except asyncio.TimeoutError: |
| 127 | + pass |
| 128 | + |
| 129 | + try: |
| 130 | + yield room_id # Lock Id assigned to the 'as' target (not required) |
| 131 | + finally: |
| 132 | + await client.run('release', name, room_id, scope=scope) |
| 133 | + |
| 134 | + |
| 135 | +async def locked(client: Client, name: str, scope: str = '//lock') -> bool: |
| 136 | + res: bool = await client.run('locked', name, scope=scope) |
| 137 | + return res |
0 commit comments