Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How does JGO use long polling?  (Read 3883 times)
0 Members and 1 Guest are viewing this topic.
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Posted 2015-08-02 21:56:30 »

I'm wondering exactly how the "there are X new posts" notification at the top of this very site works.

I know the basics: it uses ajax to request the number of new posts, and then I think it uses asynchronous long polling on the server side to only return the number when it changes.

But on the server side, doesn't that mean there are a bunch of threads (or maybe a thread pool) waiting on that update? More to the point: if I refresh the page 500 times (sorry Riven), doesn't that mean there are 500 threads (or thread pool tasks) waiting around- 499 of which are now pointless, since I'll only ever receive the last one? Isn't that a potential memory leak- if I get a bot to constantly refresh the page and create more of those waiting objects, won't it eventually break?

I'm asking about this site, but what I'm actually trying to figure out is the correct way to do this in Spring. I'm not sure how different JGO's php server is from a Java server, but I'm hoping the above questions are general enough to apply to my situation as well.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2015-08-02 22:16:38 »

I read the http-header, parse some shit, put the Socket in a List. The socket will be the only resource I need to retain to send a response, when I need to send a notification. At the time of an event, all objects in the list are iterated, the list is cleared, and response are written to all sockets that are connected. All new connections are put in that list again. The threadpool reading the requests and writing the responses is tiny.

This all happens right at the socket level. There are no servlets, no PHP scripts, just a List that holds pending requests.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #2 - Posted 2015-08-02 22:24:55 »

That's interesting, thanks for the reply.

That's a little lower level than I assumed, but I guess the question is the same: won't you end up with a big list of sockets? Wouldn't it be possible (or even easy) for bad guys to overload that list until something breaks? And aren't you keeping around references to a bunch of sockets that have been disconnected, since users make a request every time they load a page? Am I missing something, or are those references so tiny that it's not a real concern?

Apologies for the dumb questions, I've just been trying to wrap my head around this stuff for a few days now. It's been an unproductive weekend!

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2015-08-02 22:48:58 »

Apache is easier to overload than this crude long-poller, so why would I even bother. You can do pretty effective damage control at the firewall level - why burden applications with it.

If you look at the raw network traffic you'll see I do slightly more than this, behind the scenes, but in the end the entire setup is mostly single threaded. Naturally this thread manages a JDBC connection, that polls MySQL for the latest post-id at 1Hz. Once that changes, it is considered an event, and the responses will be sent.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline noctarius

JGO Knight


Medals: 61


Manager Developer Relations @Hazelcast


« Reply #4 - Posted 2015-08-03 04:20:40 »

My HTTP  Long-Polling system works slightly different. I know users logged in into the game, so I collect updates until the next requests comes in (normally you always have a connection, in practice reconnection might need a few millis). The connection knows it's userid and some special session key (so that nobody can hijack a players session).

For suspending the long-polling sockets I use Jetty continuations or the new Servlet 3 features and create an async context. That returns the underlying thread back to the pool until I need the socket to send results back. Most often I anyways don't really have to hold them for a long time since game content is highly changing but it heavily depends on the game what the update rate is.

To send events to the server (the second connection) the server immediately responds with an event id the client can wait for in the long-polling events.

Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #5 - Posted 2015-08-03 12:55:49 »

Riven: That's even more interesting. So behind the scenes you're still doing "active polling" (or whatever it's called). That's probably easier than trying to add a hook into the message posting system (especially for complicated sites with multiple user actions), but I wonder if you're concerned at all about making unnecessary calls to the database, especially if a good portion of the time there are no connected users? (I assume there are a few hours each day where nobody is connected, that might be a false assumption though.)

noctarius: That sounds closer to what I'm actually doing. I'm using Spring's DeferredResult, which I believe uses Servlet 3's async stuff. You make an interesting point about incorporating session data into the data structure that holds waiting objects. That way you can do it by session instead of by request: if that session then makes a new request, you can probably throw away any old requests by that session.

That will probably not be correct if the user has multiple pages open though. Hmph.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2015-08-03 13:01:11 »

Active polling through a JDBC connection (
SELECT MAX(ID_MSG) FROM jgo_msg
at 1Hz) is not worthy of optimization. I've got better things to do than to worry about 1 second of CPU time per day Pointing

JGO is never deserted Smiley and even if it were, sobeit. It is a constant load, so if it is okay when the server is under heavy load, it's certainly acceptable if the server is idle. The most important resource to consider would be my limited time, not to get a near-idling CPU to fully idle. It would be trivial to add though, but I can't be bothered.

What you have to consider though is minimizing the Socket send/recv buffer sizes, as that can take quite a bit of RAM.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (846 views)
2019-09-04 15:33:17

hadezbladez (5789 views)
2018-11-16 13:46:03

hadezbladez (2602 views)
2018-11-16 13:41:33

hadezbladez (6205 views)
2018-11-16 13:35:35

hadezbladez (1498 views)
2018-11-16 13:32:03

EgonOlsen (4733 views)
2018-06-10 19:43:48

EgonOlsen (5791 views)
2018-06-10 19:43:44

EgonOlsen (3275 views)
2018-06-10 19:43:20

DesertCoockie (4174 views)
2018-05-13 18:23:11

nelsongames (5500 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!