@@ -113,16 +113,21 @@ class Server:
113
113
def __init__ (self , target ):
114
114
self .index_name = "main"
115
115
self .index_url = target
116
+ self .index_created = False
117
+ self .create_index_lock = asyncio .Lock ()
116
118
117
119
async def create_index (self ):
118
- url = self .index_url + f"/{ self .index_name } "
119
- async with self .session .put (url ) as resp :
120
- resp .raise_for_status ()
120
+ if self .index_created :
121
+ return
122
+ async with self .create_index_lock :
123
+ url = self .index_url + f"/{ self .index_name } "
124
+ async with self .session .put (url ) as resp :
125
+ resp .raise_for_status ()
126
+ self .index_created = True
121
127
122
128
async def serve (self , listen_host , listen_port ):
123
129
async with aiohttp .ClientSession () as session :
124
130
self .session = session
125
- await self .create_index ()
126
131
server = await asyncio .start_server (
127
132
self .handle_connection , listen_host , listen_port
128
133
)
@@ -135,6 +140,8 @@ async def handle_connection(self, reader, writer):
135
140
proto .index_name = self .index_name
136
141
proto .index_url = self .index_url
137
142
143
+ await self .create_index ()
144
+
138
145
while True :
139
146
try :
140
147
line = await reader .readuntil (b"\n " )
0 commit comments