Skip to content

Commit 534ee79

Browse files
committed
1- Added backoff to sync_all that fixes the timeout when invalid api key is used
2- Removed api key validation
1 parent d5502ba commit 534ee79

File tree

5 files changed

+12
-65
lines changed

5 files changed

+12
-65
lines changed

splitio/client/factory.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ def _build_in_memory_factory(api_key, cfg, sdk_url=None, events_url=None, # pyl
316316
'telemetry': TelemetryAPI(http_client, api_key, sdk_metadata),
317317
}
318318

319-
if not input_validator.validate_apikey_type(apis['segments']):
320-
return None
321-
322319
storages = {
323320
'splits': InMemorySplitStorage(),
324321
'segments': InMemorySegmentStorage(),

splitio/client/input_validator.py

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -446,32 +446,7 @@ def validate_attributes(attributes, method_name):
446446
class _ApiLogFilter(logging.Filter): # pylint: disable=too-few-public-methods
447447
def filter(self, record):
448448
return record.name not in ('SegmentsAPI', 'HttpClient')
449-
450-
451-
def validate_apikey_type(segment_api):
452-
"""
453-
Try to guess if the apikey is of browser type and let the user know.
454-
455-
:param segment_api: Segments API client.
456-
:type segment_api: splitio.api.segments.SegmentsAPI
457-
"""
458-
api_messages_filter = _ApiLogFilter()
459-
_logger = logging.getLogger('splitio.api.segments')
460-
try:
461-
_logger.addFilter(api_messages_filter) # pylint: disable=protected-access
462-
segment_api.fetch_segment('__SOME_INVALID_SEGMENT__', -1, FetchOptions())
463-
except APIException as exc:
464-
if exc.status_code == 403:
465-
_LOGGER.error('factory instantiation: you passed a browser type '
466-
+ 'api_key, please grab an api key from the Split '
467-
+ 'console that is of type sdk')
468-
return False
469-
finally:
470-
_logger.removeFilter(api_messages_filter) # pylint: disable=protected-access
471-
472-
# True doesn't mean that the APIKEY is right, only that it's not of type "browser"
473-
return True
474-
449+
475450

476451
def validate_factory_instantiation(apikey):
477452
"""

splitio/sync/synchronizer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import abc
44
import logging
55
import threading
6+
import time
67

78
from splitio.api import APIException
9+
from splitio.util.backoff import Backoff
810

911

1012
_LOGGER = logging.getLogger(__name__)
@@ -212,6 +214,9 @@ def shutdown(self, blocking):
212214
class Synchronizer(BaseSynchronizer):
213215
"""Synchronizer."""
214216

217+
_ON_DEMAND_FETCH_BACKOFF_BASE = 10 # backoff base starting at 10 seconds
218+
_ON_DEMAND_FETCH_BACKOFF_MAX_WAIT = 30 # don't sleep for more than 1 minute
219+
215220
def __init__(self, split_synchronizers, split_tasks):
216221
"""
217222
Class constructor.
@@ -221,6 +226,9 @@ def __init__(self, split_synchronizers, split_tasks):
221226
:param split_tasks: tasks for starting/stopping tasks
222227
:type split_tasks: splitio.sync.synchronizer.SplitTasks
223228
"""
229+
self._backoff = Backoff(
230+
self._ON_DEMAND_FETCH_BACKOFF_BASE,
231+
self._ON_DEMAND_FETCH_BACKOFF_MAX_WAIT)
224232
self._split_synchronizers = split_synchronizers
225233
self._split_tasks = split_tasks
226234
self._periodic_data_recording_tasks = [
@@ -291,6 +299,8 @@ def sync_all(self):
291299
try:
292300
if not self.synchronize_splits(None, False):
293301
attempts -= 1
302+
how_long = self._backoff.get()
303+
time.sleep(how_long)
294304
continue
295305

296306
# Only retrying splits, since segments may trigger too many calls.

tests/integration/test_client_e2e.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import threading
6+
import pytest
67

78
from redis import StrictRedis
89

tests/integration/test_streaming_e2e.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ def test_happiness(self):
127127
'[?occupancy=metrics.publishers]control_sec'])
128128
assert qs['v'][0] == '1.1'
129129

130-
# Initial apikey validation
131-
req = split_backend_requests.get()
132-
assert req.method == 'GET'
133-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
134-
assert req.headers['authorization'] == 'Bearer some_apikey'
135-
136130
# Initial splits fetch
137131
req = split_backend_requests.get()
138132
assert req.method == 'GET'
@@ -334,12 +328,6 @@ def test_occupancy_flicker(self):
334328
'[?occupancy=metrics.publishers]control_sec'])
335329
assert qs['v'][0] == '1.1'
336330

337-
# Initial apikey validation
338-
req = split_backend_requests.get()
339-
assert req.method == 'GET'
340-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
341-
assert req.headers['authorization'] == 'Bearer some_apikey'
342-
343331
# Initial splits fetch
344332
req = split_backend_requests.get()
345333
assert req.method == 'GET'
@@ -514,12 +502,6 @@ def test_start_without_occupancy(self):
514502
'[?occupancy=metrics.publishers]control_sec'])
515503
assert qs['v'][0] == '1.1'
516504

517-
# Initial apikey validation
518-
req = split_backend_requests.get()
519-
assert req.method == 'GET'
520-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
521-
assert req.headers['authorization'] == 'Bearer some_apikey'
522-
523505
# Initial splits fetch
524506
req = split_backend_requests.get()
525507
assert req.method == 'GET'
@@ -704,12 +686,6 @@ def test_streaming_status_changes(self):
704686
'[?occupancy=metrics.publishers]control_sec'])
705687
assert qs['v'][0] == '1.1'
706688

707-
# Initial apikey validation
708-
req = split_backend_requests.get()
709-
assert req.method == 'GET'
710-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
711-
assert req.headers['authorization'] == 'Bearer some_apikey'
712-
713689
# Initial splits fetch
714690
req = split_backend_requests.get()
715691
assert req.method == 'GET'
@@ -931,12 +907,6 @@ def test_server_closes_connection(self):
931907
'[?occupancy=metrics.publishers]control_sec'])
932908
assert qs['v'][0] == '1.1'
933909

934-
# Initial apikey validation
935-
req = split_backend_requests.get()
936-
assert req.method == 'GET'
937-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
938-
assert req.headers['authorization'] == 'Bearer some_apikey'
939-
940910
# Initial splits fetch
941911
req = split_backend_requests.get()
942912
assert req.method == 'GET'
@@ -1168,12 +1138,6 @@ def test_ably_errors_handling(self):
11681138
'[?occupancy=metrics.publishers]control_sec'])
11691139
assert qs['v'][0] == '1.1'
11701140

1171-
# Initial apikey validation
1172-
req = split_backend_requests.get()
1173-
assert req.method == 'GET'
1174-
assert req.path == '/api/segmentChanges/__SOME_INVALID_SEGMENT__?since=-1'
1175-
assert req.headers['authorization'] == 'Bearer some_apikey'
1176-
11771141
# Initial splits fetch
11781142
req = split_backend_requests.get()
11791143
assert req.method == 'GET'

0 commit comments

Comments
 (0)