From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET,SPF_HELO_PASS, SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [IPv6:2a01:4f8:1c0c:6b10::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id BAF521F4C1 for ; Tue, 29 Nov 2022 21:57:09 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.b="XRuqVlNt"; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 9B5CB7E88F; Tue, 29 Nov 2022 21:57:02 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=XRuqVlNt; dkim-atps=neutral Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 1DE127E881 for ; Tue, 29 Nov 2022 21:56:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc; s=s1; bh=k/Feu37Bz822NVi2a3NMiieH5idgEy1g+vEWWgSDpVI=; b=XRuqVlNtt9+RD2vz95SNN4wid3JoG4c9zTctGQEYMHdAwGUozSSRLD0L2iACsODd/tYv zSEAzjxxvgQ7YYgbcaS2hekefX/HvOF3sfq+GqTD+cWVVUBqOvINPBaNCpN+mLU7EiUazk +5de0yPCYRXkWFef07+GHHvSXD0ky2vJEYuaOpDPbqtOsDsUo7fzH4BFDt9OYOfmg6z4nY wPxjRxR/8Fc69L2zChf5r+kyRTpOriXOoKDb338KoVTimm9JH34FULe0rDMQ5Xb7wTfCgH I9MrJifHTaDGggl/H2XshF194vfDAr4M9o//1SZnhfamlwCDGKOYGIiTLut/1CIA== Received: by filterdrecv-6f5868ff54-9sws2 with SMTP id filterdrecv-6f5868ff54-9sws2-1-63868027-23 2022-11-29 21:56:55.756619538 +0000 UTC m=+1032607.068409711 Received: from herokuapp.com (unknown) by geopod-ismtpd-6-3 (SG) with ESMTP id TV3EbYmoQXadELG0jIzMUw for ; Tue, 29 Nov 2022 21:56:55.708 +0000 (UTC) Date: Tue, 29 Nov 2022 21:56:55 +0000 (UTC) From: "ivoanjo (Ivo Anjo)" Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 19078 X-Redmine-Issue-Author: ioquatix X-Redmine-Issue-Assignee: ioquatix X-Redmine-Sender: ivoanjo X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 87426 X-SG-EID: =?us-ascii?Q?vq9NZUDHExLc+waeN8EQFLrIaw8M3n++fdtL0epuT2XFW0IcKdePWbcForkxKy?= =?us-ascii?Q?SnyAc52BkFE52cpMNFrfeJmISuVEbMONJciAiBj?= =?us-ascii?Q?VBIRCNDSem0773cp=2FeN4Jz6PgT7NLsqk9Ur7sW2?= =?us-ascii?Q?+cQNQchC4hZfL9psYXAT7ZN40+Gzw31XISxW+Do?= =?us-ascii?Q?ht+yB8lGkrqpzlJhu9N+MDYCPQ9vXJUxI399yIw?= =?us-ascii?Q?9FX8a4Cwwe37d3yLTc7b95S53e4ZV4+I3Zr1i00?= =?us-ascii?Q?2AVWQpaXXT6aF8YWRC8YA=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: K22LUJK6G64LVJKYJ7H5KU5BYW6YBQF3 X-Message-ID-Hash: K22LUJK6G64LVJKYJ7H5KU5BYW6YBQF3 X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:111076] [Ruby master Feature#19078] Introduce `Fiber#storage` for inheritable fiber-scoped variables. List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #19078 has been updated by ivoanjo (Ivo Anjo). marcotc (Marco Costa) wrote in #note-14: > > > Regarding threads I think we shouldn't inherit automatically in new threads, and rather do it explicitly (via Fiber.current.storage=) in the rare cases it's needed. > > > > I'm on the fence on this one. Usually the code spawning a thread, and the code using thread/fiber storage and needing it to be inherited are entirely decoupled. So you'd need to go convince all your dependencies that spawn threads (e.g. puma, sidekiq, etc) to add that one line of code. So I'd prefer if it was always implicitly copied. > > For cross-cutting concerns, like telemetry, automatic inheritance works best; asking a gem user to add one extra line per Fiber created would create room for error. > > I think thinking about the opposite use case, users that explicitly want a clean Fiber storage for newly created Fibers, would help here: how common is such use case? I can't think of a reason to have this as the default, except for saving on the performance cost of copying the storage. To a bit of info on top of this (and disclaimer, I work with Marco [on the ddtrace gem](https://github.com/datadog/dd-trace-rb), one really nice property of the automatic inheritance is that it makes easy something that is otherwise quite hard to do automatically from regular Ruby code. E.g. to simulate such an automatic mechanism, one needs to monkey patch thread and fiber creation, which is really awkward and error-prone (ask me how I know this). ---------------------------------------- Feature #19078: Introduce `Fiber#storage` for inheritable fiber-scoped variables. https://bugs.ruby-lang.org/issues/19078#change-100334 * Author: ioquatix (Samuel Williams) * Status: Open * Priority: Normal * Assignee: ioquatix (Samuel Williams) ---------------------------------------- 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 . The main interface is: ```ruby Fiber[key] = value Fiber[key] # => value ``` The variables are scoped (local to) a 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 (copy) of the internal storage. Fiber.current.storage= # => assigns a Hash (copy) to the internal storage. ``` 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 . ## 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/