Rails消耗外部API需要交错消费 [英] Rails consumption of external API requiring staggered consumption

查看:95
本文介绍了Rails消耗外部API需要交错消费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用外部服务执行搜索我的申请。



此搜索的结果需要从多个合作伙伴收集,并在10和90之间秒完成。在收集结果时,我反复轮询我的搜索会话以收集已经准备好的结果。



当我有新的结果,我把这些转移到客户端通过SSE。



我每5秒钟轮询一次。



我应该如何运行这个过程,的我的线程90秒(运行puma + nginx)。我需要维护我的控制器的状态,以推送SSE到请求的客户,并且不确定处理投票之间延迟的最佳方式。



非常感谢

解决方案

如果你真的想释放线程,你必须放弃SSE。为了接收SSE,浏览器维护与网络服务器的长期连接,在美洲狮的情况下,每个客户端连接将由单独的线程处理。



但是,如果您只想轮询部分结果,可以使用以下策略:


  1. 使用sidekiq启动后台搜索工作

  2. 缓存内存存储中每个搜索请求的部分结果,如redis

  3. 来自redis的投票结果

另一个选项可能是将邮件问题移至事件化服务器。事件化服务器不会在每个连接上产生一个单独的线程,不管是否长期。



一个这样的事件化服务器,完美地与rails完全集成,是Faye。程序将是:


  1. 客户订阅Faye讯息频道

  2. 客户端

  3. 在后台作业(sidekiq)中执行搜索

  4. 后台作业定期在同一Faye频道上发布部分结果

实际上,美洲狮多线程设置打算让你不要经历所有这一切。
我只是增加线程和进程的数量,只要你的系统允许,看看如何执行。



与Faye的短信



编辑1
重新思考在后台作业中移动搜索的实际效果是什么。
Sidekiq也有自己的线程池和sidekiq线程没有不同于美洲狮线程。搜索任务无论如何要做。它的线程将被大多数时间暂停,为IO。因此,上述两种解决方案的唯一好处是适当的资源平衡。它允许您定义将用于搜索作业的线程数和应用程序服务器的线程数。


  1. 在相同或不同的计算机上部署应用程序两次

  2. 配置

  3. 配置将应用程式的其余部分提供给第二个执行个体

  4. 没有单一的应用程式执行搜寻查询的nginx路由/负载平衡



  5. 您甚至可以完全放弃轮询,只需贴上到SSE


    I am using an external service to perform a search for my application.

    The results of this search need to be collected from multiple partners and take between 10 and 90 seconds to complete. While results are being collected I am repeatedly polling my search session to collect the results that have already been prepared.

    As and when I have new results I am shifting these up to the client via SSE.

    I am polling every 5 seconds or so.

    How should I be running this process without absolutely nuking one of my threads for 90 seconds (running puma + nginx). I need to maintain my controller's state to push the SSEs to the requesting client and am unsure of the best way of dealing with the delays between polls.

    much appreciated

    解决方案

    You have to give up on SSEs, if you really want to release the threads. In order to receive SSEs a browser maintains a long-living connection to the webserver and in case of puma, each client connection will be handled by a separate thread.

    However, if you just want to do polling of partial results you can use the following strategy:

    1. Start background search job with e.g sidekiq
    2. Cache partial results for each search request within an in-memory store like redis
    3. Poll results from redis

    Another option might be moving the messaging problem to an evented server. Evented Servers will not spawn a separate thread on each connection, no matter long-lived or not.

    One such evented server, which perfectly integrates with rails, is Faye. Procedure will be:

    1. Client subscribes on Faye message channel
    2. client intiates seach
    3. search is performed within background job(sidekiq)
    4. background job periodically publishes partial results on same Faye channel

    Actually the puma multithreaded setup intends to keep you from going through all of this. I would just increase the number threads and processes as far as your system allows and see how that performs. Adding more ram or some extra servers is allways cheaper and allows you to focus on another features.

    Messaging with Faye

    Edit 1 Rethinking about what would actually be the benefit of moving the search in a background job. Sidekiq also has it's own thread pool and a sidekiq thread does not differ from a puma thread. The search task has to be done anyway. Its threads will be suspended most of the time, wating for IO. So, the only benefit of the above 2 solutions is proper resource balancing. It allows you to define how many threads will be used for the search job and how many for your app server. So, how about following strategy:

    1. Deploy the app twice on same or different machines
    2. Configure nginx routing/loadbalancing for search queries with SSE to one app instance
    3. Configure serving the rest of the app to the second instance
    4. Have not a single thing of your app logic changed
    5. PROFIT

    You can even abandon polling completly and just stick to SSEs

    这篇关于Rails消耗外部API需要交错消费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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