ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110481] [Ruby master Bug#19078] Introduce `Fiber#storage` for inheritable fiber-scoped variables.
@ 2022-10-22 21:56 ioquatix (Samuel Williams)
  2022-10-24 23:07 ` [ruby-core:110504] [Ruby master Feature#19078] " tenderlovemaking (Aaron Patterson)
                   ` (28 more replies)
  0 siblings, 29 replies; 30+ messages in thread
From: ioquatix (Samuel Williams) @ 2022-10-22 21:56 UTC (permalink / raw)
  To: ruby-core

Issue #19078 has been reported by ioquatix (Samuel Williams).

----------------------------------------
Bug #19078: Introduce `Fiber#storage` for inheritable fiber-scoped variables.
https://bugs.ruby-lang.org/issues/19078

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* Assignee: ioquatix (Samuel Williams)
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Pull Request: https://github.com/ruby/ruby/pull/6612

This is an evolution of the previous ideas:

- https://bugs.ruby-lang.org/issues/19058
- https://bugs.ruby-lang.org/issues/19062

This PR introduces fiber scoped variables, and is a solution for problems like <https://github.com/ioquatix/ioquatix/discussions/17>.

The main interface is:

```ruby
Fiber[key] = value
Fiber[key] # => value
```

The variables are scoped by fiber and inherited into child fibers and threads.

```ruby
Fiber[:request_id] = SecureRandom.hex(16)

Fiber.new do
  p Fiber[:request_id] # prints the above request id
end
```

The fiber scoped variables are stored and can be accessed:

```ruby
Fiber.current.storage # => returns a Hash of the internal storage.
Fiber.current.storage= # => assigns a Hash to the internal storage.
```

I'm still on the fence as to whether these methods should return an actual mutable Hash, or function as a copy (so we don't force the internal representation to be a hash table).

Fiber itself has one new keyword argument:

```
Fiber.new(..., storage: hash, false, undef, nil)
```

This can control how the fiber variables are setup in a child context.

To minimise the performance overhead of some of the implementation choices, we are also simultaneously implementing <https://bugs.ruby-lang.org/issues/19077>.

## Examples

### Request loop

```ruby
Thread.new do
  while request = queue.pop
    Fiber.new(storage: {id: SecureRandom.hex(16)}) do
      handle_request.call(request)
    end
  end
end
```

OR

```ruby
Thread.new do
  while request = queue.pop
    Fiber.current.storage = {id: SecureRandom.hex(16)}
    handle_request.call(request)
  end
end
```




-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2022-12-21 13:41 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 21:56 [ruby-core:110481] [Ruby master Bug#19078] Introduce `Fiber#storage` for inheritable fiber-scoped variables ioquatix (Samuel Williams)
2022-10-24 23:07 ` [ruby-core:110504] [Ruby master Feature#19078] " tenderlovemaking (Aaron Patterson)
2022-10-24 23:09 ` [ruby-core:110505] " tenderlovemaking (Aaron Patterson)
2022-10-26 10:21 ` [ruby-core:110515] " byroot (Jean Boussier)
2022-10-26 20:47 ` [ruby-core:110519] " ioquatix (Samuel Williams)
2022-10-26 20:49 ` [ruby-core:110520] " ioquatix (Samuel Williams)
2022-10-27 10:15 ` [ruby-core:110522] " byroot (Jean Boussier)
2022-10-28  1:06 ` [ruby-core:110526] " ioquatix (Samuel Williams)
2022-11-08 15:48 ` [ruby-core:110658] " Eregon (Benoit Daloze)
2022-11-08 21:04 ` [ruby-core:110660] " byroot (Jean Boussier)
2022-11-09  0:31 ` [ruby-core:110664] " marcotc (Marco Costa)
2022-11-22 12:20 ` [ruby-core:110855] " Eregon (Benoit Daloze)
2022-11-22 17:32 ` [ruby-core:110859] " ioquatix (Samuel Williams)
2022-11-23 10:54 ` [ruby-core:110865] " Eregon (Benoit Daloze)
2022-11-23 19:55 ` [ruby-core:110868] " ioquatix (Samuel Williams)
2022-11-29 21:56 ` [ruby-core:111076] " ivoanjo (Ivo Anjo)
2022-11-30 17:11 ` [ruby-core:111092] " Dan0042 (Daniel DeLorme)
2022-12-01  4:59 ` [ruby-core:111103] " matz (Yukihiro Matsumoto)
2022-12-01  8:44 ` [ruby-core:111120] " ioquatix (Samuel Williams)
2022-12-01 10:05 ` [ruby-core:111125] " ioquatix (Samuel Williams)
2022-12-02  9:34 ` [ruby-core:111147] " ioquatix (Samuel Williams)
2022-12-02 15:00 ` [ruby-core:111158] " nevans (Nicholas Evans)
2022-12-02 16:23 ` [ruby-core:111159] " nevans (Nicholas Evans)
2022-12-20 13:42 ` [ruby-core:111348] " lloeki (Loic Nageleisen) via ruby-core
2022-12-20 14:53 ` [ruby-core:111351] " lloeki (Loic Nageleisen) via ruby-core
2022-12-20 16:49 ` [ruby-core:111353] " Eregon (Benoit Daloze) via ruby-core
2022-12-20 17:31 ` [ruby-core:111354] " Eregon (Benoit Daloze) via ruby-core
2022-12-20 18:22 ` [ruby-core:111355] " Eregon (Benoit Daloze) via ruby-core
2022-12-20 21:33 ` [ruby-core:111357] " Eregon (Benoit Daloze) via ruby-core
2022-12-21 13:41 ` [ruby-core:111359] " Eregon (Benoit Daloze) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).