Best Method of Channel Pooling in Google App Engine

前端 未结 2 1932
情话喂你
情话喂你 2021-01-30 18:38

It seems the only way to make the GAE Channel API financially viable is to implement some kind of pooling mechanism (one of the senior app engine product managers even told me t

相关标签:
2条回答
  • 2021-01-30 19:07

    According to the Google App Engine support team, channel tokens may not be reused. Reusing them is not expected to work.

    Can Google App Engine Channels be reused?

    0 讨论(0)
  • 2021-01-30 19:08

    Here's what I'd do (I'm actually considering writing this library after seeing your question. I need it too):

    Create a taskpool module with the following API.

    client_id, token = taskpool.get()
    
    # Setup a heartbeat in the client JS, maybe every minute. 
    # Also call this every time the client indicates presence
    taskpool.ping(client_id)
    
    taskpool.release(client_id)
    

    Implementation:

    • Store the client_id and token in an entity, with a status indicating whether it's being used, last ping time, and creation time. Let the client_id be the key. Also consider using NDB. Free memcaching.

    get() checks if there are unused tokens and returns one if it finds it. Otherwise create a new one, store and return it.

    ping() updates the last ping time for that token. Instead of polling, let the client send in a ping every [heartbeat] time.

    release() marks the token as unused.

    Run a task / cron every [heartbeat] seconds to find the tokens which haven't gotten a ping in a while - and set them as unused.

    When clients report a closed token, perform a get().

    Keep in mind, though, that a loss in security is a by-product of any kind of token pooling. If a malicious client has held on to a token and stopped sending heartbeats, it might later be able to listen in on the messages being passed to the new client once the token is re-purposed. This isn't a problem if you're on a fully public site, but keep it in mind anyway.

    I will update this answer if and when I write this up as a library.

    0 讨论(0)
提交回复
热议问题