graftcode-context 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 17ba21f24af9e3f7ab5deef351b85df8a9aa7f79bbdc344fdbdcc0c3911b36a8
4
- data.tar.gz: 27e65f9db7f47c44eed230e3caae92056fa620a49d5b54d0228eaf975f763c75
3
+ metadata.gz: f990179ce9ea25ddd8907998f20e1db67d5188b828aac1f6ac91881e63328374
4
+ data.tar.gz: e8d255b32721ea00349cb04122876a3c3f836bb26a5d28060481c30eecec8b0a
5
5
  SHA512:
6
- metadata.gz: 5b673ed072c3e2309224c4e97327b43a2e16eca26ecca0de431878a708547f3a1c743ac33deb58ead5175f89bd83352b80ac2033b3ae5cad2bbef97254a92a75
7
- data.tar.gz: d2e08fdbbc922f36a605bcbad2b5471759b83fd1c19dd8d683bb85db89734263fe65f0bf814325b0074f14aeb57146a3a1fdff936052b0eedf3598ff3fa20b7a
6
+ metadata.gz: 0256517eea84cacf5240692adf5e82282d08cceaf427b7c6707ab881ba90bdf5e3ccf45f065e8bf2502e2d554242afa03c4ae4f6cbb25c19bbbd36a93ee24226
7
+ data.tar.gz: ac54e67da3f5f52268e456cabed2c44f9ef5d365dbb3a11961907b9f809405f13b0f0b9e7ccb9528bd5053bfebd33c76d53c1e95280d2bf7da718adf8d36e1bb
data/README.md CHANGED
@@ -1,3 +1,23 @@
1
1
  # graftcode-context
2
2
 
3
- This package provides context for Graftcode invocations.
3
+ This package provides context for Graftcode invocations. Requires Ruby >= 3.2.
4
+
5
+ The Gateway injects request headers into `RequestContext`. Your handler should only **read** them.
6
+
7
+ ```ruby
8
+ require "graftcode/context"
9
+
10
+ headers = Graftcode::Context::RequestContext.current.get_headers
11
+ auth = headers["authorization"]
12
+ ```
13
+
14
+ - Use `RequestContext.current.get_headers` every time you need headers.
15
+ - The context is read-only for application code: there is no public way to set headers or replace the current context.
16
+ - `get_headers` returns a **copy**. Mutating that hash does not change the live context.
17
+ - Header names arrive in **lowercase** (the Gateway lowercases them for HTTP/2 and WebSocket) and the lookup is case-sensitive: read `authorization`, not `Authorization`.
18
+ - The Gateway forwards all request headers on `/ws`, `/h2` and `/run` (only HTTP/2 pseudo-headers are skipped). A header sent more than once arrives as one value: `cookie` values joined with `"; "`, other headers with `", "`.
19
+ - The context is kept in Fiber storage, per fiber/thread, so concurrent calls do not share it. The host sets every call's headers on the current context and restores the previous headers afterwards.
20
+ - New fibers and threads inherit the headers of the call that starts them; what they change stays in them.
21
+ - A thread that outlives the call keeps that call's headers. Create thread pools and other long-lived workers before handling calls.
22
+
23
+ Full notes: [Request context and headers](../../../docs/request-context.md)
@@ -1,24 +1,97 @@
1
1
  module Graftcode
2
2
  module Context
3
+ # Headers of the current invocation.
4
+ #
5
+ # Rule (same in every runtime since 1.1.0): a child fiber/thread sees the parent's headers as they were
6
+ # when it started; what the child changes stays in the child; parallel invocations share nothing.
7
+ #
8
+ # How: the current context lives in Fiber storage (copied into new fibers and threads on Ruby 3.2+).
9
+ # Header snapshots are never mutated once a context is current; set_headers on a current context stores a
10
+ # new context for this fiber only (copy-on-write). A context that has been current always reads the headers of
11
+ # the fiber/thread that reads it, so an instance kept from an earlier call never shows that call's headers in a
12
+ # later one.
3
13
  class RequestContext
4
- @current = nil
14
+ STORAGE_KEY = :graftcode_request_context
15
+ # Ruby 3.2+: Fiber storage (inherited by fibers and threads started from the current one).
16
+ # Fallback when Fiber storage is not available (the gem requires Ruby >= 3.2): fiber-local Thread#[] storage.
17
+ FIBER_STORAGE = Fiber.respond_to?(:[])
18
+ private_constant :STORAGE_KEY, :FIBER_STORAGE
5
19
 
6
20
  class << self
21
+ # Context of the current invocation. Kept per fiber/thread, so concurrent invocations
22
+ # do not share it. Never nil: a new, empty context is created on first access.
7
23
  def current
8
- @current ||= RequestContext.new
24
+ context = stored || (self.current = RequestContext.new)
25
+ context.send(:publish!)
26
+ context
27
+ end
28
+
29
+ private
30
+
31
+ # Host only: makes the given context current for this fiber/thread.
32
+ def current=(context)
33
+ context&.send(:publish!)
34
+ store(context)
35
+ end
36
+
37
+ def stored
38
+ FIBER_STORAGE ? Fiber[STORAGE_KEY] : Thread.current[STORAGE_KEY]
39
+ end
40
+
41
+ def store(context)
42
+ if FIBER_STORAGE
43
+ Fiber[STORAGE_KEY] = context
44
+ else
45
+ Thread.current[STORAGE_KEY] = context
46
+ end
9
47
  end
10
48
  end
11
49
 
12
50
  def initialize
13
- @headers = {}
51
+ @headers = {}.freeze
52
+ # True once the context has been current somewhere: it may be shared, so no in-place writes.
53
+ @published = false
14
54
  end
15
55
 
16
56
  def get_headers
17
- @headers.dup
57
+ visible_headers.dup
58
+ end
59
+
60
+ protected
61
+
62
+ def headers_snapshot
63
+ @headers
18
64
  end
19
65
 
66
+ private
67
+
68
+ # Host only: replaces the whole header bag (copy-on-write once the context is current).
20
69
  def set_headers(headers)
21
- @headers = headers.to_h
70
+ snapshot = headers.to_h.dup.freeze
71
+ unless @published
72
+ @headers = snapshot
73
+ return
74
+ end
75
+
76
+ successor = RequestContext.new
77
+ successor.send(:derive_from, snapshot)
78
+ self.class.send(:store, successor)
79
+ end
80
+
81
+ def publish!
82
+ @published = true
83
+ end
84
+
85
+ def derive_from(headers)
86
+ @headers = headers
87
+ @published = true
88
+ end
89
+
90
+ def visible_headers
91
+ return @headers unless @published
92
+
93
+ current = self.class.send(:stored)
94
+ current ? current.headers_snapshot : {}.freeze
22
95
  end
23
96
  end
24
97
  end
@@ -1,5 +1,5 @@
1
1
  module Graftcode
2
2
  module Context
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graftcode-context
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GRAFTCODE S.A.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-09-11 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -49,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 3.1.0
52
+ version: 3.2.0
53
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ">="