相关疑难解决方法(0)

Redis + ActionController ::活动线程没有死亡

背景:我们在一个现有的Rails应用程序中构建了一个聊天功能.我们正在使用新ActionController::Live模块并运行Puma(在生产中使用Nginx),并通过Redis订阅消息.我们正在使用EventSource客户端异步建立连接.

问题摘要:当连接终止时,线程永远不会死亡.

例如,如果用户离开,关闭浏览器,甚至转到应用程序内的其他页面,则会生成一个新线程(如预期的那样),但旧的线程继续存在.

我现在看到的问题是,当出现任何这种情况时,服务器无法知道浏览器端的连接是否被终止,直到某些东西试图写入这个损坏的流,这在浏览器中永远不会发生已离开原始页面.

这个问题似乎记录在github上,类似的问题在这里问StackOverflow (非常完全相同的问题)这里(关于获取活动线程的数量).

基于这些帖子,我能够提出的唯一解决方案是实现一种线程/连接扑克.尝试写入断开的连接会生成一个IOError我可以捕获并正确关闭连接,允许线程死亡.这是该解决方案的控制器代码:

def events
  response.headers["Content-Type"] = "text/event-stream"

  stream_error = false; # used by flusher thread to determine when to stop

  redis = Redis.new

  # Subscribe to our events
  redis.subscribe("message.create", "message.user_list_update") do |on| 
    on.message do |event, data| # when message is received, write to stream
      response.stream.write("messageType: '#{event}', data: #{data}\n\n")
    end

    # This is the monitor / connection poker thread
    # Periodically …
Run Code Online (Sandbox Code Playgroud)

multithreading ruby-on-rails publish-subscribe redis ruby-on-rails-4

34
推荐指数
2
解决办法
6110
查看次数