lemunozm / message-io

Fast and easy-to-use event-driven network library.
Apache License 2.0
1.11k stars 74 forks source link

Serve new events with multiple threads #113

Closed fgadaleta closed 2 years ago

fgadaleta commented 2 years ago

Is there a way to put the loop node_listener.for_each(move |event| match event.network() {... in a thread pool?

My application is already spawning threads for services and message-io would be one of such services.

lemunozm commented 2 years ago

Hi @fgadaleta,

I understand that you want to use different threads to process different events coming from ´for_each, right? From thenodeAPI I think you couldn't since once you callfor_each` you are tied with the thread who called it. Maybe you could copy those events and process them later in your thread pool although this would imply copying the message data.

The node API is no more than a wrapper along the NetworkController and the NetworkProcessor to handle it more easily. I think that using directly those entities you can archive what you want because you have more control. You can see an example of using it directly in benches/latency.rs (latency_by()and init_connection() functions).

I'm glad message-io is added to your services 😃

fgadaleta commented 2 years ago

I think I solved by wrapping the typical .run() method (from the distributed discovery server example) into a thread and seems to work.

Now that all services are running in separate threads (and message-io is one of them), it's time to play with different transport protocols - which i find extremely well done :)

lemunozm commented 2 years ago

Great! 😃 I hope it is useful for your project.

Any problem do not hesitate to ask

fgadaleta commented 2 years ago

Thank you so much! It'll be great