fiber-local 0.1.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 487140cfe893634ff979f39b7e8419f7b72c7bbec0e58628335935d144bc8acf
4
- data.tar.gz: '08681f3a21f2f04b271cea1b2be63e752df0b4d0fcff3cae429cf3d62fa45052'
3
+ metadata.gz: e1e3b15bd18f38db71fa92464959b53f3a9be937efe4bdd1466e96bcf50bc3c3
4
+ data.tar.gz: 10a113b80613dd0627665d0044e7c7365f64c8448dfddaa8bed7953aff19fa49
5
5
  SHA512:
6
- metadata.gz: 7207d5abd5004234e2eaf65849ddb86407b429673f1ceb8be4a13e04fdf405396f9f3284d30129e1d23967aca690b61ec759654f2e0d14b5a371910d7f712384
7
- data.tar.gz: 585da444f4d94926883c6625e1d15501c59319f6227911bcc947614853b6743ed428d639e175ad77df53f2a985a3e7578d37d155ffd8e0d550ab98c6a8d9ea8f
6
+ metadata.gz: d612d3c43dbe0d58ca35a47d57de39b73f928a489f64f1fd3456b345390be901d4f4bcec166dc072ef5f49770d80d12911b147dfbe94ff1ed86db448632d9457
7
+ data.tar.gz: 1cf83815c08ab6c1378dac5d952b1dbbe4bc18214cbb15d3f41b83ccdbab03149e822f7b6774ded3774efa20e5387b91fd6b2f4203c8d583780c83d9c7fb29fa
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,2 @@
1
+ R�4?�j� ��+ǎ�K\�S��Ve����5�3M����Q���\�d~ÿ�劈�W�=� ��:@��c���:��$��<U��
2
+ >��� ����d����^��[��s��u�N+��J���7��O�N9[���A��Z��гV~�i
@@ -1,27 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2020, by Samuel Williams.
22
5
 
23
6
  class Fiber
24
7
  module Local
25
- VERSION = "0.1.0"
8
+ VERSION = "1.1.0"
26
9
  end
27
10
  end
data/lib/fiber/local.rb CHANGED
@@ -1,29 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
3
+ # Released under the MIT License.
4
+ # Copyright, 2020-2024, by Samuel Williams.
22
5
 
23
6
  require_relative "local/version"
7
+ require 'fiber/storage'
24
8
 
25
9
  class Fiber
26
10
  module Local
11
+ def self.extended(klass)
12
+ attribute_name = klass.name.gsub('::', '_').gsub(/\W/, '').downcase.to_sym
13
+
14
+ # This is used for the general interface and fiber storage key:
15
+ klass.instance_variable_set(:@fiber_local_attribute_name, attribute_name)
16
+ klass.singleton_class.attr :fiber_local_attribute_name
17
+
18
+ # This is used for reading and writing directly to the thread instance variables:
19
+ klass.instance_variable_set(:@fiber_local_variable_name, :"@#{attribute_name}")
20
+
21
+ Thread.attr_accessor(attribute_name)
22
+ end
23
+
27
24
  # Instantiate a new thread-local object.
28
25
  # By default, invokes {new} to generate the instance.
29
26
  # @returns [Object]
@@ -34,29 +31,26 @@ class Fiber
34
31
  # Get the current thread-local instance. Create it if required.
35
32
  # @returns [Object] The thread-local instance.
36
33
  def instance
37
- thread = Thread.current
38
- name = self.name
39
-
40
- if instance = thread[self.name]
34
+ # This is considered a local "override" in the dynamic scope of the fiber:
35
+ if instance = Fiber[@fiber_local_attribute_name]
41
36
  return instance
42
37
  end
43
38
 
44
- unless instance = thread.thread_variable_get(name)
39
+ # This is generally the fast path:
40
+ thread = Thread.current
41
+ unless instance = thread.instance_variable_get(@fiber_local_variable_name)
45
42
  if instance = self.local
46
- thread.thread_variable_set(name, instance)
43
+ thread.instance_variable_set(@fiber_local_variable_name, instance)
47
44
  end
48
45
  end
49
46
 
50
- thread[self.name] = instance
51
-
52
47
  return instance
53
48
  end
54
49
 
55
50
  # Assigns to the fiber-local instance.
56
51
  # @parameter instance [Object] The object that will become the thread-local instance.
57
52
  def instance= instance
58
- thread = Thread.current
59
- thread[self.name] = instance
53
+ Fiber[@fiber_local_attribute_name] = instance
60
54
  end
61
55
  end
62
56
  end
data/license.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright, 2020-2024, by Samuel Williams.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/readme.md ADDED
@@ -0,0 +1,82 @@
1
+ # Fiber::Local
2
+
3
+ A module to simplify fiber-local state.
4
+
5
+ [![Development Status](https://github.com/socketry/fiber-local/workflows/Test/badge.svg)](https://github.com/socketry/fiber-local/actions?workflow=Test)
6
+
7
+ ## Features
8
+
9
+ - Easily access fiber-local state from a fiber.
10
+ - Default to shared thread-local state.
11
+
12
+ ## Installation
13
+
14
+ ``` bash
15
+ $ bundle add fiber-local
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ In your own class, e.g. `Logger`:
21
+
22
+ ``` ruby
23
+ class Logger
24
+ extend Fiber::Local
25
+
26
+ def initialize
27
+ @buffer = []
28
+ end
29
+
30
+ def log(*arguments)
31
+ @buffer << arguments
32
+ end
33
+ end
34
+ ```
35
+
36
+ Now, instead of instantiating your cache `LOGGER = Logger.new`, use `Logger.instance`. It will return a thread-local instance.
37
+
38
+ ``` ruby
39
+ Thread.new do
40
+ Logger.instance
41
+ # => #<Logger:0x000055a14ec6be80>
42
+ end
43
+
44
+ Thread.new do
45
+ Logger.instance
46
+ # => #<Logger:0x000055a14ec597d0>
47
+ end
48
+ ```
49
+
50
+ 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:
51
+
52
+ ``` ruby
53
+ Logger.instance
54
+ # => #<Logger:0x000055a14ec6be80>
55
+
56
+ Fiber.new do
57
+ Logger.instance = Logger.new
58
+ # => #<Logger:0x000055a14ec597d0>
59
+ end
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ We welcome contributions to this project.
65
+
66
+ 1. Fork it.
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
69
+ 4. Push to the branch (`git push origin my-new-feature`).
70
+ 5. Create new Pull Request.
71
+
72
+ ### Developer Certificate of Origin
73
+
74
+ 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.
75
+
76
+ ### Contributor Covenant
77
+
78
+ This project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.
79
+
80
+ ## See Also
81
+
82
+ - [thread-local](https://github.com/socketry/thread-local) — Strictly thread-local variables.
data.tar.gz.sig ADDED
Binary file
metadata CHANGED
@@ -1,51 +1,52 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiber-local
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain: []
11
- date: 2020-10-28 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
14
+ ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
15
+ CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
16
+ MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
17
+ MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
18
+ bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
19
+ igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
20
+ 9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
21
+ sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
22
+ e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN
23
+ XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss
24
+ RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn
25
+ tUeT3ZsAgt0JnGqNm2Bkz81kG4A2hSyFZTFA8vZGhp+hz+8Q573tAR89y9YJBdYM
26
+ zp0FM4zwMNEUwgfRzv1tEVVUEXmoFCyhzonUUw4nE4CFu/sE3ffhjKcXcY//qiSW
27
+ xm4erY3XAgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
28
+ BBYEFO9t7XWuFf2SKLmuijgqR4sGDlRsMC4GA1UdEQQnMCWBI3NhbXVlbC53aWxs
29
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWBI3NhbXVlbC53aWxs
30
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEBCwUAA4IBgQB5sxkE
31
+ cBsSYwK6fYpM+hA5B5yZY2+L0Z+27jF1pWGgbhPH8/FjjBLVn+VFok3CDpRqwXCl
32
+ xCO40JEkKdznNy2avOMra6PFiQyOE74kCtv7P+Fdc+FhgqI5lMon6tt9rNeXmnW/
33
+ c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp
34
+ 8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws
35
+ JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP
36
+ eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt
37
+ Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
+ voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
+ -----END CERTIFICATE-----
40
+ date: 2024-05-02 00:00:00.000000000 Z
12
41
  dependencies:
13
42
  - !ruby/object:Gem::Dependency
14
- name: bundler
43
+ name: fiber-storage
15
44
  requirement: !ruby/object:Gem::Requirement
16
45
  requirements:
17
46
  - - ">="
18
47
  - !ruby/object:Gem::Version
19
48
  version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: covered
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rspec
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
+ type: :runtime
49
50
  prerelease: false
50
51
  version_requirements: !ruby/object:Gem::Requirement
51
52
  requirements:
@@ -60,10 +61,13 @@ extra_rdoc_files: []
60
61
  files:
61
62
  - lib/fiber/local.rb
62
63
  - lib/fiber/local/version.rb
64
+ - license.md
65
+ - readme.md
63
66
  homepage: https://github.com/socketry/fiber-local
64
67
  licenses:
65
68
  - MIT
66
- metadata: {}
69
+ metadata:
70
+ source_code_uri: https://github.com/socketry/fiber-local.git
67
71
  post_install_message:
68
72
  rdoc_options: []
69
73
  require_paths:
@@ -72,14 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
76
  requirements:
73
77
  - - ">="
74
78
  - !ruby/object:Gem::Version
75
- version: 2.5.0
79
+ version: '3.1'
76
80
  required_rubygems_version: !ruby/object:Gem::Requirement
77
81
  requirements:
78
82
  - - ">="
79
83
  - !ruby/object:Gem::Version
80
84
  version: '0'
81
85
  requirements: []
82
- rubygems_version: 3.1.2
86
+ rubygems_version: 3.5.3
83
87
  signing_key:
84
88
  specification_version: 4
85
89
  summary: Provides a class-level mixin to make fiber local state easy.
metadata.gz.sig ADDED
Binary file