Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn all timeout into float values in seconds #2920

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions doc/content/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions src/core/builtins/builtins_http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ 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),
Some "Timeout for network operations in milliseconds." );
( "timeout",
Lang.nullable_t Lang.float_t,
Some (Lang.float 10.),
Some "Timeout for network operations in seconds." );
( "",
Lang.string_t,
None,
Expand All @@ -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))
Expand Down
44 changes: 26 additions & 18 deletions src/core/io/srt_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
5 changes: 3 additions & 2 deletions src/libs/externals.liq
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,14 @@ end
# @flag extra
# @param ~urgency Urgency (low|normal|critical).
# @param ~icon Icon filename or stock icon to display.
# @param ~time Timeout in milliseconds.
# @param ~timeout Timeout in seconds.
# @param ~display Function used to display a metadata packet.
# @param ~title Title of the notification message.
# @category Source / Track processing
def notify_metadata(~urgency="low",~icon="stock_smiley-22",~time=3000,
def notify_metadata(~urgency="low",~icon="stock_smiley-22",~timeout=3.,
~display=string_of_metadata,
~title="Liquidsoap: new track",s)
time = int_of_float(timeout * 1000.)
send = 'notify-send -i #{icon} -u #{urgency}'
^ ' -t #{time} #{process.quote(title)} '
s.on_metadata(fun (m) -> ignore(process.run(send^process.quote(display(m)))))
Expand Down
2 changes: 1 addition & 1 deletion src/libs/file.liq
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
6 changes: 3 additions & 3 deletions src/libs/http.liq
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/libs/protocols.liq
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
)

Expand Down