settings.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2014-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Represent MongoClient's configuration."""
  15. import threading
  16. from pymongo import monitor, pool
  17. from pymongo.common import LOCAL_THRESHOLD_MS, SERVER_SELECTION_TIMEOUT
  18. from pymongo.topology_description import TOPOLOGY_TYPE
  19. from pymongo.pool import PoolOptions
  20. from pymongo.server_description import ServerDescription
  21. class TopologySettings(object):
  22. def __init__(self,
  23. seeds=None,
  24. replica_set_name=None,
  25. pool_class=None,
  26. pool_options=None,
  27. monitor_class=None,
  28. condition_class=None,
  29. local_threshold_ms=LOCAL_THRESHOLD_MS,
  30. server_selection_timeout=SERVER_SELECTION_TIMEOUT):
  31. """Represent MongoClient's configuration.
  32. Take a list of (host, port) pairs and optional replica set name.
  33. """
  34. self._seeds = seeds or [('localhost', 27017)]
  35. self._replica_set_name = replica_set_name
  36. self._pool_class = pool_class or pool.Pool
  37. self._pool_options = pool_options or PoolOptions()
  38. self._monitor_class = monitor_class or monitor.Monitor
  39. self._condition_class = condition_class or threading.Condition
  40. self._local_threshold_ms = local_threshold_ms
  41. self._server_selection_timeout = server_selection_timeout
  42. self._direct = (len(self._seeds) == 1 and not replica_set_name)
  43. @property
  44. def seeds(self):
  45. """List of server addresses."""
  46. return self._seeds
  47. @property
  48. def replica_set_name(self):
  49. return self._replica_set_name
  50. @property
  51. def pool_class(self):
  52. return self._pool_class
  53. @property
  54. def pool_options(self):
  55. return self._pool_options
  56. @property
  57. def monitor_class(self):
  58. return self._monitor_class
  59. @property
  60. def condition_class(self):
  61. return self._condition_class
  62. @property
  63. def local_threshold_ms(self):
  64. return self._local_threshold_ms
  65. @property
  66. def server_selection_timeout(self):
  67. return self._server_selection_timeout
  68. @property
  69. def direct(self):
  70. """Connect directly to a single server, or use a set of servers?
  71. True if there is one seed and no replica_set_name.
  72. """
  73. return self._direct
  74. def get_topology_type(self):
  75. if self.direct:
  76. return TOPOLOGY_TYPE.Single
  77. elif self.replica_set_name is not None:
  78. return TOPOLOGY_TYPE.ReplicaSetNoPrimary
  79. else:
  80. return TOPOLOGY_TYPE.Unknown
  81. def get_server_descriptions(self):
  82. """Initial dict of (address, ServerDescription) for all seeds."""
  83. return dict([
  84. (address, ServerDescription(address))
  85. for address in self.seeds])