DEV Community

Shinichi Maeshima
Shinichi Maeshima

Posted on

Released Gon v7.0.0

I am a current maintainer of Gon. We have released Gon v7.0.0.

Release v7.0.0 · gazay/gon

As you can tell from the major version bump, this release includes breaking changes.
These breaking changes were introduced by the following PR created by @krororo:

Breaking: Make request_store an optional dependency by krororo · Pull Request #296 · gazay/gon

In gon, when you set a value like gon.foo = 'bar', the value used to be stored internally using request_store.
In the early days of Rails application development, request_store was almost the only practical option when you wanted to use a global value that was automatically reset for each request.

However, since Rails 5.2, Rails has provided an official alternative:
ActiveSupport::CurrentAttributes.

The PR above removes the dependency on request_store and switches gon to use ActiveSupport::CurrentAttributes instead.

Why this is good

I believe this change improves stability.

Currently, request_store uses Thread#[] internally.
As documented in the Thread#[] documentation, Thread#[] actually refers to fiber-local variables, which means the value cannot be accessed once execution switches to another Fiber.

Because of this, if your request-handling code uses Fibers, gon may behave incorrectly.

ActiveSupport::CurrentAttributes also initially relied on fiber-local variables when it was first introduced
(see this implementation).

However, starting with Rails 7.0, Rails can automatically switch between fiber-local and thread-local storage depending on the type of server being used
(see this commit by Shopify).

As a result, if you are using Rails 7.0 or later, ActiveSupport::CurrentAttributes should be more stable than request_store.
That is why we decided to merge this PR.

Concerns

Most users of gon probably don’t consciously think about whether it uses request_store or ActiveSupport::CurrentAttributes internally.

Given that, the new behavior is as follows:

  • If you are using Rails 5.2 or later and ActiveSupport::CurrentAttributes is available, gon will use it automatically.
  • Otherwise, you will need to explicitly add request_store to your Gemfile:
gem 'request_store'
Enter fullscreen mode Exit fullscreen mode

I believe there are very few cases where you would want to explicitly use request_store with modern versions of Rails, which is why we made this decision.
That said, if you run into any issues, please let us know!

Top comments (0)