Commit 76bb9cab by Karsa Zoltán István

linter

parent b53e8296
...@@ -9,7 +9,7 @@ from sredis.sredis import ( ...@@ -9,7 +9,7 @@ from sredis.sredis import (
set_token, set_token,
create_superpuser, create_superpuser,
get_rtokens, get_rtokens,
reset reset,
) )
import logging import logging
import requests import requests
...@@ -60,7 +60,9 @@ def proxy_get( ...@@ -60,7 +60,9 @@ def proxy_get(
def proxy_delete( def proxy_delete(
server_path: str = "/", username=Depends(get_current_user), datacenter: str = "" server_path: str = "/", username=Depends(get_current_user), datacenter: str = ""
): ):
return proxy_datacenters(server_path, username, method="DELETE", datacenter=datacenter) return proxy_datacenters(
server_path, username, method="DELETE", datacenter=datacenter
)
@app.post("/lb/rr/{server_path:path}") @app.post("/lb/rr/{server_path:path}")
...@@ -89,6 +91,7 @@ def set_tokens(tokens: List[Token] = None, username=Depends(get_current_user)): ...@@ -89,6 +91,7 @@ def set_tokens(tokens: List[Token] = None, username=Depends(get_current_user)):
set_token(username, str(token.datacenter), str(token.token)) set_token(username, str(token.datacenter), str(token.token))
return tokens return tokens
@app.get("/tokens/") @app.get("/tokens/")
def get_tokens(username=Depends(get_current_user)): def get_tokens(username=Depends(get_current_user)):
return get_rtokens(username=username) return get_rtokens(username=username)
...@@ -29,6 +29,10 @@ help = "Start the microservice on port 6973" ...@@ -29,6 +29,10 @@ help = "Start the microservice on port 6973"
shell = "poetry run uvicorn main:app --host 0.0.0.0 --port 8080 --workers 8 --log-level critical" shell = "poetry run uvicorn main:app --host 0.0.0.0 --port 8080 --workers 8 --log-level critical"
help = "Start the prod deployment" help = "Start the prod deployment"
[tool.poe.tasks.stress]
shell = "poetry run uvicorn main:app --host 0.0.0.0 --port 6973 --workers 4"
help = "Start the prod deployment"
[tool.poe.tasks.lint] [tool.poe.tasks.lint]
shell = "poetry run black . && poetry run ruff --fix . " shell = "poetry run black . && poetry run ruff --fix . "
help = "Lint the most important parts of the microservice with black" help = "Lint the most important parts of the microservice with black"
......
...@@ -9,12 +9,13 @@ import random ...@@ -9,12 +9,13 @@ import random
r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True) r = redis.Redis(host="localhost", port=6379, db=0, decode_responses=True)
def reset(): def reset():
print("Reset OK") print("Reset OK")
r.set("datacenters_cnt", 1) r.set("datacenters_cnt", 1)
all_keys = r.hgetall("datacenters_hash") all_keys = r.hgetall("datacenters_hash")
if all_keys.keys(): if all_keys.keys():
r.hdel("datacenters_hash",*list(all_keys.keys())) r.hdel("datacenters_hash", *list(all_keys.keys()))
r.delete("rrlist") r.delete("rrlist")
pipe = r.pipeline() pipe = r.pipeline()
...@@ -22,6 +23,7 @@ def reset(): ...@@ -22,6 +23,7 @@ def reset():
pipe.rpush("rrlist", dc) pipe.rpush("rrlist", dc)
pipe.execute() pipe.execute()
def add_datacenter(datacenter: str): def add_datacenter(datacenter: str):
cnt = int(r.get("datacenters_cnt")) cnt = int(r.get("datacenters_cnt"))
r.hset("datacenters_hash", cnt, datacenter) r.hset("datacenters_hash", cnt, datacenter)
...@@ -29,6 +31,7 @@ def add_datacenter(datacenter: str): ...@@ -29,6 +31,7 @@ def add_datacenter(datacenter: str):
r.set("roundrobin_cnt", 1) r.set("roundrobin_cnt", 1)
r.rpush("rrlist", datacenter) r.rpush("rrlist", datacenter)
def rr_get(): def rr_get():
rr = r.rpoplpush("rrlist", "rrlist") rr = r.rpoplpush("rrlist", "rrlist")
return rr return rr
...@@ -43,19 +46,22 @@ def wr_get(centers: DataCenterSchema): ...@@ -43,19 +46,22 @@ def wr_get(centers: DataCenterSchema):
def set_token(username: str, datacenter: str, token: str): def set_token(username: str, datacenter: str, token: str):
r.hset(f"tokens:{username}", datacenter, token) r.hset(f"tokens:{username}", datacenter, token)
def get_rtoken(username: str, datacenter: str): def get_rtoken(username: str, datacenter: str):
return r.hget(f"tokens:{username}", datacenter) return r.hget(f"tokens:{username}", datacenter)
def get_rtokens(username: str): def get_rtokens(username: str):
center_ids = list(r.hgetall("datacenters_hash").keys()) center_ids = list(r.hgetall("datacenters_hash").keys())
tokens = [] tokens = []
for id in center_ids: for id in center_ids:
token = { } token = {}
token["datacenter"] = r.hget("datacenters_hash", id) token["datacenter"] = r.hget("datacenters_hash", id)
token["token"] = get_rtoken(username=username, datacenter=token["datacenter"]) token["token"] = get_rtoken(username=username, datacenter=token["datacenter"])
tokens.insert(0, token) tokens.insert(0, token)
return tokens return tokens
def get_datacenter_token(username: str, datacenter: str): def get_datacenter_token(username: str, datacenter: str):
return str(r.hget(f"tokens:{username}", datacenter)) return str(r.hget(f"tokens:{username}", datacenter))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment