• Stars
    star
    85
  • Rank 385,668 (Top 8 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“¦ Per-request global storage for Rails prepared for multi-threaded apps

RequestLocals

Gem Version Build Status Test Coverage Code Climate Inline docs License

If you have ever needed to use a global variable in Rails, you know it sucks.

One of the usual tricks is to go for Thread.current, or if you have done your homework, to use the awesome request_store.

# Using Thread.current
def self.foo
  Thread.current[:foo] ||= 0
end

def self.foo=(value)
  Thread.current[:foo] = value
end

# Using RequestStore
def self.foo
  RequestStore.fetch(:foo) { 0 }
end

def self.foo=(value)
  RequestStore.store[:foo] = value
end

The problem

  • Using Thread.current, values can stick around even after the request is over, since some servers have a pool of Threads that they reuse, which can cause bugs.

  • Using request_store, the storage is not actually request local. Variables are stored in Thread.current, except that the storage is cleared after each request. However, this does not work when you need to use multiple threads per request, different threads access different stores.

The solution

Add this line to your Gemfile:

gem 'request_store_rails'

And change the code to this:

def self.foo
  RequestLocals.fetch(:foo) { 0 }
end

def self.foo=(value)
  RequestLocals.store[:foo] = value
end

Oh yeah, everywhere you used Thread.current or RequestStore.store just change it to RequestLocals.store. Now your variables will actually be stored in a true request-local way.

No Rails? No Problem!

A Railtie is added that configures the Middleware for you, but if you're not using Rails, no biggie! Just use the Middleware yourself, however you need. You'll probably have to shove this somewhere:

use RequestStoreRails::Middleware

Multi-Threading

The middleware in the gem sets a thread-local variable :request_store_id in Thread.current for the main thread that is executing the request.

If you need to spawn threads within a server that is already using thread-based concurrency, all you need to do is to make sure that the :request_store_id variable is set for your threads, and you will be able to access the RequestLocals as usual.

A good way to apply this pattern is by encapsulating it into a helper class:

# Public: Custom thread class that allows us to preserve the request context.
class ThreadWithContext

  # Public: Returns a new Thread that preserves the context of the current request.
  def ThreadWithContext.new(*args)
    store_id = RequestLocals.current_store_id
    Thread.new {
      RequestLocals.set_current_store_id(store_id)
      yield *args
    }
  end
end

RequestLocals[:foo] = 1

ThreadWithContext.new {
  puts RequestLocals[:foo] # => 1
}

The gem does not provide such construct to avoid name collisions, you are free to reuse the snippet above and adjust it to match your use case.

If you are feeling adventurous, you could try using this fire and forget script and make all of your threads request aware, or should I say prepend and forget πŸ˜„? Probably not something to be used in a production environment, but whatever floats your boat β›΅

Atomicity

Have in mind that the RequestLocals.fetch(:foo) { 'default' } operation is atomic, while RequestLocal[:foo] ||= 'default' is not. In most scenarios, there is not a lot of difference, but if you are in a concurrent environment make sure to use the one that is more suitable for your use case πŸ˜‰

Replacing request_store

While the plan is not to achieve 100% compatibility, this gem usually works well as a drop-in replacement. If you are using gems that rely on RequestStore but for some reason you need them to use the appropriate request/thread scope, you can try something like this on application.rb or similar:3

if RequestStore != RequestLocals
  RequestStore::Railtie.initializers.clear
  Kernel.suppress_warnings { RequestStore = RequestLocals }
end

Usage in Sidekiq

If your code depends on these global variables, it's likely that you'll need to avoid collisions in Sidekiq workers (which would happen if the current store id is nil).

You can use the following middleware, using the job id to identify the store:

class Sidekiq::Middleware::Server::RequestStoreRails
  def call(_worker, job, _queue)
    RequestLocals.set_current_store_id(job['jid'])
    yield
  ensure
    RequestLocals.clear!
    RequestLocals.set_current_store_id(nil)
  end
end

Make sure to configure it as server middleware:

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::Middleware::Server::RequestStoreRails
  end
end

Special Thanks

The inspiration for this gem, tests, and a big part of the readme were borrowed from the really cool request_store gem. Thanks Steve πŸ˜ƒ

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Don't forget to run the tests with rake.

License

Copyright (c) 2015 MΓ‘ximo Mussini

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

vite_ruby

⚑️ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,270
star
2

iles

🏝 The joyful site generator
TypeScript
1,074
star
3

vite-plugin-image-presets

πŸ–Ό Image Presets for Vite.js apps
TypeScript
246
star
4

vite-plugin-environment

Easily expose environment variables in Vite.js
TypeScript
144
star
5

vite-plugin-full-reload

♻️ Automatically reload the page when files are modified
JavaScript
134
star
6

oj_serializers

⚑️ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
113
star
7

js_from_routes

πŸ›£οΈ Generate path helpers and API methods from your Rails routes
Ruby
98
star
8

types_from_serializers

βœ… Generate TypeScript interfaces from your JSON serializers
Ruby
92
star
9

vuex-stores

πŸ—„ Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
62
star
10

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
59
star
11

jekyll-vite

⚑️🩸 Use Vite.js in Jekyll as your assets pipeline
Ruby
50
star
12

vite-plugin-stimulus-hmr

⚑️ HMR for Stimulus controllers in Vite.js
TypeScript
50
star
13

mongoid_includes

🌿 Improves eager loading support for Mongoid
Ruby
46
star
14

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
43
star
15

queryable

❔ Gives your queries a home and avoid tucking scopes inside your models
Ruby
42
star
16

capybara-compose

βœ… Easily write fluent integration tests with Capybara in Ruby
Ruby
32
star
17

better_settings

βš™ Settings for Ruby apps – fast, immutable, better.
Ruby
20
star
18

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
19

i18n_multitenant

🌎 Provides a convenient way to use tenant-specific translations
Ruby
18
star
20

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
15
star
21

resourcerer

✨ Works like magic to dry up your controllers
Ruby
10
star
22

sublime-toggle-dark-mode

🌚🌞 Toggle between dark and light mode in Sublime Text 4
JavaScript
10
star
23

pakiderm

🐘 Pakiderm will never forget the return value
Ruby
7
star
24

presenter_rails

πŸ”­ Expose your view models in a convenient way
Ruby
6
star
25

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend
TypeScript
5
star
26

journeyman

Let your factories use your business logic, making them flexible and easier to update.
Ruby
5
star
27

jekyll-vite-minima

⚑️🩸 Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
31

ElMassimo

1
star
32

vite-vue-router-hmr-repro

Vue
1
star
33

crouton

🍞 Context sensitive notifications for Rails
Ruby
1
star