|
25 | 25 | from collections.abc import Mapping |
26 | 26 | from concurrent.futures import ThreadPoolExecutor, FIRST_COMPLETED, wait as wait_futures |
27 | 27 | from copy import copy |
28 | | -from functools import partial, reduce, wraps |
| 28 | +from functools import partial, wraps |
29 | 29 | from itertools import groupby, count, chain |
30 | 30 | import json |
31 | 31 | import logging |
|
50 | 50 | from cassandra.connection import (ConnectionException, ConnectionShutdown, |
51 | 51 | ConnectionHeartbeat, ProtocolVersionUnsupported, |
52 | 52 | EndPoint, DefaultEndPoint, DefaultEndPointFactory, |
53 | | - ContinuousPagingState, SniEndPointFactory, ConnectionBusy) |
| 53 | + ContinuousPagingState, SniEndPointFactory, ConnectionBusy, Connection) |
54 | 54 | from cassandra.cqltypes import UserType |
55 | 55 | import cassandra.cqltypes as types |
56 | 56 | from cassandra.encoder import Encoder |
| 57 | +from cassandra.io.reactorloader import get_default_connection_class, try_twisted_connection |
57 | 58 | from cassandra.protocol import (QueryMessage, ResultMessage, |
58 | 59 | ErrorMessage, ReadTimeoutErrorMessage, |
59 | 60 | WriteTimeoutErrorMessage, |
|
96 | 97 | from cassandra.datastax import cloud as dscloud |
97 | 98 | from cassandra.scylla.cloud import CloudConfiguration |
98 | 99 |
|
99 | | -try: |
100 | | - from cassandra.io.twistedreactor import TwistedConnection |
101 | | -except ImportError: |
102 | | - TwistedConnection = None |
103 | | - |
104 | | -try: |
105 | | - from cassandra.io.eventletreactor import EventletConnection |
106 | | -except (ImportError, AttributeError): |
107 | | - # AttributeError was add for handling python 3.12 https://github.com/eventlet/eventlet/issues/812 |
108 | | - # TODO: remove it when eventlet issue would be fixed |
109 | | - EventletConnection = None |
| 100 | +TwistedConnection = try_twisted_connection().connection_class |
| 101 | +EventletConnection = try_twisted_connection().connection_class |
110 | 102 |
|
111 | 103 | try: |
112 | 104 | from weakref import WeakSet |
113 | 105 | except ImportError: |
114 | 106 | from cassandra.util import WeakSet # NOQA |
115 | 107 |
|
116 | | -def _is_gevent_monkey_patched(): |
117 | | - if 'gevent.monkey' not in sys.modules: |
118 | | - return False |
119 | | - import gevent.socket |
120 | | - return socket.socket is gevent.socket.socket |
121 | | - |
122 | | -def _try_gevent_import(): |
123 | | - if _is_gevent_monkey_patched(): |
124 | | - from cassandra.io.geventreactor import GeventConnection |
125 | | - return (GeventConnection,None) |
126 | | - else: |
127 | | - return (None,None) |
128 | | - |
129 | | -def _is_eventlet_monkey_patched(): |
130 | | - if 'eventlet.patcher' not in sys.modules: |
131 | | - return False |
132 | | - try: |
133 | | - import eventlet.patcher |
134 | | - return eventlet.patcher.is_monkey_patched('socket') |
135 | | - except (ImportError, AttributeError): |
136 | | - # AttributeError was add for handling python 3.12 https://github.com/eventlet/eventlet/issues/812 |
137 | | - # TODO: remove it when eventlet issue would be fixed |
138 | | - return False |
139 | | - |
140 | | -def _try_eventlet_import(): |
141 | | - if _is_eventlet_monkey_patched(): |
142 | | - from cassandra.io.eventletreactor import EventletConnection |
143 | | - return (EventletConnection,None) |
144 | | - else: |
145 | | - return (None,None) |
146 | | - |
147 | | -def _try_libev_import(): |
148 | | - try: |
149 | | - from cassandra.io.libevreactor import LibevConnection |
150 | | - return (LibevConnection,None) |
151 | | - except DependencyException as e: |
152 | | - return (None, e) |
153 | | - |
154 | | -def _try_asyncore_import(): |
155 | | - try: |
156 | | - from cassandra.io.asyncorereactor import AsyncoreConnection |
157 | | - return (AsyncoreConnection,None) |
158 | | - except DependencyException as e: |
159 | | - return (None, e) |
160 | | - |
161 | | -def _connection_reduce_fn(val,import_fn): |
162 | | - (rv, excs) = val |
163 | | - # If we've already found a workable Connection class return immediately |
164 | | - if rv: |
165 | | - return val |
166 | | - (import_result, exc) = import_fn() |
167 | | - if exc: |
168 | | - excs.append(exc) |
169 | | - return (rv or import_result, excs) |
| 108 | +log = logging.getLogger(__name__) |
170 | 109 |
|
171 | | -conn_fns = (_try_gevent_import, _try_eventlet_import, _try_libev_import, _try_asyncore_import) |
172 | | -(conn_class, excs) = reduce(_connection_reduce_fn, conn_fns, (None,[])) |
173 | | -if excs: |
174 | | - raise DependencyException("Exception loading connection class dependencies", excs) |
| 110 | +(conn_class, excs) = get_default_connection_class() |
| 111 | +if not conn_class: |
| 112 | + raise DependencyException("Unable to load a default connection class", excs) |
175 | 113 | DefaultConnection = conn_class |
176 | 114 |
|
177 | 115 | # Forces load of utf8 encoding module to avoid deadlock that occurs |
|
0 commit comments