Skip to content

Commit

Permalink
fix: #47 multi-threaded problems
Browse files Browse the repository at this point in the history
Signed-off-by: WolfBolin <wolfbolin@foxmail.com>
  • Loading branch information
wolfbolin committed Aug 1, 2024
1 parent 2a8aeee commit 19388dc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 19 deletions.
7 changes: 3 additions & 4 deletions opengemini_client/client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import gzip
import io
import itertools
from abc import ABC
from http import HTTPStatus
from typing import List
Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(self, config: Config):
self.session = requests.Session()
protocol = "https://" if config.tls_enabled else "http://"
self.endpoints = [f"{protocol}{addr.host}:{addr.port}" for addr in config.address]
self.pre_idx = AtomicInt(-1)
self.endpoints_iter = itertools.cycle(self.endpoints)

def close(self):
self.session.close()
Expand All @@ -84,9 +85,7 @@ def __exit__(self, _exc_type, _exc_val, _exc_tb):
self.session.close()

def get_server_url(self):
self.pre_idx.increment()
idx = int(self.pre_idx.get_value()) % len(self.endpoints)
return self.endpoints[idx]
return next(self.endpoints_iter)

def update_headers(self, method, url_path, headers=None) -> dict:
if headers is None:
Expand Down
15 changes: 0 additions & 15 deletions opengemini_client/utils.py
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
import threading


class AtomicInt:

def __init__(self, value=0):
self._value = value
self._lock = threading.Lock()

def increment(self):
with self._lock:
self._value += 1

def get_value(self):
with self._lock:
return self._value

0 comments on commit 19388dc

Please sign in to comment.