diff mbox series

Add low speed limit to suricatta requests

Message ID 20190226101404.7572-1-Wojciech.Michna@assaabloy.com
State Changes Requested
Headers show
Series Add low speed limit to suricatta requests | expand

Commit Message

Wojciech Michna Feb. 26, 2019, 10:14 a.m. UTC
This allows the user to set  LOW_SPEED_LIMIT and
LOW_SPEED_TIME for cURL Connection in Suricatta requests.
This allows the user to define when Suricatta should drop
connection when it's to slow.

Signed-off-by: Szczypta Marek <Marek.Szczypta@assaabloy.com>
---
 corelib/channel_curl.c     | 13 ++++++++++++-
 include/channel_curl.h     |  1 +
 suricatta/common.c         |  6 ++++++
 suricatta/server_hawkbit.c | 36 ++++++++++++++++++++++++++++++++----
 4 files changed, 51 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/corelib/channel_curl.c b/corelib/channel_curl.c
index fea6c05..8374430 100644
--- a/corelib/channel_curl.c
+++ b/corelib/channel_curl.c
@@ -480,6 +480,17 @@  channel_op_res_t channel_set_options(channel_t *this,
 			  "this is most probably not what you want. "
 			  "Adapted it to %us instead.\n", SPEED_LOW_TIME_SEC);
 	}
