-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: async
-
None
Avoid awaiting coroutines when holding locks. In the AsyncMongoClient there are various places where we await coroutines while holding the Topology or Pool locks. This is problematic because it increases lock contention.
Instead we should remove all awaits as much as possible. For example in the pool:
async with self.lock: while ( self.conns and self.conns[-1].idle_time_seconds() > self.opts.max_idle_time_seconds ): conn = self.conns.pop() await conn.close_conn(ConnectionClosedReason.IDLE)
Instead we can do:
close_conns = [] async with self.lock: while ( self.conns and self.conns[-1].idle_time_seconds() > self.opts.max_idle_time_seconds ): close_conns.append(self.conns.pop()) for conn in close_conns: await conn.close_conn(ConnectionClosedReason.IDLE)