-
Notifications
You must be signed in to change notification settings - Fork 12
/
gapic_connection.py
52 lines (37 loc) · 1.76 KB
/
gapic_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from typing import AsyncIterator, TypeVar, Optional, Callable, AsyncIterable
import asyncio
from google.cloud.pubsublite.internal.wire.connection import Connection, Request, Response, ConnectionFactory
from google.cloud.pubsublite.internal.wire.work_item import WorkItem
from google.cloud.pubsublite.internal.wire.permanent_failable import PermanentFailable
T = TypeVar('T')
class GapicConnection(Connection[Request, Response], AsyncIterator[Request], PermanentFailable):
_write_queue: 'asyncio.Queue[WorkItem[Request]]'
_response_it: Optional[AsyncIterator[Response]]
def __init__(self):
super().__init__()
self._write_queue = asyncio.Queue(maxsize=1)
def set_response_it(self, response_it: AsyncIterator[Response]):
self._response_it = response_it
async def write(self, request: Request) -> None:
item = WorkItem(request)
await self.await_or_fail(self._write_queue.put(item))
await self.await_or_fail(item.response_future)
async def read(self) -> Response:
return await self.await_or_fail(self._response_it.__anext__())
def __aenter__(self):
return self
def __aexit__(self, exc_type, exc_value, traceback) -> None:
pass
async def __anext__(self) -> Request:
item: WorkItem[Request] = await self.await_or_fail(self._write_queue.get())
item.response_future.set_result(None)
return item.request
def __aiter__(self) -> AsyncIterator[Response]:
return self
class GapicConnectionFactory(ConnectionFactory[Request, Response]):
_producer = Callable[[AsyncIterator[Request]], AsyncIterable[Response]]
def New(self) -> Connection[Request, Response]:
conn = GapicConnection[Request, Response]()
response_iterable = self._producer(conn)
conn.set_response_it(response_iterable.__aiter__())
return conn