Selecting server ports
Two features to implement for this issue;
- [ ] port range/randomisation
- [ ] ensuring ports in use return some value/throw
Commit 530e5f9 reintroduced throwing for the classic Tcp transport; hasn't been validated for LibUv.
@ademar said
I have been thinking of introducing dynamic ports; like: type Port = Fixed of uint16 | Range of (uint16 * uint16)
Which is one way of doing it.
Ping @philsquared – the new issue I mentioned.
I started working on this here https://github.com/ranma42/suave/tree/wip/rand-port but I am unsure about what is the best way forward.
It looks like Suave often assumes that the input data for the binding will match exactly the binding outcome. Some of that can be relaxed without breaking changes, but at least HttpBinding will probably need to allow a port range.
What is the best way to change this?
So far (beside minor changes), I introduced a SocketBindingRange which allows a port range and a preferred port to be specified. It is then used in the HttpBindingRange, which replaces HttpBinding as the type to be used when specifying the list of desired bindings.
For some reason I missed the original ping on this - sorry for not responding sooner, @haf. I noticed that throwing on port binding failure was reintroduced (although by trying it) a while back and that sorted things for me. The extra work on port randomisation looks interesting, though, and I'd be keen to switch to that when it's ready. My use is on a production environment at the moment so I can't really afford to test it too much before then but will try to give it a go in a dev env to give feedback.
Another alternative could be binding to port 0 to pick up an ephemeral port to listen to (https://www.dnorth.net/2012/03/17/the-port-0-trick/). This almost works in Suave 2.1.1. The only problem I have is that Suave reports it is bound to port 0 and I end up having to use netstat -ab (on windows) to determine the ephemeral port that was assigned.