Skip to content

Commit

Permalink
Add configurable timeouts for http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-samoylenko committed Apr 16, 2024
1 parent faa7d6f commit 4129ac7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 20 deletions.
11 changes: 11 additions & 0 deletions shkeeper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import functools
import os
import logging
import secrets
from decimal import Decimal
import shutil

from flask import Flask
import requests

from .utils import format_decimal

Expand Down Expand Up @@ -41,6 +43,8 @@ def create_app(test_config=None):
TRON_MULTISERVER_GUI=bool(os.environ.get('TRON_MULTISERVER_GUI')),
FORCE_WALLET_ENCRYPTION=bool(os.environ.get('FORCE_WALLET_ENCRYPTION')),
UNCONFIRMED_TX_NOTIFICATION=bool(os.environ.get('UNCONFIRMED_TX_NOTIFICATION')),
REQUESTS_TIMEOUT=int(os.environ.get('REQUESTS_TIMEOUT', 10)),
REQUESTS_NOTIFICATION_TIMEOUT=int(os.environ.get('REQUESTS_NOTIFICATION_TIMEOUT', 30)),
)

if test_config is None:
Expand Down Expand Up @@ -81,6 +85,13 @@ def default(self, obj):
app.json_decoder = ShkeeperJSONDecoder
app.json_encoder = ShkeeperJSONEncoder

for method in ("get", "options", "head", "post", "put", "patch", "delete"):
setattr(
requests,
method,
functools.partial(getattr(requests, method), timeout=app.config.get('REQUESTS_TIMEOUT')),
)

db.init_app(app)
migrate.init_app(app, db)
with app.app_context():
Expand Down
2 changes: 1 addition & 1 deletion shkeeper/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flask import current_app as app
from flask.json import JSONDecoder
from flask_sqlalchemy import sqlalchemy
import requests
from shkeeper import requests

from shkeeper import db
from shkeeper.auth import basic_auth_optional, login_required, api_key_required
Expand Down
17 changes: 13 additions & 4 deletions shkeeper/callback.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import click
import requests
from shkeeper import requests

from flask import Blueprint
from flask import current_app as app
Expand Down Expand Up @@ -29,7 +29,12 @@ def send_unconfirmed_notification(crypto_name, txid, addr, amount):

app.logger.warning(f'[{crypto_name}/{txid}] Posting {notification} to {invoice.callback_url} with api key {apikey}')
try:
r = requests.post(invoice.callback_url, json=notification, headers={"X-Shkeeper-Api-Key": apikey})
r = requests.post(
invoice.callback_url,
json=notification,
headers={"X-Shkeeper-Api-Key": apikey},
timeout=app.config.get('REQUESTS_NOTIFICATION_TIMEOUT'),
)
except Exception as e:
app.logger.error(f'[{crypto_name}/{txid}] Unconfirmed TX notification failed: {e}')
return False
Expand Down Expand Up @@ -68,8 +73,12 @@ def send_notification(tx):
apikey = Crypto.instances[tx.crypto].wallet.apikey
app.logger.warning(f'[{tx.crypto}/{tx.txid}] Posting {notification} to {tx.invoice.callback_url} with api key {apikey}')
try:
r = requests.post(tx.invoice.callback_url,
json=notification, headers={"X-Shkeeper-Api-Key": apikey})
r = requests.post(
tx.invoice.callback_url,
json=notification,
headers={"X-Shkeeper-Api-Key": apikey},
timeout=app.config.get('REQUESTS_NOTIFICATION_TIMEOUT'),
)
except Exception as e:
app.logger.error(f'[{tx.crypto}/{tx.txid}] Notification failed: {e}')
return False
Expand Down
2 changes: 1 addition & 1 deletion shkeeper/modules/classes/bitcoin_like_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datetime
import functools

import requests
from shkeeper import requests
from shkeeper.modules.classes.crypto import Crypto


Expand Down
7 changes: 3 additions & 4 deletions shkeeper/modules/classes/bnb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod
from os import environ
import json
import requests
from shkeeper import requests
import datetime
from collections import namedtuple
from decimal import Decimal
Expand All @@ -28,7 +28,7 @@ def get_auth_creds(self):
return (username, password)


def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
if self.crypto == self.network_currency and subtract_fee_from_amount:
fee = Decimal(self.estimate_tx_fee(amount)['fee'])
if fee >= amount:
Expand All @@ -40,7 +40,7 @@ def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
auth=self.get_auth_creds(),
).json(parse_float=Decimal)
return response

def getstatus(self):
try:
response = requests.post(
Expand All @@ -59,4 +59,3 @@ def getstatus(self):

except Exception as e:
return "Offline"

2 changes: 1 addition & 1 deletion shkeeper/modules/classes/ethereum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod
from os import environ
import json
import requests
from shkeeper import requests
import datetime
from collections import namedtuple
from decimal import Decimal
Expand Down
2 changes: 1 addition & 1 deletion shkeeper/modules/classes/tron_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from collections import namedtuple

import requests
from shkeeper import requests
from flask import current_app as app

from shkeeper.modules.classes.crypto import Crypto
Expand Down
10 changes: 4 additions & 6 deletions shkeeper/modules/classes/xrp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod
from os import environ
import json
import requests
from shkeeper import requests
import datetime
from collections import namedtuple
from decimal import Decimal
Expand All @@ -26,7 +26,7 @@ def get_auth_creds(self):
return (username, password)


def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
if self.crypto == self.network_currency and subtract_fee_from_amount:
fee = Decimal(self.estimate_tx_fee(amount)['fee'])
if fee >= amount:
Expand All @@ -38,8 +38,8 @@ def mkpayout(self, destination, amount, fee, subtract_fee_from_amount=False):
auth=self.get_auth_creds(),
).json(parse_float=Decimal)
return response


def getstatus(self):
try:
response = requests.post(
Expand All @@ -59,5 +59,3 @@ def getstatus(self):

except Exception as e:
return "Offline"


2 changes: 1 addition & 1 deletion shkeeper/modules/rates/binance.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from decimal import Decimal

import requests
from shkeeper import requests

from shkeeper.modules.classes.rate_source import RateSource

Expand Down
2 changes: 1 addition & 1 deletion shkeeper/modules/rates/manual.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from decimal import Decimal

import requests
from shkeeper import requests

from shkeeper.modules.classes.rate_source import RateSource

Expand Down

0 comments on commit 4129ac7

Please sign in to comment.