pyrelukko 0.2.0__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyrelukko might be problematic. Click here for more details.

pyrelukko/pyrelukko.py CHANGED
@@ -33,9 +33,18 @@ RETRY_KWARGS = [
33
33
  'delay',
34
34
  'backoff',
35
35
  'max_delay',
36
- 'exceptions'
36
+ 'exceptions',
37
37
  ]
38
38
 
39
+ OWN_KWARGS = [
40
+ 'acquire_wait_for_timeout',
41
+ 'acquire_modulo',
42
+ 'ws_ping_interval',
43
+ 'ws_ping_timeout',
44
+ 'ws_wait_for_timeout',
45
+ ]
46
+ OWN_KWARGS.extend(RETRY_KWARGS)
47
+
39
48
  logger = logging.getLogger(__name__)
40
49
 
41
50
 
@@ -66,7 +75,12 @@ class RelukkoClient:
66
75
  )
67
76
  self._setup_session(api_key, **kwargs)
68
77
  self._setup_http_adapters_retry(**kwargs)
69
- self._setup_pyrelukko_retry(**kwargs)
78
+ self.ws_wait_for_timeout = 2
79
+ self.ws_ping_interval = 60
80
+ self.ws_ping_timeout = 20
81
+ self.acquire_wait_for_timeout = 2
82
+ self.acquire_modulo = 100
83
+ self._setup_pyrelukko_kwargs(**kwargs)
70
84
 
71
85
  self.base_url = self._setup_base_url(base_url)
72
86
  self.ws_url = self._setup_ws_url(str(self.base_url))
@@ -90,10 +104,10 @@ class RelukkoClient:
90
104
  self.base_url = self._setup_base_url(base_url or self.base_url)
91
105
  self.ws_url = self._setup_ws_url(str(self.base_url))
92
106
  self._setup_ssl_ctx(**kwargs)
93
- self._setup_pyrelukko_retry(**kwargs)
107
+ self._setup_pyrelukko_kwargs(**kwargs)
94
108
 
95
- def _setup_pyrelukko_retry(self, **kwargs):
96
- for kwarg in RETRY_KWARGS:
109
+ def _setup_pyrelukko_kwargs(self, **kwargs):
110
+ for kwarg in OWN_KWARGS:
97
111
  setattr(
98
112
  self,
99
113
  kwarg,
@@ -151,7 +165,7 @@ class RelukkoClient:
151
165
 
152
166
  def _setup_ws_url(self, ws_url: str) -> Url:
153
167
  url = ws_url.replace("http", "ws", 1)
154
- return parse_url(f"{url}/deletions")
168
+ return parse_url(f"{url}/ws/broadcast")
155
169
 
156
170
  def _setup_base_url(self, base_url: Union[Url, str]) -> Url:
157
171
  if isinstance(base_url, str):
@@ -174,14 +188,17 @@ class RelukkoClient:
174
188
  additional_headers=additional_headers,
175
189
  ssl=self.ssl_ctx,
176
190
  logger=logger,
191
+ ping_interval=self.ws_ping_interval,
192
+ ping_timeout=self.ws_ping_timeout,
177
193
  ) as websocket:
178
194
  while self.ws_running.is_set():
179
195
  try:
180
- ws_message = await asyncio.wait_for(websocket.recv(), timeout=2)
196
+ ws_message = await asyncio.wait_for(
197
+ websocket.recv(), timeout=self.ws_wait_for_timeout)
181
198
  if ws_message:
182
199
  logger.debug("Received message: '%s'", ws_message)
183
200
  msg: Dict = json.loads(ws_message)
184
- if msg.get('deletions'):
201
+ if msg.get('deleted'):
185
202
  # Signal the HTTP thread to wake up
186
203
  self.message_received.set()
187
204
  except TimeoutError:
