|
18 | 18 | import hmac
|
19 | 19 | import logging
|
20 | 20 | import struct
|
| 21 | +import warnings |
21 | 22 | from abc import ABCMeta, abstractmethod
|
22 | 23 | from dataclasses import dataclass, field
|
23 | 24 | from hashlib import sha1
|
|
28 | 29 | from bson import SON, Binary, CodecOptions
|
29 | 30 | from pymongo.errors import (
|
30 | 31 | AutoReconnect,
|
| 32 | + ConfigurationError, |
31 | 33 | ConnectionFailure,
|
32 | 34 | CursorNotFound,
|
33 | 35 | NotPrimaryError,
|
@@ -67,6 +69,11 @@ def _digest(msg, mac=mac):
|
67 | 69 | return to_bytes(_ui, mac.digest_size, "big")
|
68 | 70 |
|
69 | 71 |
|
| 72 | +DEFAULT_MAX_BSON_SIZE = 16777216 |
| 73 | +DEFAULT_MAX_WRITE_BATCH_SIZE = 1000 |
| 74 | +DEFAULT_MAX_MESSAGE_SIZE = 48000000 |
| 75 | + |
| 76 | + |
70 | 77 | INT_MAX = 2147483647
|
71 | 78 |
|
72 | 79 | OP_REPLY = 1
|
@@ -435,6 +442,25 @@ def __init__(self):
|
435 | 442 |
|
436 | 443 | self.__auth_lock = defer.DeferredLock()
|
437 | 444 |
|
| 445 | + def init_from_hello_response(self, config: dict) -> None: |
| 446 | + """`config` is a dict from server hello response""" |
| 447 | + # Track max bson object size limit. |
| 448 | + self.max_bson_size = config.get("maxBsonObjectSize", DEFAULT_MAX_BSON_SIZE) |
| 449 | + self.max_write_batch_size = config.get( |
| 450 | + "maxWriteBatchSize", DEFAULT_MAX_WRITE_BATCH_SIZE |
| 451 | + ) |
| 452 | + self.max_message_size = config.get( |
| 453 | + "maxMessageSizeBytes", DEFAULT_MAX_MESSAGE_SIZE |
| 454 | + ) |
| 455 | + self.set_wire_versions( |
| 456 | + config.get("minWireVersion", 0), config.get("maxWireVersion", 0) |
| 457 | + ) |
| 458 | + |
| 459 | + # MongoDB < 4.0 |
| 460 | + if self.max_wire_version < 7: |
| 461 | + warnings.warn("TxMongo: MongoDB version <4.0 is not supported") |
| 462 | + raise ConfigurationError("TxMongo: MongoDB version <4.0 is not supported") |
| 463 | + |
438 | 464 | def inflight(self):
|
439 | 465 | return len(self.__deferreds)
|
440 | 466 |
|
@@ -558,7 +584,7 @@ def set_wire_versions(self, min_wire_version, max_wire_version):
|
558 | 584 | self.min_wire_version = min_wire_version
|
559 | 585 | self.max_wire_version = max_wire_version
|
560 | 586 |
|
561 |
| - def send_op_query_command(self, database, query): |
| 587 | + def send_op_query_command(self, database, query) -> defer.Deferred[dict]: |
562 | 588 | cmd_collection = str(database) + ".$cmd"
|
563 | 589 | return self.send_query(
|
564 | 590 | Query(collection=cmd_collection, query=bson.encode(query))
|
|
0 commit comments