+
+	if (channel_data->low_speed_limit == 0) {
+		channel_data->low_speed_limit = SPEED_LOW_BYTES_SEC;
+		DEBUG("cURL's low download speed limit is disabled, "
+			  "this is most probably not what you want. "
+			  "Adapted it to %uB per second instead.\n", SPEED_LOW_BYTES_SEC);
+	}
+
+	DEBUG("cURL's timeout is  %us .\n", channel_data->low_speed_timeout);
+	DEBUG("cURL's limit   is %uB .\n", channel_data->low_speed_limit);
+
 	channel_curl_t *channel_curl = this->priv;
 	channel_op_res_t result = CHANNEL_OK;
 	if ((curl_easy_setopt(channel_curl->handle, CURLOPT_URL,
@@ -487,7 +498,7 @@  channel_op_res_t channel_set_options(channel_t *this,
 	    (curl_easy_setopt(channel_curl->handle, CURLOPT_USERAGENT,
 			      "libcurl-agent/1.0") != CURLE_OK) ||
 	    (curl_easy_setopt(channel_curl->handle, CURLOPT_LOW_SPEED_LIMIT,
-			      SPEED_LOW_BYTES_SEC) != CURLE_OK) ||
+	    		channel_data->low_speed_limit) != CURLE_OK) ||
 	    (curl_easy_setopt(channel_curl->handle, CURLOPT_LOW_SPEED_TIME,
 			      channel_data->low_speed_timeout) != CURLE_OK) ||
 	    (curl_easy_setopt(channel_curl->handle, CURLOPT_HTTPHEADER,
diff --git a/include/channel_curl.h b/include/channel_curl.h
index 0e499c6..0e13111 100644
--- a/include/channel_curl.h
+++ b/include/channel_curl.h
@@ -53,6 +53,7 @@  typedef struct {
 	unsigned int method;
 	unsigned int retries;
 	unsigned int low_speed_timeout;
+	unsigned int low_speed_limit;
 	channel_body_t format;
 	bool debug;
 	bool usessl;
diff --git a/suricatta/common.c b/suricatta/common.c
index 7e59885..23d922b 100644
--- a/suricatta/common.c
+++ b/suricatta/common.c
@@ -21,6 +21,12 @@  void suricatta_channel_settings(void *elem, channel_data_t *chan)
 	get_field(LIBCFG_PARSER, elem, "retry",
 		&chan->retries);
 
+	get_field(LIBCFG_PARSER, elem, "lstimeout",
+		&chan->low_speed_timeout);
+
+	get_field(LIBCFG_PARSER, elem, "lslimit",
+		&chan->low_speed_limit);
+
 	GET_FIELD_STRING_RESET(LIBCFG_PARSER, elem, "retrywait", tmp);
 	if (strlen(tmp))
 		chan->retry_sleep =
diff --git a/suricatta/server_hawkbit.c b/suricatta/server_hawkbit.c
index 53801e1..9dfca16 100644
--- a/suricatta/server_hawkbit.c
+++ b/suricatta/server_hawkbit.c
@@ -34,6 +34,20 @@ 
 
 #define JSON_OBJECT_FREED 1
 
+/*
+ * Number of seconds while below low speed
+ * limit before aborting. It can be overwritten
+ * by -s command line flag.
+ */
+#define CHANNEL_DEFAULT_LOWSPEED_TIME	300
+
+/*
+ * Number of bytes per seconds that
+ * the transfer should be before aborting. It can be overwritten
+ * by -l command line flag.
+ */
+#define CHANNEL_DEFAULT_LOWSPEED_LIMIT	8
+
 static struct option long_options[] = {
     {"tenant", required_argument, NULL, 't'},
     {"id", required_argument, NULL, 'i'},
@@ -46,6 +60,8 @@  static struct option long_options[] = {
     {"proxy", optional_argument, NULL, 'y'},
     {"targettoken", required_argument, NULL, 'k'},
     {"gatewaytoken", required_argument, NULL, 'g'},
+    {"lstimeout", required_argument, NULL, 's'},
+    {"lslimit", required_argument, NULL, 'l'},
     {NULL, 0, NULL, 0}};
 
 static unsigned short mandatory_argument_count = 0;
@@ -121,7 +137,9 @@  static channel_data_t channel_data_defaults = {.debug = false,
 					       .format = CHANNEL_PARSE_JSON,
 					       .nocheckanswer = false,
 					       .nofollow = false,
-					       .strictssl = true};
+					       .strictssl = true,
+						   .low_speed_timeout = CHANNEL_DEFAULT_LOWSPEED_TIME,
+						   .low_speed_limit = CHANNEL_DEFAULT_LOWSPEED_LIMIT};
 
 static struct timeval server_time;
 
@@ -1444,9 +1462,11 @@  void server_print_help(void)
 	    "\t  -y, --proxy         Use proxy. Either give proxy URL, else "
 	    "{http,all}_proxy env is tried.\n"
 	    "\t  -k, --targettoken   Set target token.\n"
-	    "\t  -g, --gatewaytoken  Set gateway token.\n",
+	    "\t  -g, --gatewaytoken  Set gateway token.\n"
+	    "\t  -s, --lstimeout     timeout to check if a connection is lost (default: %d)\n"
+	    "\t  -l, --lslimit     average transfer speed Bytes per seconds (default: %d)\n",
 	    CHANNEL_DEFAULT_POLLING_INTERVAL, CHANNEL_DEFAULT_RESUME_TRIES,
-	    CHANNEL_DEFAULT_RESUME_DELAY);
+	    CHANNEL_DEFAULT_RESUME_DELAY,CHANNEL_DEFAULT_LOWSPEED_TIME,CHANNEL_DEFAULT_LOWSPEED_LIMIT);
 }
 
 static int server_hawkbit_settings(void *elem, void  __attribute__ ((__unused__)) *data)
@@ -1519,7 +1539,7 @@  server_op_res_t server_start(char *fname, int argc, char *argv[])
 
 	/* reset to optind=1 to parse suricatta's argument vector */
 	optind = 1;
-	while ((choice = getopt_long(argc, argv, "t:i:c:u:p:xr:y::w:k:g:",
+	while ((choice = getopt_long(argc, argv, "t:i:c:u:p:xr:y::w:k:g:l:s:",
 				     long_options, NULL)) != -1) {
 		switch (choice) {
 		case 't':
@@ -1595,6 +1615,14 @@  server_op_res_t server_start(char *fname, int argc, char *argv[])
 			channel_data_defaults.retry_sleep =
 			    (unsigned int)strtoul(optarg, NULL, 10);
 			break;
+		case 's':
+			channel_data_defaults.low_speed_timeout =
+			    (unsigned int)strtoul(optarg, NULL, 10);
+			break;
+		case 'l':
+			channel_data_defaults.low_speed_limit =
+			    (unsigned int)strtoul(optarg, NULL, 10);
+			break;
 		case '?':
 		default:
 			return SERVER_EINIT;