-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig.py
171 lines (146 loc) · 6.11 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Default settings for the Split.IO SDK Python client."""
import os.path
import logging
from enum import Enum
from splitio.engine.impressions import ImpressionsMode
from splitio.client.input_validator import validate_flag_sets
_LOGGER = logging.getLogger(__name__)
DEFAULT_DATA_SAMPLING = 1
class AuthenticateScheme(Enum):
"""Authentication Scheme."""
NONE = 'NONE'
KERBEROS = 'KERBEROS'
DEFAULT_CONFIG = {
'operationMode': 'standalone',
'connectionTimeout': 1500,
'streamingEnabled': True,
'featuresRefreshRate': 30,
'segmentsRefreshRate': 30,
'metricsRefreshRate': 3600,
'impressionsRefreshRate': 5 * 60,
'impressionsBulkSize': 5000,
'impressionsQueueSize': 10000,
'eventsPushRate': 10,
'eventsBulkSize': 5000,
'eventsQueueSize': 10000,
'labelsEnabled': True,
'IPAddressesEnabled': True,
'impressionsMode': 'OPTIMIZED',
'impressionListener': None,
'redisLocalCacheEnabled': True,
'redisLocalCacheTTL': 5,
'redisHost': 'localhost',
'redisPort': 6379,
'redisDb': 0,
'redisUsername': None,
'redisPassword': None,
'redisSocketTimeout': None,
'redisSocketConnectTimeout': None,
'redisSocketKeepalive': None,
'redisSocketKeepaliveOptions': None,
'redisConnectionPool': None,
'redisUnixSocketPath': None,
'redisEncoding': 'utf-8',
'redisEncodingErrors': 'strict',
'redisErrors': None,
'redisDecodeResponses': True,
'redisRetryOnTimeout': False,
'redisSsl': False,
'redisSslKeyfile': None,
'redisSslCertfile': None,
'redisSslCertReqs': None,
'redisSslCaCerts': None,
'redisMaxConnections': None,
'machineName': None,
'machineIp': None,
'splitFile': os.path.join(os.path.expanduser('~'), '.split'),
'segmentDirectory': os.path.expanduser('~'),
'localhostRefreshEnabled': False,
'preforkedInitialization': False,
'dataSampling': DEFAULT_DATA_SAMPLING,
'storageWrapper': None,
'storagePrefix': None,
'storageType': None,
'flagSetsFilter': None,
'httpAuthenticateScheme': AuthenticateScheme.NONE,
'kerberosPrincipalUser': None,
'kerberosPrincipalPassword': None
}
def _parse_operation_mode(sdk_key, config):
"""
Process incoming config to determine operation mode and storage type
:param config: user supplied config
:type config: dict
:returns: operation mode and storage type
:rtype: Tuple (str, str)
"""
if sdk_key == 'localhost':
_LOGGER.debug('Using Localhost operation mode')
return 'localhost', 'localhost'
if 'redisHost' in config or 'redisSentinels' in config:
_LOGGER.debug('Using Redis storage operation mode')
return 'consumer', 'redis'
if config.get('storageType') is not None:
if config.get('storageType').lower() == 'pluggable':
_LOGGER.debug('Using Pluggable storage operation mode')
return 'consumer', 'pluggable'
_LOGGER.warning('You passed an invalid storageType, acceptable value is '
'`pluggable`. Defaulting storage to In-Memory mode.')
_LOGGER.debug('Using In-Memory operation mode')
return 'standalone', 'memory'
def _sanitize_impressions_mode(storage_type, mode, refresh_rate=None):
"""
Check supplied impressions mode and adjust refresh rate.
:param config: default + supplied config
:type config: dict
:returns: config with sanitized impressions mode & refresh rate
:rtype: config
"""
if not isinstance(mode, ImpressionsMode):
try:
mode = ImpressionsMode(mode.upper())
except (ValueError, AttributeError):
mode = ImpressionsMode.OPTIMIZED
_LOGGER.warning('You passed an invalid impressionsMode, impressionsMode should be ' \
'one of the following values: `debug`, `none` or `optimized`. '
' Defaulting to `optimized` mode.')
if mode == ImpressionsMode.DEBUG:
refresh_rate = max(1, refresh_rate) if refresh_rate is not None else 60
else:
refresh_rate = max(60, refresh_rate) if refresh_rate is not None else 5 * 60
return mode, refresh_rate
def sanitize(sdk_key, config):
"""
Look for inconsistencies or ill-formed configs and tune it accordingly.
:param sdk_key: sdk key
:type sdk_key: str
:param config: DEFAULT + user supplied config
:type config: dict
:returns: sanitized config
:rtype: dict
"""
config['operationMode'], config['storageType'] = _parse_operation_mode(sdk_key, config)
processed = DEFAULT_CONFIG.copy()
processed.update(config)
imp_mode, imp_rate = _sanitize_impressions_mode(config['storageType'], config.get('impressionsMode'),
config.get('impressionsRefreshRate'))
processed['impressionsMode'] = imp_mode
processed['impressionsRefreshRate'] = imp_rate
if processed['metricsRefreshRate'] < 60:
_LOGGER.warning('metricRefreshRate parameter minimum value is 60 seconds, defaulting to 3600 seconds.')
processed['metricsRefreshRate'] = 3600
if config['operationMode'] == 'consumer' and config.get('flagSetsFilter') is not None:
processed['flagSetsFilter'] = None
_LOGGER.warning('config: FlagSets filter is not applicable for Consumer modes where the SDK does keep rollout data in sync. FlagSet filter was discarded.')
else:
processed['flagSetsFilter'] = sorted(validate_flag_sets(processed['flagSetsFilter'], 'SDK Config')) if processed['flagSetsFilter'] is not None else None
if config.get('httpAuthenticateScheme') is not None:
try:
authenticate_scheme = AuthenticateScheme(config['httpAuthenticateScheme'].upper())
except (ValueError, AttributeError):
authenticate_scheme = AuthenticateScheme.NONE
_LOGGER.warning('You passed an invalid HttpAuthenticationScheme, HttpAuthenticationScheme should be ' \
'one of the following values: `none` or `kerberos`. '
' Defaulting to `none` mode.')
processed["httpAuthenticateScheme"] = authenticate_scheme
return processed