Skip to content

Commit 1bb2170

Browse files
Move protocol constants initialization to protocol.py
1 parent 4615ed3 commit 1bb2170

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

txmongo/connection.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
from txmongo.protocol import MongoProtocol
1919
from txmongo.utils import get_err, timeout
2020

21-
DEFAULT_MAX_BSON_SIZE = 16777216
22-
DEFAULT_MAX_WRITE_BATCH_SIZE = 1000
23-
DEFAULT_MAX_MESSAGE_SIZE = 48000000
24-
25-
2621
_PRIMARY_READ_PREFERENCES = {
2722
ReadPreference.PRIMARY.mode,
2823
ReadPreference.PRIMARY_PREFERRED.mode,
@@ -77,7 +72,7 @@ def _initializeProto(self, proto):
7772

7873
@staticmethod
7974
@timeout
80-
def __send_ismaster(proto, _deadline):
75+
def __send_ismaster(proto, _deadline) -> defer.Deferred[dict]:
8176
return proto.send_op_query_command("admin", {"ismaster": 1})
8277

8378
@defer.inlineCallbacks
@@ -110,23 +105,7 @@ def configure(self, proto: MongoProtocol):
110105
msg = "TxMongo: Mongo instance does not match requested replicaSet."
111106
raise ConfigurationError(msg)
112107

113-
# FIXME: move this initialization to MongoProtocol class
114-
# Track max bson object size limit.
115-
proto.max_bson_size = config.get("maxBsonObjectSize", DEFAULT_MAX_BSON_SIZE)
116-
proto.max_write_batch_size = config.get(
117-
"maxWriteBatchSize", DEFAULT_MAX_WRITE_BATCH_SIZE
118-
)
119-
proto.max_message_size = config.get(
120-
"maxMessageSizeBytes", DEFAULT_MAX_MESSAGE_SIZE
121-
)
122-
proto.set_wire_versions(
123-
config.get("minWireVersion", 0), config.get("maxWireVersion", 0)
124-
)
125-
126-
# MongoDB < 4.0
127-
if proto.max_wire_version < 7:
128-
warnings.warn("TxMongo: MongoDB version <4.0 is not supported")
129-
raise ConfigurationError("TxMongo: MongoDB version <4.0 is not supported")
108+
proto.init_from_hello_response(config)
130109

131110
# Track the other hosts in the replica set.
132111
hosts = config.get("hosts")

txmongo/protocol.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hmac
1919
import logging
2020
import struct
21+
import warnings
2122
from abc import ABCMeta, abstractmethod
2223
from dataclasses import dataclass, field
2324
from hashlib import sha1
@@ -28,6 +29,7 @@
2829
from bson import SON, Binary, CodecOptions
2930
from pymongo.errors import (
3031
AutoReconnect,
32+
ConfigurationError,
3133
ConnectionFailure,
3234
CursorNotFound,
3335
NotPrimaryError,
@@ -67,6 +69,11 @@ def _digest(msg, mac=mac):
6769
return to_bytes(_ui, mac.digest_size, "big")
6870

6971

72+
DEFAULT_MAX_BSON_SIZE = 16777216
73+
DEFAULT_MAX_WRITE_BATCH_SIZE = 1000
74+
DEFAULT_MAX_MESSAGE_SIZE = 48000000
75+
76+
7077
INT_MAX = 2147483647
7178

7279
OP_REPLY = 1
@@ -435,6 +442,25 @@ def __init__(self):
435442

436443
self.__auth_lock = defer.DeferredLock()
437444

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+
438464
def inflight(self):
439465
return len(self.__deferreds)
440466

@@ -558,7 +584,7 @@ def set_wire_versions(self, min_wire_version, max_wire_version):
558584
self.min_wire_version = min_wire_version
559585
self.max_wire_version = max_wire_version
560586

561-
def send_op_query_command(self, database, query):
587+
def send_op_query_command(self, database, query) -> defer.Deferred[dict]:
562588
cmd_collection = str(database) + ".$cmd"
563589
return self.send_query(
564590
Query(collection=cmd_collection, query=bson.encode(query))

0 commit comments

Comments
 (0)