| 8 | | It happened after 220 api calls within the span of 7minutes, which probably means x5-10 database calls. |
| 9 | | |
| 10 | | This forces me to turn of connection pooling. |
| | 8 | Further investigation shows the reason was a Threaded Middleware orm interaction, like this simplified code. |
| | 9 | |
| | 10 | class TimeMonitorMiddleware(object): |
| | 11 | batched_logs: List[RequestLog] = [] |
| | 12 | |
| | 13 | def __call__(self, request: WSGIRequest) -> HttpResponse: |
| | 14 | # ... |
| | 15 | |
| | 16 | # Save batched logs |
| | 17 | if len(self.batched_logs) >= 50: |
| | 18 | Thread(target=RequestLog.objects.bulk_create, args=(batched_logs,)).start() |
| | 19 | |
| | 20 | return response |
| | 21 | |
| | 22 | |