pax_global_header00006660000000000000000000000064146150117410014512gustar00rootroot0000000000000052 comment=e2299f7a88c3336946e1c469cd086fd3350f5529 fiber-local-1.1.0/000077500000000000000000000000001461501174100136705ustar00rootroot00000000000000fiber-local-1.1.0/.editorconfig000066400000000000000000000001511461501174100163420ustar00rootroot00000000000000root = true [*] indent_style = tab indent_size = 2 [*.{yml,yaml}] indent_style = space indent_size = 2 fiber-local-1.1.0/.github/000077500000000000000000000000001461501174100152305ustar00rootroot00000000000000fiber-local-1.1.0/.github/workflows/000077500000000000000000000000001461501174100172655ustar00rootroot00000000000000fiber-local-1.1.0/.github/workflows/coverage.yaml000066400000000000000000000021201461501174100217370ustar00rootroot00000000000000name: Coverage on: [push, pull_request] permissions: contents: read env: CONSOLE_OUTPUT: XTerm COVERAGE: PartialSummary jobs: test: name: ${{matrix.ruby}} on ${{matrix.os}} runs-on: ${{matrix.os}}-latest strategy: matrix: os: - ubuntu - macos ruby: - "3.3" steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 5 run: bundle exec bake test - uses: actions/upload-artifact@v3 with: name: coverage-${{matrix.os}}-${{matrix.ruby}} path: .covered.db validate: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.3" bundler-cache: true - uses: actions/download-artifact@v3 - name: Validate coverage timeout-minutes: 5 run: bundle exec bake covered:validate --paths */.covered.db \; fiber-local-1.1.0/.github/workflows/documentation.yaml000066400000000000000000000021621461501174100230230ustar00rootroot00000000000000name: Documentation on: push: branches: - main # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages: permissions: contents: read pages: write id-token: write # Allow one concurrent deployment: concurrency: group: "pages" cancel-in-progress: true env: CONSOLE_OUTPUT: XTerm BUNDLE_WITH: maintenance jobs: generate: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: "3.3" bundler-cache: true - name: Installing packages run: sudo apt-get install wget - name: Generate documentation timeout-minutes: 5 run: bundle exec bake utopia:project:static --force no - name: Upload documentation artifact uses: actions/upload-pages-artifact@v3 with: path: docs deploy: runs-on: ubuntu-latest environment: name: github-pages url: ${{steps.deployment.outputs.page_url}} needs: generate steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4 fiber-local-1.1.0/.github/workflows/test-external.yaml000066400000000000000000000011461461501174100227520ustar00rootroot00000000000000name: Test External on: [push, pull_request] permissions: contents: read env: CONSOLE_OUTPUT: XTerm jobs: test: name: ${{matrix.ruby}} on ${{matrix.os}} runs-on: ${{matrix.os}}-latest strategy: matrix: os: - ubuntu - macos ruby: - "3.1" - "3.2" - "3.3" steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 10 run: bundle exec bake test:external fiber-local-1.1.0/.github/workflows/test.yaml000066400000000000000000000016641461501174100211370ustar00rootroot00000000000000name: Test on: [push, pull_request] permissions: contents: read env: CONSOLE_OUTPUT: XTerm jobs: test: name: ${{matrix.ruby}} on ${{matrix.os}} runs-on: ${{matrix.os}}-latest continue-on-error: ${{matrix.experimental}} strategy: matrix: os: - ubuntu - macos ruby: - "3.1" - "3.2" - "3.3" experimental: [false] include: - os: ubuntu ruby: truffleruby experimental: true - os: ubuntu ruby: jruby experimental: true - os: ubuntu ruby: head experimental: true steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 with: ruby-version: ${{matrix.ruby}} bundler-cache: true - name: Run tests timeout-minutes: 10 run: bundle exec bake test fiber-local-1.1.0/.gitignore000066400000000000000000000001511461501174100156550ustar00rootroot00000000000000/.bundle/ /pkg/ /gems.locked /.covered.db /external # rspec failure tracking .rspec_status /gems.locked fiber-local-1.1.0/example/000077500000000000000000000000001461501174100153235ustar00rootroot00000000000000fiber-local-1.1.0/example/request.rb000066400000000000000000000005651461501174100173460ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2021, by Samuel Williams. require_relative '../lib/fiber/local' require 'securerandom' module RequestID extend Fiber::Local end Fiber.new do RequestID.instance = SecureRandom.uuid pp RequestID.instance pp Fiber.current Fiber.new do pp RequestID.instance end.resume end.resume fiber-local-1.1.0/fiber-local.gemspec000066400000000000000000000013351461501174100174160ustar00rootroot00000000000000# frozen_string_literal: true require_relative "lib/fiber/local/version" Gem::Specification.new do |spec| spec.name = "fiber-local" spec.version = Fiber::Local::VERSION spec.summary = "Provides a class-level mixin to make fiber local state easy." spec.authors = ["Samuel Williams"] spec.license = "MIT" spec.cert_chain = ['release.cert'] spec.signing_key = File.expand_path('~/.gem/release.pem') spec.homepage = "https://github.com/socketry/fiber-local" spec.metadata = { "source_code_uri" => "https://github.com/socketry/fiber-local.git", } spec.files = Dir.glob(['{lib}/**/*', '*.md'], File::FNM_DOTMATCH, base: __dir__) spec.required_ruby_version = ">= 3.1" spec.add_dependency "fiber-storage" end fiber-local-1.1.0/gems.rb000066400000000000000000000005311461501174100151470ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2024, by Samuel Williams. source "https://rubygems.org" gemspec group :maintenance, optional: true do gem "bake-gem" gem "bake-modernize" gem "utopia-project" end group :test do gem "sus" gem "covered" gem "bake-test" gem "bake-test-external" end fiber-local-1.1.0/lib/000077500000000000000000000000001461501174100144365ustar00rootroot00000000000000fiber-local-1.1.0/lib/fiber/000077500000000000000000000000001461501174100155255ustar00rootroot00000000000000fiber-local-1.1.0/lib/fiber/local.rb000066400000000000000000000032241461501174100171450ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2024, by Samuel Williams. require_relative "local/version" require 'fiber/storage' class Fiber module Local def self.extended(klass) attribute_name = klass.name.gsub('::', '_').gsub(/\W/, '').downcase.to_sym # This is used for the general interface and fiber storage key: klass.instance_variable_set(:@fiber_local_attribute_name, attribute_name) klass.singleton_class.attr :fiber_local_attribute_name # This is used for reading and writing directly to the thread instance variables: klass.instance_variable_set(:@fiber_local_variable_name, :"@#{attribute_name}") Thread.attr_accessor(attribute_name) end # Instantiate a new thread-local object. # By default, invokes {new} to generate the instance. # @returns [Object] def local self.new end # Get the current thread-local instance. Create it if required. # @returns [Object] The thread-local instance. def instance # This is considered a local "override" in the dynamic scope of the fiber: if instance = Fiber[@fiber_local_attribute_name] return instance end # This is generally the fast path: thread = Thread.current unless instance = thread.instance_variable_get(@fiber_local_variable_name) if instance = self.local thread.instance_variable_set(@fiber_local_variable_name, instance) end end return instance end # Assigns to the fiber-local instance. # @parameter instance [Object] The object that will become the thread-local instance. def instance= instance Fiber[@fiber_local_attribute_name] = instance end end end fiber-local-1.1.0/lib/fiber/local/000077500000000000000000000000001461501174100166175ustar00rootroot00000000000000fiber-local-1.1.0/lib/fiber/local/version.rb000066400000000000000000000002401461501174100206250ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020, by Samuel Williams. class Fiber module Local VERSION = "1.1.0" end end fiber-local-1.1.0/license.md000066400000000000000000000020731461501174100156360ustar00rootroot00000000000000# MIT License Copyright, 2020-2024, by Samuel Williams. 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. fiber-local-1.1.0/readme.md000066400000000000000000000036441461501174100154560ustar00rootroot00000000000000# Fiber::Local A module to simplify fiber-local state. [![Development Status](https://github.com/socketry/fiber-local/workflows/Test/badge.svg)](https://github.com/socketry/fiber-local/actions?workflow=Test) ## Features - Easily access fiber-local state from a fiber. - Default to shared thread-local state. ## Installation ``` bash $ bundle add fiber-local ``` ## Usage In your own class, e.g. `Logger`: ``` ruby class Logger extend Fiber::Local def initialize @buffer = [] end def log(*arguments) @buffer << arguments end end ``` Now, instead of instantiating your cache `LOGGER = Logger.new`, use `Logger.instance`. It will return a thread-local instance. ``` ruby Thread.new do Logger.instance # => # end Thread.new do Logger.instance # => # end ``` In cases where you have job per fiber or request per fiber, you might want to collect all log output for a specific fiber, you can do the following: ``` ruby Logger.instance # => # Fiber.new do Logger.instance = Logger.new # => # end ``` ## Contributing We welcome contributions to this project. 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. ### Developer Certificate of Origin This project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted. ### Contributor Covenant This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms. ## See Also - [thread-local](https://github.com/socketry/thread-local) — Strictly thread-local variables. fiber-local-1.1.0/release.cert000066400000000000000000000033141461501174100161700ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11 ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz MjRaFw0zMjA4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tt9rNeXmnW/ c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp 8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8 voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg= -----END CERTIFICATE----- fiber-local-1.1.0/test/000077500000000000000000000000001461501174100146475ustar00rootroot00000000000000fiber-local-1.1.0/test/fiber/000077500000000000000000000000001461501174100157365ustar00rootroot00000000000000fiber-local-1.1.0/test/fiber/local.rb000066400000000000000000000034751461501174100173660ustar00rootroot00000000000000# frozen_string_literal: true # Released under the MIT License. # Copyright, 2020-2024, by Samuel Williams. require 'fiber/local' class MyThing extend Fiber::Local end describe Fiber::Local do it "has a version number" do expect(Fiber::Local::VERSION).to be =~ /\d+\.\d+\.\d+/ end with '#instance' do it "creates unique instances in different threads" do instance1 = Thread.new do MyThing.instance end.value instance2 = Thread.new do MyThing.instance end.value expect(instance1).not.to be_equal(instance2) end it "returns same instance in same thread" do expect(MyThing.instance).to be_equal(MyThing.instance) end it "inherits assigned values in nested fibers" do instance = Object.new storage = Fiber.new do MyThing.instance = instance expect(MyThing.instance).to be_equal(instance) Fiber.new do expect(MyThing.instance).to be_equal(instance) Fiber.current.storage end.resume end.resume expect(storage).to have_keys( MyThing.fiber_local_attribute_name => be_equal(instance) ) end it "inherits assigned values in nested threads" do instance = Object.new storage = Thread.new do MyThing.instance = instance Thread.new do Fiber.current.storage end.value end.value expect(storage).to have_keys( MyThing.fiber_local_attribute_name => be_equal(instance) ) end end with '#instance=' do let(:object) {Object.new} it "can assign an object to the fiber local instance" do MyThing.instance = object expect(MyThing.instance).to be_equal(object) end it "has fiber local scope" do Fiber.new do MyThing.instance = object expect(MyThing.instance).to be_equal(object) end expect(MyThing.instance).not.to be_equal(object) end end end