@@ -26,17 +26,19 @@ class WSProxy:
26
26
'app_name' , 'protocol' ,
27
27
'args' , 'envs' ,
28
28
'reader' , 'writer' ,
29
- 'down_task' ,
30
29
)
31
30
32
- def __init__ (self , api_session : AsyncSession ,
33
- session_name : str ,
34
- app_name : str ,
35
- protocol : str ,
36
- args : MutableMapping [str , Union [None , str , List [str ]]],
37
- envs : MutableMapping [str , str ],
38
- reader : asyncio .StreamReader ,
39
- writer : asyncio .StreamWriter ):
31
+ def __init__ (
32
+ self ,
33
+ api_session : AsyncSession ,
34
+ session_name : str ,
35
+ app_name : str ,
36
+ protocol : str ,
37
+ args : MutableMapping [str , Union [None , str , List [str ]]],
38
+ envs : MutableMapping [str , str ],
39
+ reader : asyncio .StreamReader ,
40
+ writer : asyncio .StreamWriter ,
41
+ ) -> None :
40
42
self .api_session = api_session
41
43
self .session_name = session_name
42
44
self .app_name = app_name
@@ -45,9 +47,8 @@ def __init__(self, api_session: AsyncSession,
45
47
self .envs = envs
46
48
self .reader = reader
47
49
self .writer = writer
48
- self .down_task = None
49
50
50
- async def run (self ):
51
+ async def run (self ) -> None :
51
52
prefix = get_naming (self .api_session .api_version , 'path' )
52
53
path = f"/stream/{ prefix } /{ self .session_name } /{ self .protocol } proxy"
53
54
params = {'app' : self .app_name }
@@ -63,7 +64,7 @@ async def run(self):
63
64
content_type = "application/json" )
64
65
async with api_rqst .connect_websocket () as ws :
65
66
66
- async def downstream ():
67
+ async def downstream () -> None :
67
68
try :
68
69
async for msg in ws :
69
70
if msg .type == aiohttp .WSMsgType .ERROR :
@@ -79,17 +80,16 @@ async def downstream():
79
80
except ConnectionResetError :
80
81
pass # shutting down
81
82
except asyncio .CancelledError :
82
- raise
83
+ pass
83
84
finally :
84
85
self .writer .close ()
85
- if hasattr (self .writer , 'wait_closed' ): # Python 3.7+
86
- try :
87
- await self .writer .wait_closed ()
88
- except (BrokenPipeError , IOError ):
89
- # closed
90
- pass
91
-
92
- self .down_task = asyncio .ensure_future (downstream ())
86
+ try :
87
+ await self .writer .wait_closed ()
88
+ except (BrokenPipeError , IOError ):
89
+ # closed
90
+ pass
91
+
92
+ down_task = asyncio .create_task (downstream ())
93
93
try :
94
94
while True :
95
95
chunk = await self .reader .read (DEFAULT_CHUNK_SIZE )
@@ -101,11 +101,11 @@ async def downstream():
101
101
except asyncio .CancelledError :
102
102
raise
103
103
finally :
104
- if not self . down_task .done ():
105
- await self . down_task
106
- self . down_task = None
104
+ if not down_task .done ():
105
+ down_task . cancel ()
106
+ await down_task
107
107
108
- async def write_error (self , msg ) :
108
+ async def write_error (self , msg : aiohttp . WSMessage ) -> None :
109
109
if isinstance (msg .data , bytes ):
110
110
error_msg = msg .data .decode ('utf8' )
111
111
else :
@@ -138,11 +138,17 @@ class ProxyRunnerContext:
138
138
local_server : Optional [asyncio .AbstractServer ]
139
139
exit_code : int
140
140
141
- def __init__ (self , host : str , port : int ,
142
- session_name : str , app_name : str , * ,
143
- protocol : str = 'http' ,
144
- args : Sequence [str ] = None ,
145
- envs : Sequence [str ] = None ) -> None :
141
+ def __init__ (
142
+ self ,
143
+ host : str ,
144
+ port : int ,
145
+ session_name : str ,
146
+ app_name : str ,
147
+ * ,
148
+ protocol : str = 'http' ,
149
+ args : Sequence [str ] = None ,
150
+ envs : Sequence [str ] = None ,
151
+ ) -> None :
146
152
self .host = host
147
153
self .port = port
148
154
self .session_name = session_name
@@ -180,8 +186,11 @@ def __init__(self, host: str, port: int,
180
186
else :
181
187
self .envs [split [0 ]] = ''
182
188
183
- async def handle_connection (self , reader : asyncio .StreamReader ,
184
- writer : asyncio .StreamWriter ) -> None :
189
+ async def handle_connection (
190
+ self ,
191
+ reader : asyncio .StreamReader ,
192
+ writer : asyncio .StreamWriter ,
193
+ ) -> None :
185
194
assert self .api_session is not None
186
195
p = WSProxy (self .api_session , self .session_name ,
187
196
self .app_name , self .protocol ,
0 commit comments