Skip to content

Commit befd8b9

Browse files
Introduce ProtocolFeatures
Introduces the ProtocolFeatures class which contains information that affects how the CQL protocol should be serialized and deserialized. Currently, it only supports the Scylla-specific SCYLLA_RATE_LIMIT_ERROR extension.
1 parent 7c9df85 commit befd8b9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

cassandra/protocol_features.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import logging
2+
3+
log = logging.getLogger(__name__)
4+
5+
6+
RATE_LIMIT_ERROR_EXTENSION = "SCYLLA_RATE_LIMIT_ERROR"
7+
8+
class ProtocolFeatures(object):
9+
rate_limit_error = None
10+
11+
def __init__(self, rate_limit_error=None):
12+
self.rate_limit_error = rate_limit_error
13+
14+
@staticmethod
15+
def parse_from_supported(supported):
16+
return ProtocolFeatures(rate_limit_error = ProtocolFeatures.maybe_parse_rate_limit_error(supported))
17+
18+
@staticmethod
19+
def maybe_parse_rate_limit_error(supported):
20+
vals = supported.get(RATE_LIMIT_ERROR_EXTENSION)
21+
if vals is not None:
22+
code_str = ProtocolFeatures.get_cql_extension_field(vals, "ERROR_CODE")
23+
return int(code_str)
24+
25+
# Looks up a field which starts with `key=` and returns the rest
26+
@staticmethod
27+
def get_cql_extension_field(vals, key):
28+
for v in vals:
29+
stripped_v = v.strip()
30+
if stripped_v.startswith(key) and stripped_v[len(key)] == '=':
31+
result = stripped_v[len(key) + 1:]
32+
return result
33+
return None
34+
35+
def add_startup_options(self, options):
36+
if self.rate_limit_error is not None:
37+
options[RATE_LIMIT_ERROR_EXTENSION] = ""
38+

0 commit comments

Comments
 (0)