From d7b0730b32b602523b7f98fc9bba3ee3c9bddd34 Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Fri, 24 Feb 2023 08:43:00 -0600 Subject: [PATCH] Turn all timeout into float values in seconds. Fixes: #2809 --- CHANGES.md | 2 ++ doc/content/migrating.md | 11 ++++++++ src/core/builtins/builtins_http.ml | 10 ++++--- src/core/io/srt_io.ml | 44 ++++++++++++++++++------------ src/libs/file.liq | 2 +- src/libs/http.liq | 6 ++-- src/libs/protocols.liq | 4 +-- 7 files changed, 51 insertions(+), 28 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ed4c87017a..807c900486 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -36,6 +36,8 @@ Changed: - Changed `cry` to be a required dependency. - Changed default character encoding in `output.harbor`, `output.icecast` `output.shoutcast` to `UTF-8` (#2704) +- BREAKING: all `timeout` settings and parameters are now `float` values + and in seconds (#2809) - Added support for a Javascript build an interpreter. - Removed support for `%define` variables, superseded by support for actual variables in encoders. diff --git a/doc/content/migrating.md b/doc/content/migrating.md index e14f14f211..be718e6779 100644 --- a/doc/content/migrating.md +++ b/doc/content/migrating.md @@ -11,6 +11,17 @@ The `!x` notation for getting the value of a reference is now deprecated. You should write `x()` instead. And `x := v` is now an alias for `x.set(v)` (both can be used interchangeably). +### Timeout + +We used to have timeout values labelled `timeout` or `timeout_ms`, some of these would be integer and +in milliseconds, other floating point and in seconds etc. This was pretty confusing so, now all `timeout` +settings and arguments have been unified to be named `timeout` and hold a floating point value representing +a number of seconds. + +In most cases, your script will not execute until you have updated your custom `timeout` +values but you should also review all of them to make sure that they follow the new +convention. + ### Harbor HTTP server The API for registering HTTP server endpoint was completely. It should be more flexible and diff --git a/src/core/builtins/builtins_http.ml b/src/core/builtins/builtins_http.ml index 71f37590f9..cce7ff41d9 100644 --- a/src/core/builtins/builtins_http.ml +++ b/src/core/builtins/builtins_http.ml @@ -69,9 +69,9 @@ let add_http_request ~base ~stream_body ~descr ~request name = Lang.bool_t, Some (Lang.bool true), Some "Perform redirections if needed." ); - ( "timeout_ms", - Lang.nullable_t Lang.int_t, - Some (Lang.int 10000), + ( "timeout", + Lang.nullable_t Lang.float_t, + Some (Lang.float 10.), Some "Timeout for network operations in milliseconds." ); ( "", Lang.string_t, @@ -99,7 +99,9 @@ let add_http_request ~base ~stream_body ~descr ~request name = List.map (fun (x, y) -> (Lang.to_string x, Lang.to_string y)) headers in let timeout = - Lang.to_valued_option Lang.to_int (List.assoc "timeout_ms" p) + Lang.to_valued_option + (fun v -> int_of_float (1000. *. Lang.to_float v)) + (List.assoc "timeout" p) in let http_version = Option.map Lang.to_string (Lang.to_option (List.assoc "http_version" p)) diff --git a/src/core/io/srt_io.ml b/src/core/io/srt_io.ml index 06ceff5acf..adb7389ded 100644 --- a/src/core/io/srt_io.ml +++ b/src/core/io/srt_io.ml @@ -103,24 +103,24 @@ let common_options ~mode = Some (Lang.float 2.), Some "Delay between connection attempts. Used only in caller mode." ); ( "read_timeout", - Lang.nullable_t Lang.int_t, - Some (Lang.int 1_000), + Lang.nullable_t Lang.float_t, + Some (Lang.float 1.), Some - "Timeout, in milliseconds, after which read operations are aborted if \ - no data was received, indefinite if `null`." ); + "Timeout, in seconds, after which read operations are aborted if no \ + data was received, indefinite if `null`." ); ( "write_timeout", - Lang.nullable_t Lang.int_t, - Some (Lang.int 1_000), + Lang.nullable_t Lang.float_t, + Some (Lang.float 1.), Some - "Timeout, in milliseconds, after which write operations are aborted if \ - no data was received, indefinite if `null`." ); + "Timeout, in seconds, after which write operations are aborted if no \ + data was received, indefinite if `null`." ); ( "connection_timeout", - Lang.nullable_t Lang.int_t, + Lang.nullable_t Lang.float_t, Some Lang.null, Some - "Timeout, in milliseconds, after which initial connection operations \ - are aborted if no data was received. Uses library's default if \ - `nulll`. Used only in `client` mode." ); + "Timeout, in seconds, after which initial connection operations are \ + aborted if no data was received. Uses library's default if `nulll`. \ + Used only in `client` mode." ); ("payload_size", Lang.int_t, Some (Lang.int 1316), Some "Payload size."); ("messageapi", Lang.bool_t, Some (Lang.bool true), Some "Use message api"); ( "on_connect", @@ -215,13 +215,19 @@ let parse_common_options p = let on_disconnect = List.assoc "on_disconnect" p in let polling_delay = Lang.to_float (List.assoc "polling_delay" p) in let read_timeout = - Lang.to_valued_option Lang.to_int (List.assoc "read_timeout" p) + Lang.to_valued_option + (fun v -> int_of_float (1000. *. Lang.to_float v)) + (List.assoc "read_timeout" p) in let write_timeout = - Lang.to_valued_option Lang.to_int (List.assoc "write_timeout" p) + Lang.to_valued_option + (fun v -> int_of_float (1000. *. Lang.to_float v)) + (List.assoc "write_timeout" p) in let connection_timeout = - Lang.to_valued_option Lang.to_int (List.assoc "connection_timeout" p) + Lang.to_valued_option + (fun v -> int_of_float (1000. *. Lang.to_float v)) + (List.assoc "connection_timeout" p) in { mode = mode_of_value (List.assoc "mode" p); @@ -264,8 +270,8 @@ let conf_level = Dtools.Conf.int ~p:(conf_log#plug "level") ~d:4 "Level" let conf_poll = Dtools.Conf.void ~p:(conf_srt#plug "poll") "Poll configuration" let conf_timeout = - Dtools.Conf.int ~p:(conf_poll#plug "timeout") ~d:100 - "Timeout for polling loop, in ms" + Dtools.Conf.float ~p:(conf_poll#plug "timeout") ~d:0.1 + "Timeout for polling loop, in seconda." let conf_enforced_encryption = Dtools.Conf.bool @@ -299,7 +305,9 @@ module Poll = struct let process () = try if List.length (Srt.Poll.sockets t.p) = 0 then raise Empty; - let read, write = Srt.Poll.wait t.p ~timeout:conf_timeout#get in + let read, write = + Srt.Poll.wait t.p ~timeout:(int_of_float (1000. *. conf_timeout#get)) + in let apply fn s = try fn s with exn -> diff --git a/src/libs/file.liq b/src/libs/file.liq index a89f115bb5..33aca2b21b 100644 --- a/src/libs/file.liq +++ b/src/libs/file.liq @@ -236,7 +236,7 @@ def file.download(~filename, ~timeout=5., url) = response = http.get.stream( on_body_data=file_writer, - timeout_ms=int_of_float(timeout * 1000.), + timeout=timeout, url ) diff --git a/src/libs/http.liq b/src/libs/http.liq index b77f9ae213..71b498b691 100644 --- a/src/libs/http.liq +++ b/src/libs/http.liq @@ -485,7 +485,7 @@ upload_file_fn = fun (~name, ~content_type, ~headers, ~boundary, ~filename, ~fil }]) headers = ("Content-Type", "multipart/form-data; boundary=#{data.boundary}")::headers - fn(headers=headers, timeout_ms=timeout, redirect=redirect, data=data.contents, url) + fn(headers=headers, timeout=timeout, redirect=redirect, data=data.contents, url) end # Send a file via POST request encoded in multipart/form-data. The contents can @@ -499,7 +499,7 @@ end # @param ~filename File name sent in the request. # @param ~file File whose contents is to be sent in the request. # @param ~contents Contents of the file sent in the request. -# @param ~timeout Timeout in ms. +# @param ~timeout Timeout in seconds. # @param ~redirect Follow reidrections. # @param url URL to post to. def http.post.file(~name="file", ~content_type=null(), ~headers=[], ~boundary=null(), ~filename=null(), ~file=null(), ~contents=null(), ~timeout=null(), ~redirect=true, url) @@ -518,7 +518,7 @@ end # @param ~filename File name sent in the request. # @param ~file File whose contents is to be sent in the request. # @param ~contents Contents of the file sent in the request. -# @param ~timeout Timeout in ms. +# @param ~timeout Timeout in seconds. # @param ~redirect Follow reidrections. # @param url URL to put to. def http.put.file(~name="file", ~content_type=null(), ~headers=[], ~boundary=null(), ~filename=null(), ~file=null(), ~contents=null(), ~timeout=null(), ~redirect=true, url) diff --git a/src/libs/protocols.liq b/src/libs/protocols.liq index eff777dbac..c8e0e31886 100644 --- a/src/libs/protocols.liq +++ b/src/libs/protocols.liq @@ -158,7 +158,7 @@ def protocol.http(proto,~rlog,~maxtime,arg) = timeout = maxtime - time() - ret = http.head(timeout_ms=int_of_float(timeout*1000.), uri) + ret = http.head(timeout=timeout, uri) code = ret.status_code ?? 999 headers = ret.headers @@ -198,7 +198,7 @@ def protocol.http(proto,~rlog,~maxtime,arg) = try response = http.get.stream( on_body_data=file_writer, - timeout_ms=int_of_float(timeout * 1000.), + timeout=timeout, uri )