Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
balancer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1bbd6872
authored
Mar 23, 2023
by
Karsa Zoltán István
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rr endpoint
parent
5ea6c94e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
56 additions
and
29 deletions
+56
-29
balancer/util.py
+12
-6
core/models.py
+2
-2
main.py
+22
-7
sredis/models.py
+2
-2
sredis/sredis.py
+10
-5
stresstest/main.py
+8
-7
No files found.
balancer/util.py
View file @
1bbd6872
...
@@ -5,22 +5,28 @@ import requests
...
@@ -5,22 +5,28 @@ import requests
import
json
import
json
def
proxy_datacenters
(
serverpath
:
str
,
username
,
method
=
"GET"
,
balancer_fun
=
rr_get
):
def
proxy_datacenters
(
server
=
balancer_fun
()
serverpath
:
str
,
username
,
method
=
"GET"
,
body
=
""
,
balancer_fun
=
rr_get
):
if
"datacenter"
in
body
:
server
=
body
[
"datacenter"
]
else
:
server
=
balancer_fun
()
token
=
get_datacenter_token
(
username
,
server
)
token
=
get_datacenter_token
(
username
,
server
)
url
=
f
"{server}/{serverpath}"
url
=
f
"{server}/{serverpath}"
t_resp
=
requests
.
request
(
t_resp
=
requests
.
request
(
json
=
body
,
method
=
method
,
method
=
method
,
url
=
url
,
url
=
url
,
allow_redirects
=
False
,
allow_redirects
=
False
,
verify
=
False
,
verify
=
False
,
headers
=
{
"Authorization"
:
token
},
headers
=
{
"Authorization"
:
token
},
)
)
if
t_resp
.
status_code
/
100
!=
2
:
if
int
(
t_resp
.
status_code
/
100
)
!=
2
:
raise
HTTPException
(
raise
HTTPException
(
status_code
=
t_resp
.
status_code
,
detail
=
"Remote server error"
status_code
=
t_resp
.
status_code
,
detail
=
"Remote server error"
)
)
response
=
ORJSONResponse
(
obj
=
json
.
loads
(
t_resp
.
content
)
json
.
loads
(
t_resp
.
content
),
status_code
=
t_resp
.
status_code
obj
[
0
][
"datacenter"
]
=
server
)
response
=
ORJSONResponse
(
obj
[
0
],
status_code
=
t_resp
.
status_code
)
return
response
return
response
core/models.py
View file @
1bbd6872
...
@@ -19,8 +19,9 @@ class UserLoginSchema(BaseModel):
...
@@ -19,8 +19,9 @@ class UserLoginSchema(BaseModel):
class
Config
:
class
Config
:
schema_extra
=
{
"example"
:
{
"username"
:
"user"
,
"password"
:
"weakpassword"
}}
schema_extra
=
{
"example"
:
{
"username"
:
"user"
,
"password"
:
"weakpassword"
}}
class
DataCenterSchema
(
BaseModel
):
class
DataCenterSchema
(
BaseModel
):
name
:
str
=
Field
(
min_length
=
3
,
max_length
=
50
)
name
:
str
=
Field
(
min_length
=
3
,
max_length
=
50
)
url
:
str
url
:
str
active
:
bool
=
False
active
:
bool
=
False
weight
:
float
=
1.0
weight
:
float
=
1.0
\ No newline at end of file
main.py
View file @
1bbd6872
...
@@ -2,7 +2,13 @@ from fastapi import FastAPI, Response, Body, Depends
...
@@ -2,7 +2,13 @@ from fastapi import FastAPI, Response, Body, Depends
import
os
import
os
from
typing
import
List
from
typing
import
List
from
balancer.util
import
proxy_datacenters
from
balancer.util
import
proxy_datacenters
from
sredis.sredis
import
check_user
,
create_puser
,
add_datacenter
,
set_token
,
create_superpuser
from
sredis.sredis
import
(
check_user
,
create_puser
,
add_datacenter
,
set_token
,
create_superpuser
,
)
import
logging
import
logging
import
requests
import
requests
from
core.models
import
User
,
Token
,
UserLoginSchema
,
DataCenterSchema
from
core.models
import
User
,
Token
,
UserLoginSchema
,
DataCenterSchema
...
@@ -22,7 +28,7 @@ add_datacenter("https://kappa1.fured.cloud.bme.hu")
...
@@ -22,7 +28,7 @@ add_datacenter("https://kappa1.fured.cloud.bme.hu")
add_datacenter
(
"https://kappa2.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa2.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa3.fured.cloud.bme.hu"
)
add_datacenter
(
"https://kappa3.fured.cloud.bme.hu"
)
create_superpuser
(
create_superpuser
(
User
(
username
=
'admin'
,
email
=
'example@domain.com'
,
password
=
os
.
getenv
(
'ADMIN_PASS'
))
User
(
username
=
"admin"
,
email
=
"example@domain.com"
,
password
=
os
.
getenv
(
"ADMIN_PASS"
))
)
)
...
@@ -40,13 +46,22 @@ async def user_login(user: UserLoginSchema = Body(...)):
...
@@ -40,13 +46,22 @@ async def user_login(user: UserLoginSchema = Body(...)):
@app.get
(
"/lb/{server_path:path}"
)
@app.get
(
"/lb/{server_path:path}"
)
def
proxy_get
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)):
def
proxy_get
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
),
body
=
Body
()):
return
proxy_datacenters
(
server_path
,
username
)
return
proxy_datacenters
(
server_path
,
username
,
body
=
body
)
@app.post
(
"/lb/rr/{server_path:path}"
)
def
proxy_post_rr
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
),
body
=
Body
()
):
return
proxy_datacenters
(
server_path
,
username
,
body
=
body
,
method
=
"POST"
)
@app.post
(
"/lb/{server_path:path}"
)
@app.post
(
"/lb/ff/{server_path:path}"
)
def
proxy_post
(
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
)):
def
proxy_post_ff
(
return
proxy_datacenters
(
server_path
,
username
,
method
=
"POST"
)
server_path
:
str
=
"/"
,
username
=
Depends
(
get_current_user
),
body
=
Body
()
):
return
proxy_datacenters
(
server_path
,
username
,
body
=
body
,
method
=
"POST"
)
@app.post
(
"/add_datacenter/"
)
@app.post
(
"/add_datacenter/"
)
...
...
sredis/models.py
View file @
1bbd6872
...
@@ -8,7 +8,8 @@ class PUser(HashModel):
...
@@ -8,7 +8,8 @@ class PUser(HashModel):
password
:
str
password
:
str
admin
:
int
=
0
admin
:
int
=
0
class
DataCenter
(
HashModel
):
class
DataCenter
(
HashModel
):
name
:
str
=
Field
(
index
=
True
)
name
:
str
=
Field
(
index
=
True
)
url
:
str
=
Field
(
index
=
True
)
url
:
str
=
Field
(
index
=
True
)
active
:
bool
=
Field
(
index
=
True
)
active
:
bool
=
Field
(
index
=
True
)
\ No newline at end of file
sredis/sredis.py
View file @
1bbd6872
...
@@ -20,6 +20,7 @@ def add_datacenter(datacenter: str):
...
@@ -20,6 +20,7 @@ def add_datacenter(datacenter: str):
r
.
incr
(
"datacenters_cnt"
)
r
.
incr
(
"datacenters_cnt"
)
r
.
set
(
"roundrobin_cnt"
,
1
)
r
.
set
(
"roundrobin_cnt"
,
1
)
def
rr_get
():
def
rr_get
():
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
rr
=
int
(
r
.
get
(
"roundrobin_cnt"
))
rr
=
int
(
r
.
get
(
"roundrobin_cnt"
))
...
@@ -29,10 +30,12 @@ def rr_get():
...
@@ -29,10 +30,12 @@ def rr_get():
r
.
incr
(
"roundrobin_cnt"
)
r
.
incr
(
"roundrobin_cnt"
)
return
str
(
r
.
hget
(
"datacenters_hash"
,
rr
))
return
str
(
r
.
hget
(
"datacenters_hash"
,
rr
))
def
wr_get
(
centers
:
DataCenterSchema
):
def
wr_get
(
centers
:
DataCenterSchema
):
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
cnt
=
int
(
r
.
get
(
"datacenters_cnt"
))
indexis
=
range
(
1
,
cnt
+
1
)
indexis
=
range
(
1
,
cnt
+
1
)
return
random
.
choice
(
indexis
,
weights
=
())[
0
]
return
random
.
choice
(
indexis
,
weights
=
())[
0
]
def
set_token
(
username
:
str
,
datacenter
:
str
,
token
:
str
):
def
set_token
(
username
:
str
,
datacenter
:
str
,
token
:
str
):
print
(
f
"tokens:{username}"
+
datacenter
)
print
(
f
"tokens:{username}"
+
datacenter
)
...
@@ -64,8 +67,11 @@ def create_superpuser(user: User):
...
@@ -64,8 +67,11 @@ def create_superpuser(user: User):
s
=
PUser
.
find
(
PUser
.
username
==
user
.
username
)
.
all
()
s
=
PUser
.
find
(
PUser
.
username
==
user
.
username
)
.
all
()
if
not
s
:
if
not
s
:
puser
=
PUser
(
puser
=
PUser
(
username
=
user
.
username
,
email
=
user
.
email
,
password
=
hash_pass
(
user
.
password
),
admin
=
1
username
=
user
.
username
,
email
=
user
.
email
,
password
=
hash_pass
(
user
.
password
),
admin
=
1
,
)
)
puser
.
save
()
puser
.
save
()
return
True
return
True
return
False
return
False
\ No newline at end of file
stresstest/main.py
View file @
1bbd6872
import
json
import
json
from
locust
import
TaskSet
,
task
,
HttpUser
from
locust
import
TaskSet
,
task
,
HttpUser
class
PerformanceTests
(
TaskSet
):
class
PerformanceTests
(
TaskSet
):
def
on_start
(
self
):
def
on_start
(
self
):
resp
=
self
.
client
.
post
(
"/user/login"
,
json
=
{
resp
=
self
.
client
.
post
(
"username"
:
"karsa"
,
"/user/login"
,
json
=
{
"username"
:
"karsa"
,
"password"
:
"12345678"
}
"password"
:
"12345678"
)
})
self
.
client
.
headers
=
{
self
.
client
.
headers
=
{
'Authorization'
:
'Bearer '
+
json
.
loads
(
resp
.
_content
)[
'access_token'
]}
"Authorization"
:
"Bearer "
+
json
.
loads
(
resp
.
_content
)[
"access_token"
]
}
@task
(
1
)
@task
(
1
)
def
testFastApi
(
self
):
def
testFastApi
(
self
):
...
@@ -15,4 +17,4 @@ class PerformanceTests(TaskSet):
...
@@ -15,4 +17,4 @@ class PerformanceTests(TaskSet):
class
WebsiteUser
(
HttpUser
):
class
WebsiteUser
(
HttpUser
):
tasks
=
[
PerformanceTests
]
tasks
=
[
PerformanceTests
]
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment