-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PYTHON-3186 Avoid SDAM heartbeat timeouts on AWS Lambda (#912)
Poll monitor socket with timeout=0 one last time after timeout expires. This avoids heartbeat timeouts and connection churn on Lambda and other FaaS envs.
- Loading branch information
1 parent
1d30802
commit c58950a
Showing
4 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2022-present MongoDB, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Used by test_client.TestClient.test_sigstop_sigcont.""" | ||
|
||
import logging | ||
import sys | ||
|
||
sys.path[0:0] = [""] | ||
|
||
from pymongo import monitoring | ||
from pymongo.mongo_client import MongoClient | ||
|
||
|
||
class HeartbeatLogger(monitoring.ServerHeartbeatListener): | ||
"""Log events until the listener is closed.""" | ||
|
||
def __init__(self): | ||
self.closed = False | ||
|
||
def close(self): | ||
self.closed = True | ||
|
||
def started(self, event: monitoring.ServerHeartbeatStartedEvent) -> None: | ||
if self.closed: | ||
return | ||
logging.info("%s", event) | ||
|
||
def succeeded(self, event: monitoring.ServerHeartbeatSucceededEvent) -> None: | ||
if self.closed: | ||
return | ||
logging.info("%s", event) | ||
|
||
def failed(self, event: monitoring.ServerHeartbeatFailedEvent) -> None: | ||
if self.closed: | ||
return | ||
logging.warning("%s", event) | ||
|
||
|
||
def main(uri: str) -> None: | ||
heartbeat_logger = HeartbeatLogger() | ||
client = MongoClient( | ||
uri, | ||
event_listeners=[heartbeat_logger], | ||
heartbeatFrequencyMS=500, | ||
connectTimeoutMS=500, | ||
) | ||
client.admin.command("ping") | ||
logging.info("TEST STARTED") | ||
# test_sigstop_sigcont will SIGSTOP and SIGCONT this process in this loop. | ||
while True: | ||
try: | ||
data = input('Type "q" to quit: ') | ||
except EOFError: | ||
break | ||
if data == "q": | ||
break | ||
client.admin.command("ping") | ||
logging.info("TEST COMPLETED") | ||
heartbeat_logger.close() | ||
client.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 2: | ||
print("unknown or missing options") | ||
print(f"usage: python3 {sys.argv[0]} 'mongodb://localhost'") | ||
exit(1) | ||
|
||
# Enable logs in this format: | ||
# 2022-03-30 12:40:55,582 INFO <ServerHeartbeatStartedEvent ('localhost', 27017)> | ||
FORMAT = "%(asctime)s %(levelname)s %(message)s" | ||
logging.basicConfig(format=FORMAT, level=logging.INFO) | ||
main(sys.argv[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters