feat: post to leadere

This commit is contained in:
Aravinth Manivannan 2023-05-27 10:29:05 +05:30
parent 9c0a85915c
commit 6ba293d287
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

43
test.py
View file

@ -1,17 +1,17 @@
#!/bin/env /bin/python #!/bin/env /bin/python
# mCaptcha - A proof of work based DoS protection system # mCaptcha - A proof of work based DoS protection system
# Copyright © 2023 Aravinth Manivannan <realravinth@batsense.net> # Copyright © 2023 Aravinth Manivannan <realravinth@batsense.net>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the # published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. # License, or (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details. # GNU Affero General Public License for more details.
# #
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
@ -44,11 +44,12 @@ def metrics(host: str):
def write(host, data): def write(host, data):
resp = requests.post(f"http://{host}/write", json=data) resp = requests.post(f"http://{host}/write", json=data)
print(f"RPC Status: {resp.status_code}") print(f"RPC Status: {resp.status_code}")
data = resp.json() resp = resp.json()
if "Err" in data: if "Err" in resp:
leader = data["Err"]["APIError"]["ForwardToLeader"]["leader_node"]["addr"] leader = resp["Err"]["APIError"]["ForwardToLeader"]["leader_node"]["addr"]
write(leader, data) print(f"Forwarding write to leader {leader}")
return data['Ok']['data'] return write(leader, data)
return resp["Ok"]["data"]
def add_vote(host: str, captcha_id: str): def add_vote(host: str, captcha_id: str):
@ -77,14 +78,24 @@ def add_captcha(host: str, captcha_id: str):
pprint(f"Captcha added {captcha_id}: {resp}") pprint(f"Captcha added {captcha_id}: {resp}")
if __name__ == "__main__": host = "localhost:9001"
host = "localhost:9001" peers = [(2, "localhost:9002"), (3, "localhost:9003"), (4, "localhost:9004")]
# init(host) captcha_id = "test_1"
# add_host(host, id=2, peer="localhost:9002")
# switch_to_cluster(host, nodes=[1, 2]) def initialize_cluster():
# metrics(host) init(host)
captcha_id = "test_1" for peer_id, peer in peers:
add_host(host=host, id=peer_id, peer=peer)
switch_to_cluster(host, nodes=[1, 2,3,4])
add_captcha(host, captcha_id) add_captcha(host, captcha_id)
add_vote(host, captcha_id) add_vote(host, captcha_id)
for _ in range(0,600): for _ in range(0, 600):
add_vote(host, captcha_id) add_vote(host, captcha_id)
if __name__ == "__main__":
add_vote("localhost:9002", captcha_id)