Skip to content

Achieve true thread local context #61

@rwdaigle

Description

@rwdaigle

In an environment using a Fiber pool (such as an evented Rack app), setting a Scrolls context in a Rack middleware will not then be available to all scopes within that request. This is because in Ruby 2.x Thread.current[]= behavior does not actually set a thread-local variable, but a Fiber-local one.

To achieve true thread locality you need to use thread_variable_get (and set). I believe this is how context should be implemented?

module Scrolls
  module Log

    def context=(h)
      Thread.current.thread_variable_set(:scrolls_context, h || {})
    end

    def context
      Thread.current.thread_variable_get(:scrolls_context)
    end
  end
end

If so, let me know and I can submit a proper PR. One up front question would be your preference in Ruby version detection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions