poco icon indicating copy to clipboard operation
poco copied to clipboard

Question: HTTP Server with SocketReactor

Open kapcino opened this issue 7 years ago • 5 comments

I used to use HTTPServer class to create a http server. But when connection increases, it can't server the request even I set a large number of threads.

I found SocketRactor which uses reactor pattern, is it possible to implement my own http server based on this class and keep the same interface with HTTPServer?

Can I port code in HTTPServerConnection::run() to Reactor handler's onRead call back? Does HTTPServerSession read the socket synchronically?

Thanks!

kapcino avatar Oct 17 '17 03:10 kapcino

HTTPServer is built on TCPServer framework, which uses ThreadPool to handle requests. It seems to me that the logical route would be to (aside from replacing the TCP server foundation with SocketReactor) disable the socket I/O functionality in HTTPSession and give the HTTPSession's socket reference to the reactor for I/O.

This is just off the top of my head and I could be missing something important, @obiltschnig can provide more insight.

aleks-f avatar Oct 17 '17 20:10 aleks-f

It's much more complicated, unfortunately. Mostly due to the use of streams which do not work well with non-blocking or async I/O.

obiltschnig avatar Oct 17 '17 20:10 obiltschnig

It is very helpful, thanks a lot @aleks-f and @obiltschnig !

kapcino avatar Oct 18 '17 00:10 kapcino

OK, I looked a bit deeper into this, threaded server is a pain many people complain about.

Streams do not seem to be such a big problem - since they access socket through HTTPSession, internally we can maintain a "socketless" object that is fed by reactor and pretend like there's a blocking socket behind the interface, just like before. I ran into more trouble due to socket coupling down the class hierarchy and, of course, usual async and reactor-related problems.

Anyway, there are following new/modified classes:

HTTPSession - modified to work in "socketless" mode
HTTPServerAsyncConnection (new) - this, and HTTPSession, is where most of the new code is
HTTPServerSession (new) - this is now an abstract parent for sync and async HTTP*Session
HTTPServerAsyncSession (new) - inherits from HTTPServerSession
HTTPServerSyncSession (old HTTPServerSession) - now inherits from new HTTPServerSession)

A very early and rough PoC is in feature/reactor-http branch. Probably flawed in many ways (chunked won't work for sure yet), but I have one test that passes (well, most of the time - it hangs occasionally, did not have time to investigate.

@obiltschnig , I'll tag this issue with unspecified milestone and perhaps it may be a basis for async HTTP server in the future - take a look when you get a chance.

@kapcino if you feel inclined to help, do not hesitate (but we first have to see what the benevolent dictator says :)

aleks-f avatar Oct 19 '17 02:10 aleks-f

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Aug 26 '22 03:08 github-actions[bot]

This issue was closed because it has been inactive for 60 days since being marked as stale.

github-actions[bot] avatar Jan 02 '23 02:01 github-actions[bot]