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

Add set_read_timeout/2 and set_write_timeout/2 #1152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aarroyoc
Copy link
Sponsor Contributor

@aarroyoc aarroyoc commented Dec 19, 2021

Add timeout option for Tcp Streams

This fixes #1083 but there are two issues:

  • The error it gives when the timeout works (and the read operation fails) doesn't make any sense, but I think it requires more refactoring so I'm waiting until the new rebis-dev branch arrives. For example: caught: error(syntax_error(input_output_error),get_char/2) its what get_char/2 throws when the timeout does its job
  • infinite is a special value. An atom to deactivate the timeout. However, using other atoms don't throw type_error nor domain_error (it just fails). I've added a comment but I think a new value of DomainErrorType could fit here, but other approaches might be worth discussing

Test code:

?- use_module(library(sockets)).
   true.
?- socket_server_open('0.0.0.0':9000, Socket), socket_server_accept(Socket, _, Stream, [type(text)]), set_read_timeout(Stream, 10), get_char(Stream, X).

Then telnet to port 9000 and wait ten seconds.

@UWN
Copy link

UWN commented Dec 19, 2021

Why not as an option to write_term/2 and read_term/2? Avoid state, if possible, OTOH, not sure what you mean by read and write.

@aarroyoc
Copy link
Sponsor Contributor Author

@UWN in #1083 it was said that a separate predicate would be an option. These predicates only apply to TcpStream (reading now, maybe it's not clear from the original text).

@UWN
Copy link

UWN commented Dec 19, 2021

Needs to be discussed.

@triska
Copy link
Contributor

triska commented Dec 19, 2021

I think one of the most important applications for this may be phrase_from_stream/2, i.e., to apply a DCG to a network stream. Should a timeout option become available for every predicate that reads from a stream, such as read_n_chars/3 or read_line_to_chars/3 from library(charsio)?

@aarroyoc
Copy link
Sponsor Contributor Author

That would work too but I'm not sure it's worth duplicating every read/write predicate. Implementation will also be more complicated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a predicate to set a timeout while reading an stream
3 participants