pyblish / pyblish-endpoint

Integration endpoint for externally running frontends
GNU Lesser General Public License v3.0
0 stars 2 forks source link

find_next_port() #20

Open mottosso opened 9 years ago

mottosso commented 9 years ago

Goal

To enable an arbitrary amount of servers to run, without accidentally tripping on each others ports.

Implementation

Currently, the port number of each server is randomised between two numbers; the odds of clashes are 1/1000.

Instead, we'll do something similar to the Freelance Protocol of ZeroMQ in which the server pings a pre-determined port number - e.g. 6000. If this port is occupied, the server responds with information about itself; such as which host it's running within. Next, it'll ping 6001, then 6002 and so forth until an available port is found.

When an available port number is found, the server listens for future pings from future clients.

>>> from pyblish_endpoint.server import find_next_port
>>> find_next_port()
6000
>>> find_next_port()
6000
>>> find_next_port(start=7000)
7000

Future

Though 1/1000 doesn't seem large, I've already encountered 2-3 occurrences in 3-4 months; which, when multiplied by the amount of potential users of Pyblish is an unacceptable number.

However, the prime purpose is not to enable zero clashes, but to enable a client to ask about available hosts currently running.

By "scanning" port numbers starting at a particular range, we'll be able to visualise running hosts and connect/re-connect running clients on-the-fly.

Useful.

mottosso commented 9 years ago

The backend of this has been implemented in 1.1.8, all that remains is the interface above. https://github.com/pyblish/pyblish-endpoint/blob/master/pyblish_endpoint/resource.py#L29