Skip to content
This repository has been archived by the owner on Sep 13, 2018. It is now read-only.

Allow core to be specified for a TcpServer instance #110

Closed
aajtodd opened this issue Jan 5, 2017 · 9 comments
Closed

Allow core to be specified for a TcpServer instance #110

aajtodd opened this issue Jan 5, 2017 · 9 comments

Comments

@aajtodd
Copy link
Contributor

aajtodd commented Jan 5, 2017

Right now the with_handle method of TcpServer allows access to a handle of the core created by serve. It would be useful to allow the core instance to be passed in (or spawn the server using a provided handle).

Without such an API anything that needs access to the handle (e.g. other initialization that requires a handle) has to go through the with_handle call of TcpServer.

Whether a spawn vs run API (or both) is added is debatable.

@carllerche carllerche added this to the 0.2 release milestone Jan 6, 2017
@vorner
Copy link

vorner commented Jan 14, 2017

+1 to this. I think the other motivation would be to allow having multiple different TcpServers on the same event loop.

@radix
Copy link

radix commented Mar 2, 2017

I'm confused by the very idea of the serve function -- why should TCP server logic know anything at all about running the event loop? I'm used to setting up asynchronous applications like this (pure pseudo-code):

let reactor = reactor();

listen_tcp(reactor, addr, port, handle);
listen_tcp(reactor, addr, other_port, handle_other);
looping_call(reactor, 5, do_stuff);
connect_tcp(reactor, remote_addr, 80, fetch_data);

reactor.run();

This way it's easy to run multiple things on an event loop. Is this not something that Tokio supports? Or am I misunderstanding the whole concept of Reactor Cores?

@alexcrichton
Copy link
Contributor

@radix yes event loops support running many things (e.g. as you've mentioned)

The intention of serve and TcpServer was to be convenient, not all-encompassing.

@sardok
Copy link

sardok commented Apr 19, 2017

Is there any simplified version of TcpServer's serve function which explicitly instantiates the service and let it controls the socket based on protocol implementation?

@messense
Copy link

Is there anyway to obtain the handle in the service TcpServer.serve used?

@alexcrichton
Copy link
Contributor

@messense would TcpServer::with_handle solve your use case?

@NickeZ
Copy link

NickeZ commented Apr 30, 2017

@alexcrichton How do you use ::with_handle to get access to the core? My usecase is that I want to run a tcpserver and other futures on the same reactor core.

@alexcrichton
Copy link
Contributor

@NickeZ oh the with_handle function doesn't necessarily give you ownership of a Core, but it gives you the ability with a Handle to spawn more work onto a core. The TcpServer helper have a thread per core (only one core by default) and the closure is invoked once per thread with the Handle for the specified core. That way you can spawn more work onto the core at the beginning.

Note that TcpServer doesn't provide an easy way of running multiple TCP servers next to one another, to do that you'll need to call the bind_server function from tokio-proto directly. You can find some more information about that on a similar Hyper issue as well as this comment.

@carllerche
Copy link
Member

This will not be an issue w/ Tokio reform (that is in progress) as the handle will be set for the executor. I'm going to close this for now. Let me know if I am incorrect.

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

No branches or pull requests

8 participants