带有IMAP IDLE的Ruby on Rails用于多个帐户 [英] Ruby on Rails with IMAP IDLE for multiple accounts

查看:46
本文介绍了带有IMAP IDLE的Ruby on Rails用于多个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个Ruby on Rails应用程序,该应用程序允许用户通过Gmail登录,并且与他们的收件箱之间始终保持IDLE连接.电子邮件一旦进入Gmail收件箱,就需要进入应用程序.

I'm currently building a Ruby on Rails app that allows users to sign in via Gmail and it have a constant IDLE connection to their Inbox. Emails need to arrive in the app as soon as they come into their Gmail Inbox.

目前,在实现方面,我有以下问题,还有一些我确实需要帮助弄清楚的问题.

Currently I have the following in terms of implementation, and some issues that I really need some help figuring out.

目前,当Rails应用启动时,它会为每个用户创建一个线程,该线程进行身份验证并循环运行以保持IDLE连接保持活动状态.

At the moment, when the Rails app boots up, it creates a thread per user which authenticates and runs in a loop to keep the IDLE connection alive.

每10到15分钟,线程就会反弹IDLE",以便传输少量数据以确保IDLE连接保持活动状态.

Every 10-15 minutes, the thread will "bounce IDLE", so that a little data is transferred to make sure the IDLE connection stays alive.

我认为的主要问题是可扩展性以及该应用与Postgres的连接数量.似乎每个线程都需要连接到Postgres,这在Heroku上会受到最大连接数的限制(最大连接数为20,基本连接数为500).

The major issue I think is in terms of scalability and how many connections the app has to Postgres. It seems that each thread requires a connection to Postgres, this will be heavily limited on Heroku by the number of max connections (20 for basic and 500 for any plans after that).

我确实需要以下方面的帮助:

I really need help with the following:

  • 使所有这些IDLE连接保持活动状态却减少数据库所需的线程数和连接数的最佳方法是什么?
    • 注意:如果对Gmail的刷新令牌用完了,则可能会刷新用户令牌,因此需要访问数据库
    • What's the best way to keep all these IDLE connections alive, but reducing the number of threads and connections needed to the database?
      • Note: user token refresh may happen if the refresh token to Gmail runs out, so this requires access to the database

      在这个问题中,我已经实现了与OP类似的功能: Ruby IMAP IDLE并发-如何解决?

      I have implemented something similar to the OP in this question: Ruby IMAP IDLE concurrency - how to tackle?

      推荐答案

      无需为每个IMAP会话生成新线程.这些可以在单个线程中完成.

      There is no need to spawn a new thread for each IMAP session. These can be done in a single thread.

      维护所有用户及其IMAP会话的数组(或哈希).产生一个线程,在该线程中,一个接一个地向每个连接发送IDLE保持活动状态.定期运行循环.这肯定会给您比当前方法更多的并发性.

      Maintain an Array (or Hash) of all users and their IMAP sessions. Spawn a thread, in that thread, send IDLE keep-alive to each of the connections one after the other. Run the loop periodically. This will definitely give you far more concurrency than your current approach.

      一个长期的方法是使用EventMachine.这将允许在同一线程中使用许多IMAP连接.如果要在同一过程中处理Web请求,则应为事件计算机创建一个单独的线程.这种方法可以为您提供惊人的并发性.有关事件机兼容的IMAP库,请参见 https://github.com/ConradIrwin/em-imap .

      A long term approach will be to use EventMachine. That will allow using many IMAP connections in the same thread. If you are processing web requests in the same process, you should create a separate thread for Event Machine. This approach can provide you phenomenal concurrency. See https://github.com/ConradIrwin/em-imap for Eventmachine compatible IMAP library.

      这篇关于带有IMAP IDLE的Ruby on Rails用于多个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