@@ -199,19 +216,25 @@ class RelukkoClient:
199
216
  """
200
217
 
201
218
  start_time = time.time()
202
-
219
+ loop_counter = 0
220
+ got_message = False
221
+ res = None
203
222
  while True:
204
223
  elapsed_time = time.time() - start_time
205
224
  if elapsed_time > max_run_time:
206
225
  self.ws_running.clear()
207
226
  _thread_store.insert(0, None)
208
227
  return
209
-
210
- res = self._make_request(
211
- url=url, method="POST", payload=payload)
228
+ # If self.message_recieved is True try to get lock ASAP!
229
+ # Otherwise only in every Xth run in case websocket broke.
230
+ if got_message or loop_counter % self.acquire_modulo == 0:
231
+ res = self._make_request(
232
+ url=url, method="POST", payload=payload)
233
+ loop_counter += 1
212
234
  if res is None:
213
235
  # Conflict 409
214
- self.message_received.wait(timeout=30)
236
+ got_message = self.message_received.wait(
237
+ timeout=self.acquire_wait_for_timeout)
215
238
  self.message_received.clear()
216
239
  else:
217
240
  _thread_store.insert(0, res)
@@ -277,7 +300,8 @@ class RelukkoClient:
277
300
 
278
301
  thread_store = []
279
302
  http_thread = threading.Thread(
280
- target=self._acquire_relukko, args=(url, max_run_time, payload, thread_store))
303
+ target=self._acquire_relukko,
304
+ args=(url, max_run_time, payload, thread_store))
281
305
  http_thread.start()
282
306
  http_thread.join()
283
307
  self.ws_listener.join()
@@ -0,0 +1,86 @@
1
+ # pylint: skip-file
2
+ """
3
+ Testcontainers to be used in PyTests, for a fixture see tests/conftest.py
4
+ (relukko_backend):
5
+
6
+ @pytest.fixture(scope="session")
7
+ def relukko_backend():
8
+ with Network() as rl_net:
9
+ with RelukkoDbContainer(net=rl_net,
10
+ image="postgres:16", hostname="relukkodb") as _db:
11
+ db_url = "postgresql://relukko:relukko@relukkodb/relukko"
12
+ with RelukkoContainer(rl_net, db_url=db_url) as backend:
13
+ relukko = RelukkoClient(
14
+ base_url=backend.get_api_url(), api_key="somekey")
15
+ yield relukko, backend
16
+ """
17
+ import socket
18
+
19
+ from testcontainers.core.network import Network
20
+ from testcontainers.core.waiting_utils import wait_container_is_ready
21
+ from testcontainers.generic import ServerContainer
22
+ from testcontainers.postgres import PostgresContainer
23
+
24
+
25
+ class RelukkoContainer(ServerContainer):
26
+ def __init__(self, net: Network,
27
+ image="registry.gitlab.com/relukko/relukko:latest", db_url=None):
28
+ self.db_url = db_url
29
+ self.net = net
30
+ super(RelukkoContainer, self).__init__(image=image, port=3000)
31
+
32
+ def _configure(self):
33
+ self.with_env("DATABASE_URL", self.db_url)
34
+ self.with_env("RELUKKO_API_KEY", "somekey")
35
+ self.with_env("RELUKKO_USER", "relukko")
36
+ self.with_env("RELUKKO_PASSWORD", "relukko")
37
+ self.with_env("RELUKKO_BIND_ADDR", "0.0.0.0")
38
+ self.with_network(self.net)
39
+
40
+ def get_api_url(self) -> str:
41
+ return f"http://localhost:{self.get_exposed_port(3000)}"
42
+
43
+ def _create_connection_url(self) -> str:
44
+ return f"{self.get_api_url()}/healthchecker"
45
+
46
+
47
+ class RelukkoDbContainer(PostgresContainer):
48
+ def __init__(
49
+ self, net: Network,
50
+ image: str = "postgres:latest",
51
+ port: int = 5432,
52
+ username: str | None = None,
53
+ password: str | None = None,
54
+ dbname: str | None = None,
55
+ driver: str | None = "psycopg2",
56
+ **kwargs) -> None:
57
+ self.net = net
58
+ super().__init__(image, port, username, password, dbname, driver, **kwargs)
59
+
60
+ def _configure(self) -> None:
61
+ self.with_env("POSTGRES_USER", "relukko")
62
+ self.with_env("POSTGRES_PASSWORD", "relukko")
63
+ self.with_env("POSTGRES_DB", "relukko")
64
+ self.with_network(self.net)
65
+
66
+ @wait_container_is_ready()
67
+ def _connect(self) -> None:
68
+ packet = bytes([
69
+ 0x00, 0x00, 0x00, 0x52, 0x00, 0x03, 0x00, 0x00,
70
+ 0x75, 0x73, 0x65, 0x72, 0x00, 0x72, 0x65, 0x6c,
71
+ 0x75, 0x6b, 0x6b, 0x6f, 0x00, 0x64, 0x61, 0x74,
72
+ 0x61, 0x62, 0x61, 0x73, 0x65, 0x00, 0x72, 0x65,
73
+ 0x6c, 0x75, 0x6b, 0x6b, 0x6f, 0x00, 0x61, 0x70,
74
+ 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
75
+ 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x70,
76
+ 0x73, 0x71, 0x6c, 0x00, 0x63, 0x6c, 0x69, 0x65,
77
+ 0x6e, 0x74, 0x5f, 0x65, 0x6e, 0x63, 0x6f, 0x64,
78
+ 0x69, 0x6e, 0x67, 0x00, 0x55, 0x54, 0x46, 0x38,
79
+ 0x00, 0x00
80
+ ])
81
+ port = self.get_exposed_port(self.port)
82
+ with socket.create_connection(("localhost", port)) as sock:
83
+ sock.send(packet)
84
+ buf = sock.recv(40)
85
+ if len(buf) == 0 and "SCRAM-SHA" not in buf:
86
+ raise ConnectionError
pyrelukko/version.py CHANGED
@@ -1,2 +1,2 @@
1
1
  # pylint: disable=all
2
- __version__ = "0.2.0"
2
+ __version__ = "0.4.0"
@@ -1,16 +1,21 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pyrelukko
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Relukko client.
5
5
  Author-email: Reto Zingg <g.d0b3rm4n@gmail.com>
6
- Requires-Python: >=3.12
6
+ Requires-Python: >=3.10
7
7
  Description-Content-Type: text/markdown
8
8
  Classifier: Development Status :: 3 - Alpha
9
9
  Classifier: Intended Audience :: Developers
10
10
  Classifier: License :: OSI Approved :: MIT License
11
11
  Classifier: Topic :: Internet :: WWW/HTTP
12
12
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
13
17
  Requires-Dist: requests >=2.32.3
18
+ Requires-Dist: websockets >= 13.1
14
19
  Project-URL: Homepage, https://gitlab.com/relukko/pyrelukko
15
20
  Project-URL: Issues, https://gitlab.com/relukko/pyrelukko/-/issues
16
21
 
@@ -0,0 +1,9 @@
1
+ pyrelukko/__init__.py,sha256=n-mTJV7AOfr4bljtDxok3E3gXIBRk-kFa2_Ql3S8Ht0,61
2
+ pyrelukko/pyrelukko.py,sha256=8ea0t-qoQqoP7K1BZ6iaDk2ZSsBxuY3J_Z24P8PdJdY,12335
3
+ pyrelukko/retry.py,sha256=XeCc0trPhAggdEnu2nwtpFderlHaDQFTXH_O1dLeiGQ,1587
4
+ pyrelukko/testcontainers.py,sha256=mSUfnyBFVmt9N9LoI_EwXG4EomY8pQCvHzff2ESio-8,3354
5
+ pyrelukko/version.py,sha256=pIAXZHS0AKfM9TgvQfXXvE9frJxGVMYpEYyLdg5iaGQ,44
6
+ pyrelukko-0.4.0.dist-info/LICENSE,sha256=aEI4dThWmRUiEPch0-KaZQmHZgTyuz88Edmp25H6tAw,1089
7
+ pyrelukko-0.4.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
8
+ pyrelukko-0.4.0.dist-info/METADATA,sha256=DwdoXKmelYcvpI7OFwvl2tRvPdzXIhFiM5VxlptyBxw,939
9
+ pyrelukko-0.4.0.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- pyrelukko/__init__.py,sha256=n-mTJV7AOfr4bljtDxok3E3gXIBRk-kFa2_Ql3S8Ht0,61
2
- pyrelukko/pyrelukko.py,sha256=5bDqueUMxbMSxbuSOE9L7aHpAP3bax4DHQqATeuHs1s,11435
3
- pyrelukko/retry.py,sha256=XeCc0trPhAggdEnu2nwtpFderlHaDQFTXH_O1dLeiGQ,1587
4
- pyrelukko/version.py,sha256=5ajVDlNTAW-Ykqh9HwJkug5LWeNtanZQhZox4hNtxio,44
5
- pyrelukko-0.2.0.dist-info/LICENSE,sha256=aEI4dThWmRUiEPch0-KaZQmHZgTyuz88Edmp25H6tAw,1089
6
- pyrelukko-0.2.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
7
- pyrelukko-0.2.0.dist-info/METADATA,sha256=0tOto4sHXUD_sFAfTvC6EhV2l4NER-gNyYE66ori8TQ,701
8
- pyrelukko-0.2.0.dist-info/RECORD,,