Home > Net >  Using PyMongo with docker-compose generating 'waves' of connections
Using PyMongo with docker-compose generating 'waves' of connections

Time:11-02

I'm building a system with docker-compose with an FastAPI api and Mongo as the runtime DB. The basic architecture looks as follows (relevant parts of my docker-compose.yaml -file):

version: "3.4"

services:
  api:
    build:
      context: ./api
    ports:
      - "5057:5057"
    expose:
      - "5057"
    working_dir: /home/app
    restart: always
    links:
      - mongo
    depends_on:
      - mongo
    networks:
      - intercomm
    command: uvicorn api:app --host 0.0.0.0 --ssl-keyfile=./localhost-key.pem --ssl-certfile=./localhost.pem --port 5057 --reload

  mongo:
    image: mongo
    environment: 
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password
    ports:
      - '27017:27017'
    networks:
      - intercomm
    volumes:
      - runtimedb:/data/db/

networks:
  intercomm:

volumes:
  runtimedb:

An example call to the database looks something like this:

import pymongo

MONGO_DB_USERNAME = 'root'
MONGO_DB_PASSWORD = 'password'
MONGO_DB_SERVICE_NAME = 'mongo'
MONGO_DB_PORT = '27017'

RUNTIME_LOGGING_ENDPOINT = f'mongodb://{MONGO_DB_USERNAME}:{MONGO_DB_PASSWORD}@{MONGO_DB_SERVICE_NAME}:{MONGO_DB_PORT}/'

RUNDB_NAME = 'test_rundb'
RUNDB_COLLECTION_NAME = 'test_collection'

def initialize_process(data_to_insert : dict):

    with pymongo.MongoClient(RUNTIME_LOGGING_ENDPOINT) as client:
        db = client[RUNDB_NAME]
        db_target_collection = db[RUNDB_COLLECTION_NAME]

        try:
            db_target_collection.insert_one(data_to_insert)
        except Exception as e:
            logging.error(f'Failed initialization with data {dict(init_data)} due to {e}.')

The system boots and starts running quite nicely, except that mongo is periodically dumping a huge amount of log data to the terminal, and at very unpredictable intervals turns unresponsive. I haven't been able to figure out if the problem is with my Mongo setup or if I'm doing wrong with the Python side?

These very verbose mongo-log dumps seem to have the following rough formula:

