Ruby on Rails with IMAP IDLE for multiple accounts -


i'm building ruby on rails app allows users sign in via gmail , have constant idle connection inbox. emails need arrive in app come gmail inbox.

currently have following in terms of implementation, , issues need figuring out.

at moment, when rails app boots up, creates thread per user authenticates , runs in loop keep idle connection alive.

every 10-15 minutes, thread "bounce idle", little data transferred make sure idle connection stays alive.

the major issue think in terms of scalability , how many connections app has postgres. seems each thread requires connection postgres, heavily limited on heroku number of max connections (20 basic , 500 plans after that).

i need following:

  • what's best way keep these idle connections alive, reducing number of threads , connections needed database?
    • note: user token refresh may happen if refresh token gmail runs out, requires access database
  • are there other suggestions how may implemented?

edit:

i have implemented similar op in question: ruby imap idle concurrency - how tackle?

there no need spawn new thread each imap session. these can done in single thread.

maintain array (or hash) of users , imap sessions. spawn thread, in thread, send idle keep-alive each of connections 1 after other. run loop periodically. give far more concurrency current approach.

a long term approach use eventmachine. allow using many imap connections in same thread. if processing web requests in same process, should create separate thread event machine. approach can provide phenomenal concurrency. see https://github.com/conradirwin/em-imap eventmachine compatible imap library.


Comments