-
Notifications
You must be signed in to change notification settings - Fork 177
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
Safer alternative to Lwt_io.establish_server #258
Conversation
Didn't look at the code, but this seems very useful indeed. Safe usage of resources is crucial (I hate debugging FDs leaks). About the "concurrent connection limiting": this could be done with an optional |
An
Seems like it might be a nice thing to have. Want to be careful, however, because ideally we could reduce the number of features in Lwt, and/or reinterpret more things in terms of others. |
It can be a simple addition to type semaphore = private int t
val semaphore : int -> semaphore
val with_semaphore : semaphore -> (unit -> 'a Lwt.t) -> 'a Lwt.t |
Yes. But I guess a mutex could also be a semaphore with limit 1, but Also, I wonder if/how |
When establish_server closes a socket, it first calls shutdown(2), then close(2) on it. When one calls shutdown, if the peer has already closed the connection, the result is ENOTCONN. This is translated into an exception in OCaml. Before this commit, the server did not handle this exception, so it never called close on the socket in this case, instead allowing a spurious exception to escape.
6875f74
to
c346d81
Compare
Lwt_io.establish_server
is designed to be analogous toLwt_io.open_connection
: it hands you channels, but doesn't try to close them. You have to close them yourself.People have expressed (1, 2) a desire for a version of
establish_server
that is more likeLwt_io.with_connection
: it runs your callbackf
to spawn handler threads, and automatically schedules closing the associated channels once each thread completes. This PR adds such a function:I'm calling it
establish_server_async
as suggested by @c-cube for now, but other suggestions are welcome.This seems like a difficult problem at first, since if the implicit
close
calls raise exceptions, the user code has no opportunity to catch them and do logging, etc – among other potential challenges. However, if the user needs control over that, it can explicitly close the channels inf
, and surround that with whatever handling code it wants. The additional implicitclose
s will then have no effect.So, this should give a safer API for simple usage, that nonetheless remains usable in more complex scenarios.
In fact, I've thought about this at length, and I can find only one reason, besides API breakage, not to replace
establish_server
: one may want to disable implicit channel closing, because the channels are, for example, being stored byf
in some data structure. However, we could control that with an optional argument.Other things to consider:
I don't think
establish_server_async
has any impact on these, relative toestablish_server
, but...I will probably rewrite the documentation after getting a fresh look at it in some days.
cc @c-cube @fxfactorial @artemkin
Resolves #208.
Resolves #225.