【问题标题】:Redis connection in multi-threaded environment (Unicorn)多线程环境下的Redis连接(独角兽)
【发布时间】:2012-06-07 04:53:39
【问题描述】:

我已经为这个错误苦苦挣扎了很长一段时间:

Redis::ProtocolError: Got 'i' as initial reply byte. 
If you're running in a multi-threaded environment, make sure you pass the :thread_safe
option when initializing the connection. If you're in a forking environment, such as 
Unicorn, you need to connect to Redis after forking.

在使用 Unicorn 和 Redis 的应用程序中会间歇性发生这种情况。从这个redis-rb Github issue 看来,:thread_safe 选项现在默认启用。我正在使用redis 2.2.2,因为redis 3.0.1 与最新版本的resque 不兼容。

在我的 Unicorn 配置中,我在分叉后使用 Redis.current.quit

我还使用名为 ruote-redis 的 gem 连接到 Redis,它是工作流引擎 Ruote 的存储实现。

我如何确保我的所有 Redis 连接都稳定,并且我不会再收到这个影响我们应用程序正常使用的错误?

【问题讨论】:

    标签: ruby-on-rails ruby redis unicorn


    【解决方案1】:

    Unicorn 不是多线程的。您自己使用线程吗?

    如文档中所述,您遇到的问题是多个 Unicorn 工作人员共享同一个连接(即同一个底层文件描述符)。

    This change,包含在 redis-rb 3.0 版本中,使其更加清晰。

    如果您仍然遇到此错误,请发布您的 Unicorn 配置。

    【讨论】:

    • 我没有使用线程。我很想使用 redis-rb 3.0,但 resque 尚不兼容。我已将 ruote-redis gem 切换为使用 Mongo 进行存储的 ruote-mon。这似乎已经解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多