This repository was archived by the owner on May 5, 2022. It is now read-only.
forked from launchdarkly/python-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
115 lines (96 loc) · 3.24 KB
/
Copy path__init__.py
File metadata and controls
115 lines (96 loc) · 3.24 KB
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
import logging
from ldclient.rwlock import ReadWriteLock
from ldclient.version import VERSION
from .client import *
from .util import log
__version__ = VERSION
__LONG_SCALE__ = float(0xFFFFFFFFFFFFFFF)
__BUILTINS__ = ["key", "ip", "country", "email",
"firstName", "lastName", "avatar", "name", "anonymous"]
"""Settings."""
start_wait = 5
__client = None
__config = Config()
__lock = ReadWriteLock()
# 2 Use Cases:
# 1. Initial setup: sets the config for the uninitialized client
# 2. Allows on-the-fly changing of the config. When this function is called after the client has been initialized
# the client will get re-initialized with the new config. In order for this to work, the return value of
# ldclient.get() should never be assigned
def set_config(config):
global __config
global __client
global __lock
try:
__lock.lock()
if __client:
log.info("Reinitializing LaunchDarkly Client " + version.VERSION + " with new config")
new_client = LDClient(config=config, start_wait=start_wait)
old_client = __client
__client = new_client
old_client.close()
finally:
__config = config
__lock.unlock()
# 2 Use Cases:
# 1. Initial setup: sets the sdk key for the uninitialized client
# 2. Allows on-the-fly changing of the sdk key. When this function is called after the client has been initialized
# the client will get re-initialized with the new sdk key. In order for this to work, the return value of
# ldclient.get() should never be assigned
def set_sdk_key(sdk_key):
global __config
global __client
global __lock
sdk_key_changed = False
try:
__lock.rlock()
if sdk_key is __config.sdk_key:
log.info("New sdk_key is the same as the existing one. doing nothing.")
else:
sdk_key_changed = True
finally:
__lock.runlock()
if sdk_key_changed:
try:
__lock.lock()
__config = __config.copy_with_new_sdk_key(new_sdk_key=sdk_key)
if __client:
log.info("Reinitializing LaunchDarkly Client " + version.VERSION + " with new sdk key")
new_client = LDClient(config=__config, start_wait=start_wait)
old_client = __client
__client = new_client
old_client.close()
finally:
__lock.unlock()
def get():
global __config
global __client
global __lock
try:
__lock.rlock()
if __client:
return __client
finally:
__lock.runlock()
try:
__lock.lock()
if not __client:
log.info("Initializing LaunchDarkly Client " + version.VERSION)
__client = LDClient(config=__config, start_wait=start_wait)
return __client
finally:
__lock.unlock()
# Add a NullHandler for Python < 2.7 compatibility
class NullHandler(logging.Handler):
def emit(self, record):
pass
if not log.handlers:
log.addHandler(NullHandler())
try:
# noinspection PyUnresolvedReferences
unicode
except NameError:
__BASE_TYPES__ = (str, float, int, bool)
else:
# noinspection PyUnresolvedReferences
__BASE_TYPES__ = (str, float, int, bool, unicode)