-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added db router with random algo for now
- Loading branch information
Amey Tendulkar
committed
Nov 1, 2023
1 parent
fd98336
commit 285ed46
Showing
4 changed files
with
50 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import random | ||
|
||
class PrimaryReplicaRouter: | ||
def db_for_read(self, model, **hints): | ||
""" | ||
Reads go to a randomly-chosen replica. | ||
""" | ||
db_to = random.choice(['replica1', 'replica2']) | ||
print('****'*20) | ||
print(db_to) | ||
return db_to | ||
|
||
def db_for_write(self, model, **hints): | ||
""" | ||
Writes always go to primary. | ||
""" | ||
return 'primary' | ||
|
||
def allow_relation(self, obj1, obj2, **hints): | ||
""" | ||
Relations between objects are allowed if both objects are | ||
in the primary/replica pool. | ||
""" | ||
db_set = {'primary', 'replica1', 'replica2'} | ||
if obj1._state.db in db_set and obj2._state.db in db_set: | ||
return True | ||
return None | ||
|
||
def allow_migrate(self, db, app_label, model_name=None, **hints): | ||
""" | ||
All non-auth models end up in this pool. | ||
""" | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import requests | ||
|
||
def callapi(): | ||
url = "http://localhost:8000/api/products-by-category?category=21&page=2" | ||
|
||
payload = {} | ||
headers = {} | ||
|
||
response = requests.request("GET", url, headers=headers, data=payload) | ||
|
||
for i in range(0,100): | ||
callapi() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters