From 70f22473b3f4d6fdd9a1fc54ec61867a24475454 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Wed, 3 Jul 2024 12:55:07 +0100 Subject: [PATCH] Update proxy upstream limits, add heartbeat --- README.md | 4 ++++ config/nginx.conf | 6 +++--- docker-compose.yml | 1 + proxy/config.py | 2 ++ proxy/heartbeat.py | 36 ++++++++++++++++++++++++++++++++++++ proxy/main.py | 4 +++- 6 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 proxy/heartbeat.py diff --git a/README.md b/README.md index 78e1e2b..8ec27e4 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ JSON-RPC endpoints for SKALE chains. It is based on NGINX. - `ETH_ENDPOINT` - endpoint of the Ethereum network where `skale-manager` contracts are deployed +#### Optional environment variables + +- `HEARTBEAT_URL` - URL for healthcheck endpoint (optional) + ## License [![License](https://img.shields.io/github/license/skalenetwork/skale-proxy.svg)](LICENSE) diff --git a/config/nginx.conf b/config/nginx.conf index 5934398..721d237 100644 --- a/config/nginx.conf +++ b/config/nginx.conf @@ -20,9 +20,9 @@ http { limit_req zone=one burst=100; - proxy_read_timeout 180s; - proxy_connect_timeout 60s; - proxy_send_timeout 180s; + proxy_read_timeout 60s; + proxy_connect_timeout 30s; + proxy_send_timeout 60s; location / { proxy_http_version 1.1; diff --git a/docker-compose.yml b/docker-compose.yml index 9f90df9..2a9dc05 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ services: skale-proxy: environment: ETH_ENDPOINT: ${ETH_ENDPOINT} + HEARTBEAT_URL: ${HEARTBEAT_URL} image: skale-proxy:latest container_name: proxy_admin build: diff --git a/proxy/config.py b/proxy/config.py index 66f4b08..9e8c364 100644 --- a/proxy/config.py +++ b/proxy/config.py @@ -27,6 +27,8 @@ MONITOR_INTERVAL = os.getenv('MONITOR_INTERVAL', 60 * 60 * 2) +HEARTBEAT_URL = os.getenv('HEARTBEAT_URL') + NGINX_WWW_FOLDER = os.path.join(PROJECT_PATH, 'www') CHAINS_INFO_FILEPATH = os.path.join(NGINX_WWW_FOLDER, 'chains.json') diff --git a/proxy/heartbeat.py b/proxy/heartbeat.py new file mode 100644 index 0000000..51e395e --- /dev/null +++ b/proxy/heartbeat.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# +# This file is part of SKALE Proxy +# +# Copyright (C) 2024-Present SKALE Labs +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import logging +import requests + +logger = logging.getLogger(__name__) + + +def send_heartbeat(heartbeat_url): + if heartbeat_url is None: + logger.info('HEARTBEAT_URL is not set on the proxy') + try: + response = requests.get(heartbeat_url) + if response.status_code == 200: + logger.info('Heartbeat signal is successfully sent') + else: + logger.warning(f'Failed to send heartbeat signal: {response.status_code}') + except Exception as e: + logger.error(f'Failed to send heartbeat signal: {e}') diff --git a/proxy/main.py b/proxy/main.py index 9b0bc90..a0acc1c 100644 --- a/proxy/main.py +++ b/proxy/main.py @@ -24,10 +24,11 @@ from proxy.nginx import update_nginx_configs from proxy.endpoints import generate_endpoints from proxy.helper import init_default_logger, write_json +from proxy.heartbeat import send_heartbeat from proxy.str_formatters import arguments_list_string from proxy.config import ( CHAINS_INFO_FILEPATH, MONITOR_INTERVAL, ENDPOINT, SM_ABI_FILEPATH, - TMP_CHAINS_FOLDER, TMP_UPSTREAMS_FOLDER + TMP_CHAINS_FOLDER, TMP_UPSTREAMS_FOLDER, HEARTBEAT_URL ) @@ -48,6 +49,7 @@ def main(): schains_endpoints = generate_endpoints(ENDPOINT, SM_ABI_FILEPATH) write_json(CHAINS_INFO_FILEPATH, schains_endpoints) update_nginx_configs(schains_endpoints) + send_heartbeat(HEARTBEAT_URL) logger.info(f'Proxy iteration done, sleeping for {MONITOR_INTERVAL}s...') sleep(MONITOR_INTERVAL)