Start with N=3.

  1. Connection information log messages for Pymongo/CPython with connectionCount:n, where n goes from 1 to N. Messages say "msg":"Connection accepted" and "msg":"client metadata".
  2. An "Authentication succeeded" log message, saying "msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42344","extraInfo":{}}}
  3. Log messages for the closing of the connections in step 1 in reverse order, i.e. `"Connection ended" for each opened connection.

Then you increase N by one and repat steps 1-3 until you get to a big enough N, e.g. N=8, at which point you do steps 1&2 but for step 3 you add messages about "msg":"Interrupted operation as its client disconnected".

A literal example is here:

mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.695 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn151","msg":"client metadata","attr":{"remote":"172.19.0.9:42336","client":"conn151","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.698 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42340","uuid":"f60dc155-3dee-4250-9754-0e6e233a545b","connectionId":152,"connectionCount":2}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.699 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn152","msg":"client metadata","attr":{"remote":"172.19.0.9:42340","client":"conn152","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.700 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42344","uuid":"ac1876c8-991c-4160-863a-91540f7555c2","connectionId":153,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.700 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn153","msg":"client metadata","attr":{"remote":"172.19.0.9:42344","client":"conn153","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.712 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn153","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42344","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.714 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn153","msg":"Connection ended","attr":{"remote":"172.19.0.9:42344","uuid":"ac1876c8-991c-4160-863a-91540f7555c2","connectionId":153,"connectionCount":2}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.714 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn152","msg":"Connection ended","attr":{"remote":"172.19.0.9:42340","uuid":"f60dc155-3dee-4250-9754-0e6e233a545b","connectionId":152,"connectionCount":1}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.717 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42358","uuid":"50041fc5-74c7-4120-86c1-99ba6ee88e2a","connectionId":154,"connectionCount":2}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.717 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn154","msg":"client metadata","attr":{"remote":"172.19.0.9:42358","client":"conn154","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.719 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42366","uuid":"c4729060-3ac3-4577-8935-dd698cd14cbd","connectionId":155,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.719 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn155","msg":"client metadata","attr":{"remote":"172.19.0.9:42366","client":"conn155","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.719 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42374","uuid":"aeef6a56-6520-4c94-aa70-61a5a52e60a1","connectionId":156,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.720 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn156","msg":"client metadata","attr":{"remote":"172.19.0.9:42374","client":"conn156","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.733 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn156","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42374","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.735 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn155","msg":"Connection ended","attr":{"remote":"172.19.0.9:42366","uuid":"c4729060-3ac3-4577-8935-dd698cd14cbd","connectionId":155,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.735 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn156","msg":"Connection ended","attr":{"remote":"172.19.0.9:42374","uuid":"aeef6a56-6520-4c94-aa70-61a5a52e60a1","connectionId":156,"connectionCount":2}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.737 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42390","uuid":"e45d69f5-4b00-436c-b1f1-f319a85208db","connectionId":157,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.738 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn157","msg":"client metadata","attr":{"remote":"172.19.0.9:42390","client":"conn157","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.739 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42394","uuid":"4546de21-0fd0-42fa-9801-d9d85919a168","connectionId":158,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.740 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42398","uuid":"24274fc0-1d7c-4ffa-a03f-3d59f537311a","connectionId":159,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.740 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn158","msg":"client metadata","attr":{"remote":"172.19.0.9:42394","client":"conn158","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.741 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn159","msg":"client metadata","attr":{"remote":"172.19.0.9:42398","client":"conn159","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.752 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn158","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42394","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.754 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn159","msg":"Connection ended","attr":{"remote":"172.19.0.9:42398","uuid":"24274fc0-1d7c-4ffa-a03f-3d59f537311a","connectionId":159,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.754 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn158","msg":"Connection ended","attr":{"remote":"172.19.0.9:42394","uuid":"4546de21-0fd0-42fa-9801-d9d85919a168","connectionId":158,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.756 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42412","uuid":"82c988ec-f273-4eba-91b2-840ebf8f035c","connectionId":160,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.757 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn160","msg":"client metadata","attr":{"remote":"172.19.0.9:42412","client":"conn160","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.759 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42420","uuid":"8cd89266-a022-4516-b3be-d3441349db59","connectionId":161,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.760 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42436","uuid":"47614b9c-1725-42c6-a2fd-6e4389b60c39","connectionId":162,"connectionCount":6}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.760 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn161","msg":"client metadata","attr":{"remote":"172.19.0.9:42420","client":"conn161","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.761 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn162","msg":"client metadata","attr":{"remote":"172.19.0.9:42436","client":"conn162","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.772 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn162","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42436","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.774 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn161","msg":"Connection ended","attr":{"remote":"172.19.0.9:42420","uuid":"8cd89266-a022-4516-b3be-d3441349db59","connectionId":161,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.774 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn162","msg":"Connection ended","attr":{"remote":"172.19.0.9:42436","uuid":"47614b9c-1725-42c6-a2fd-6e4389b60c39","connectionId":162,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.776 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42446","uuid":"5cf3e848-6e31-4814-9188-4ca049264da9","connectionId":163,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.777 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn163","msg":"client metadata","attr":{"remote":"172.19.0.9:42446","client":"conn163","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.779 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42452","uuid":"37905119-996a-4611-a301-f85033a63790","connectionId":164,"connectionCount":6}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.780 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42468","uuid":"ab5745a0-d6bd-4843-9deb-86eb661423e5","connectionId":165,"connectionCount":7}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.780 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn164","msg":"client metadata","attr":{"remote":"172.19.0.9:42452","client":"conn164","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.781 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn165","msg":"client metadata","attr":{"remote":"172.19.0.9:42468","client":"conn165","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.792 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn164","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42452","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.794 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn165","msg":"Connection ended","attr":{"remote":"172.19.0.9:42468","uuid":"ab5745a0-d6bd-4843-9deb-86eb661423e5","connectionId":165,"connectionCount":6}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.794 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn164","msg":"Connection ended","attr":{"remote":"172.19.0.9:42452","uuid":"37905119-996a-4611-a301-f85033a63790","connectionId":164,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.797 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42472","uuid":"e8a308a6-0aaf-42b9-9608-da9a0141eb1b","connectionId":166,"connectionCount":6}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.797 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn166","msg":"client metadata","attr":{"remote":"172.19.0.9:42472","client":"conn166","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.799 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42488","uuid":"a34d1f21-b82a-4c40-a518-4d1f84b87cf6","connectionId":167,"connectionCount":7}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.800 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn167","msg":"client metadata","attr":{"remote":"172.19.0.9:42488","client":"conn167","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.800 00:00"},"s":"I",  "c":"NETWORK",  "id":22943,   "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.19.0.9:42496","uuid":"e3817592-ce45-4ffb-99db-e9694777f772","connectionId":168,"connectionCount":8}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.801 00:00"},"s":"I",  "c":"NETWORK",  "id":51800,   "ctx":"conn168","msg":"client metadata","attr":{"remote":"172.19.0.9:42496","client":"conn168","doc":{"driver":{"name":"PyMongo","version":"4.3.2"},"os":{"type":"Linux","name":"Linux","architecture":"x86_64","version":"5.15.0-50-generic"},"platform":"CPython 3.10.4.final.0"}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.813 00:00"},"s":"I",  "c":"ACCESS",   "id":20250,   "ctx":"conn168","msg":"Authentication succeeded","attr":{"mechanism":"SCRAM-SHA-256","speculative":true,"principalName":"root","authenticationDatabase":"admin","remote":"172.19.0.9:42496","extraInfo":{}}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.815 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn167","msg":"Connection ended","attr":{"remote":"172.19.0.9:42488","uuid":"a34d1f21-b82a-4c40-a518-4d1f84b87cf6","connectionId":167,"connectionCount":7}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:18.815 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn168","msg":"Connection ended","attr":{"remote":"172.19.0.9:42496","uuid":"e3817592-ce45-4ffb-99db-e9694777f772","connectionId":168,"connectionCount":6}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.200 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn151","msg":"Interrupted operation as its client disconnected","attr":{"opId":2798}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.201 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn151","msg":"Connection ended","attr":{"remote":"172.19.0.9:42336","uuid":"8137556c-39ce-49f0-9e93-99e91159e553","connectionId":151,"connectionCount":5}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.219 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn154","msg":"Interrupted operation as its client disconnected","attr":{"opId":2804}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.220 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn154","msg":"Connection ended","attr":{"remote":"172.19.0.9:42358","uuid":"50041fc5-74c7-4120-86c1-99ba6ee88e2a","connectionId":154,"connectionCount":4}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.240 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn157","msg":"Interrupted operation as its client disconnected","attr":{"opId":2812}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.240 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn157","msg":"Connection ended","attr":{"remote":"172.19.0.9:42390","uuid":"e45d69f5-4b00-436c-b1f1-f319a85208db","connectionId":157,"connectionCount":3}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.260 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn160","msg":"Interrupted operation as its client disconnected","attr":{"opId":2819}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.260 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn160","msg":"Connection ended","attr":{"remote":"172.19.0.9:42412","uuid":"82c988ec-f273-4eba-91b2-840ebf8f035c","connectionId":160,"connectionCount":2}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.280 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn163","msg":"Interrupted operation as its client disconnected","attr":{"opId":2826}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.280 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn163","msg":"Connection ended","attr":{"remote":"172.19.0.9:42446","uuid":"5cf3e848-6e31-4814-9188-4ca049264da9","connectionId":163,"connectionCount":1}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.300 00:00"},"s":"I",  "c":"-",        "id":20883,   "ctx":"conn166","msg":"Interrupted operation as its client disconnected","attr":{"opId":2833}}
mongo_1           | {"t":{"$date":"2022-11-01T14:31:19.300 00:00"},"s":"I",  "c":"NETWORK",  "id":22944,   "ctx":"conn166","msg":"Connection ended","attr":{"remote":"172.19.0.9:42472","uuid":"e8a308a6-0aaf-42b9-9608-da9a0141eb1b","connectionId":166,"connectionCount":0}}

CodePudding user response:

Mongodb driver opens a pool of connections and keeps them persisten to reuse on consecutive calls per client.

with pymongo.MongoClient(RUNTIME_LOGGING_ENDPOINT) as client:

Creates a new client, opens all connections, then closes all connections when you exit the with block

Make client a global variable and re-use it in all functions, don't use with statement.

  • Related