prosody 0.5.1 → 0.6.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.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.config/rail.toml +26 -0
  3. data/.release-please-manifest.json +1 -1
  4. data/.ruby-version +1 -1
  5. data/.taplo.toml +1 -1
  6. data/AGENTS.md +29 -15
  7. data/CHANGELOG.md +7 -0
  8. data/CONFIGURATION.md +19 -16
  9. data/Cargo.lock +254 -235
  10. data/Cargo.toml +11 -8
  11. data/README.md +100 -23
  12. data/examples/keyed_state.rb +10 -2
  13. data/examples/keyed_state.rbs +1 -0
  14. data/ext/prosody/Cargo.toml +1 -1
  15. data/ext/prosody/src/admin.rs +36 -36
  16. data/ext/prosody/src/bridge/mod.rs +5 -10
  17. data/ext/prosody/src/client/config/connections.rs +170 -0
  18. data/ext/prosody/src/client/config/middleware.rs +184 -0
  19. data/ext/prosody/src/client/config/mod.rs +396 -0
  20. data/ext/prosody/src/client/config/state.rs +323 -0
  21. data/ext/prosody/src/client/mod.rs +31 -117
  22. data/ext/prosody/src/client/readers.rs +106 -0
  23. data/ext/prosody/src/client/request.rs +2 -2
  24. data/ext/prosody/src/client/support.rs +13 -32
  25. data/ext/prosody/src/gvl.rs +8 -6
  26. data/ext/prosody/src/handler/{context.rs → context/mod.rs} +36 -150
  27. data/ext/prosody/src/handler/context/vending.rs +138 -0
  28. data/ext/prosody/src/handler/message.rs +36 -0
  29. data/ext/prosody/src/handler/mod.rs +18 -8
  30. data/ext/prosody/src/handler/state/deque.rs +144 -0
  31. data/ext/prosody/src/handler/state/mod.rs +163 -268
  32. data/ext/prosody/src/handler/state/query.rs +264 -0
  33. data/ext/prosody/src/handler/state/registration.rs +15 -86
  34. data/ext/prosody/src/handler/state/scan.rs +33 -141
  35. data/ext/prosody/src/handler/state/set.rs +98 -0
  36. data/ext/prosody/src/lib.rs +54 -36
  37. data/ext/prosody/src/logging.rs +6 -6
  38. data/ext/prosody/src/published.rs +171 -152
  39. data/ext/prosody/src/scheduler/result.rs +2 -1
  40. data/ext/prosody/src/util.rs +65 -3
  41. data/lib/prosody/client.rb +32 -0
  42. data/lib/prosody/configuration.rb +20 -12
  43. data/lib/prosody/demand.rb +27 -0
  44. data/lib/prosody/native_stubs/client.rb +157 -0
  45. data/lib/prosody/native_stubs/context.rb +178 -0
  46. data/lib/prosody/native_stubs/message.rb +133 -0
  47. data/lib/prosody/native_stubs.rb +11 -956
  48. data/lib/prosody/state/deque.rb +253 -0
  49. data/lib/prosody/state/map.rb +313 -0
  50. data/lib/prosody/state/set.rb +121 -0
  51. data/lib/prosody/state/value.rb +58 -0
  52. data/lib/prosody/state.rb +157 -677
  53. data/lib/prosody/version.rb +1 -1
  54. data/lib/prosody.rb +2 -1
  55. data/sig/configuration.rbs +19 -10
  56. data/sig/prosody.rbs +37 -4
  57. data/sig/published.rbs +102 -0
  58. data/sig/state.rbs +109 -122
  59. data/typecheck/payload_types.rb +12 -0
  60. data/typecheck/payload_types.rbs +1 -0
  61. metadata +30 -11
  62. data/ext/prosody/src/client/config.rs +0 -1300
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Native stubs for {Prosody::Client}: sending and subscribing.
4
+
5
+ module Prosody
6
+ # Main client for interacting with the Prosody messaging system.
7
+ # Provides methods for sending messages and subscribing to Kafka topics.
8
+ #
9
+ # @see ext/prosody/src/client/mod.rs for implementation
10
+ class Client
11
+ # Creates a new Prosody client with the given configuration.
12
+ #
13
+ # @param config [Hash, Configuration] Client configuration
14
+ # @return [Client] A new client instance
15
+ # @raise [ArgumentError] If the configuration is invalid
16
+ # @raise [RuntimeError] If client initialization fails
17
+ #
18
+ # @example Creating a client with a Configuration object
19
+ # config = Prosody::Configuration.new do |c|
20
+ # c.bootstrap_servers = "localhost:9092"
21
+ # c.group_id = "my-consumer-group"
22
+ # end
23
+ # client = Prosody::Client.new(config)
24
+ #
25
+ # @example Creating a client with a hash
26
+ # client = Prosody::Client.new(
27
+ # bootstrap_servers: "localhost:9092",
28
+ # group_id: "my-consumer-group"
29
+ # )
30
+ def self.new(config)
31
+ raise NotImplementedError, "This method is implemented natively in Rust"
32
+ end
33
+
34
+ # Returns the current state of the consumer.
35
+ #
36
+ # The consumer can be in one of four states:
37
+ # - `:shut_down` - The client is shut down
38
+ # - `:unconfigured` - The consumer has not been configured yet
39
+ # - `:configured` - The consumer is configured but not running
40
+ # - `:running` - The consumer is actively consuming messages
41
+ #
42
+ # @return [Symbol] The current consumer state
43
+ def consumer_state
44
+ raise NotImplementedError, "This method is implemented natively in Rust"
45
+ end
46
+
47
+ # Returns the number of Kafka partitions currently assigned to this consumer.
48
+ #
49
+ # This method can be used to monitor the consumer's workload and ensure
50
+ # proper load balancing across multiple consumer instances.
51
+ #
52
+ # @return [Integer] The number of assigned partitions
53
+ def assigned_partition_count
54
+ raise NotImplementedError, "This method is implemented natively in Rust"
55
+ end
56
+
57
+ # Checks if the consumer is stalled.
58
+ #
59
+ # A stalled consumer is one that has stopped processing messages due to
60
+ # errors or reaching processing limits. This can be used to detect unhealthy
61
+ # consumers that need attention.
62
+ #
63
+ # @return [Boolean] true if the consumer is stalled, false otherwise
64
+ def stalled?
65
+ raise NotImplementedError, "This method is implemented natively in Rust"
66
+ end
67
+
68
+ # Sends a message to the specified Kafka topic.
69
+ #
70
+ # @param topic [String] The destination topic name
71
+ # @param key [String] The message key for partitioning
72
+ # @param payload [Prosody::json_value] The JSON-compatible message payload
73
+ # @return [void]
74
+ # @raise [RuntimeError] If the message cannot be sent
75
+ #
76
+ # @example Sending a simple message
77
+ # client.send_message("my-topic", "user-123", {
78
+ # "event" => "login", "timestamp" => Time.now.to_i
79
+ # })
80
+ def send_message(topic, key, payload)
81
+ raise NotImplementedError, "This method is implemented natively in Rust"
82
+ end
83
+
84
+ # Sends an excise record to the specified Kafka topic.
85
+ #
86
+ # @param topic [String] The destination topic name
87
+ # @param key [String] The message key for partitioning
88
+ # @return [void]
89
+ # @raise [RuntimeError] If the excise record cannot be sent
90
+ def excise(topic, key)
91
+ raise NotImplementedError, "This method is implemented natively in Rust"
92
+ end
93
+
94
+ # Subscribes to Kafka topics using the provided handler.
95
+ # The handler must implement `on_message`, `on_excise`, and `on_timer`.
96
+ #
97
+ # @param handler [EventHandler] A handler object that processes messages
98
+ # @return [void]
99
+ # @raise [ArgumentError] If a required handler method is missing
100
+ # @raise [RuntimeError] If subscription fails
101
+ #
102
+ # @example Subscribing with a handler
103
+ # class MyHandler < Prosody::EventHandler
104
+ # def on_message(context, message)
105
+ # puts "Received message: #{message.payload}"
106
+ # end
107
+ #
108
+ # def on_excise(_context, message)
109
+ # puts "Excised key: #{message.key}"
110
+ # end
111
+ #
112
+ # def on_timer(_context, _timer)
113
+ # end
114
+ # end
115
+ #
116
+ # client.subscribe(MyHandler.new)
117
+ def subscribe(handler)
118
+ raise NotImplementedError, "This method is implemented natively in Rust"
119
+ end
120
+
121
+ # Unsubscribes from all topics, stopping message processing.
122
+ #
123
+ # This method gracefully shuts down the consumer, completing any in-flight
124
+ # messages before stopping.
125
+ #
126
+ # @return [void]
127
+ # @raise [RuntimeError] If unsubscription fails
128
+ #
129
+ def unsubscribe
130
+ raise NotImplementedError, "This method is implemented natively in Rust"
131
+ end
132
+
133
+ # Shuts down the consumer and all client services.
134
+ #
135
+ # @return [void]
136
+ # @raise [RuntimeError] If shutdown fails
137
+ #
138
+ # @example Shutting down a client
139
+ # client.shutdown
140
+ def shutdown
141
+ raise NotImplementedError, "This method is implemented natively in Rust"
142
+ end
143
+
144
+ # Returns the configured source system identifier.
145
+ #
146
+ # The source system is used to identify the originating service or
147
+ # component in produced messages, enabling loop detection.
148
+ #
149
+ # @return [String] The source system identifier
150
+ #
151
+ # @example Getting the source system
152
+ # puts client.source_system # => "my-service"
153
+ def source_system
154
+ raise NotImplementedError, "This method is implemented natively in Rust"
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,178 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Native stubs for {Prosody::Context}: event metadata, cancellation, and timer
4
+ # scheduling.
5
+
6
+ module Prosody
7
+ # Represents the context of a Kafka message, providing metadata and control
8
+ # capabilities for message handling.
9
+ #
10
+ # Instances of this class are created by the native code and passed to your
11
+ # EventHandler's #on_message method.
12
+ #
13
+ # @see ext/prosody/src/handler/context/mod.rs for implementation
14
+ class Context
15
+ # @private
16
+ def initialize
17
+ raise NotImplementedError, "This class is implemented natively in Rust"
18
+ end
19
+
20
+ # Checks if cancellation has been requested.
21
+ #
22
+ # This method can be called within message handlers to detect when the
23
+ # handler should exit. Cancellation includes message-level cancellation
24
+ # (e.g., handler timeout) and partition shutdown. During shutdown,
25
+ # cancellation is delayed until near the end of the shutdown timeout to
26
+ # allow in-flight work to complete.
27
+ #
28
+ # @return [Boolean] true if cancellation has been requested, false otherwise
29
+ #
30
+ # @example Checking for cancellation in a loop
31
+ # def on_message(context, message)
32
+ # items = message.payload["items"]
33
+ # items.each do |item|
34
+ # return if context.should_cancel?
35
+ # process_item(item)
36
+ # end
37
+ # end
38
+ def should_cancel?
39
+ raise NotImplementedError, "This method is implemented natively in Rust"
40
+ end
41
+
42
+ # Returns the demand this handler call serves.
43
+ #
44
+ # A normal delivery has kind +:normal+ and 0 retries. A retry after a
45
+ # failure has kind +:failure+ and a retry count that is 1 on the first
46
+ # retry. The count is an estimate; see {Prosody::Demand}.
47
+ #
48
+ # @return [Prosody::Demand]
49
+ #
50
+ # @example Reading the retry count
51
+ # def on_message(context, message)
52
+ # logger.warn("retry #{context.demand.retries}") if context.demand.failure?
53
+ # end
54
+ def demand
55
+ raise NotImplementedError, "This method is implemented natively in Rust"
56
+ end
57
+
58
+ # Blocks until cancellation is signaled.
59
+ #
60
+ # Cancellation includes message-level cancellation (e.g., handler timeout)
61
+ # and partition shutdown. During shutdown, cancellation is delayed until near
62
+ # the end of the shutdown timeout to allow in-flight work to complete.
63
+ # This method is useful for long-running handlers that need to wait for
64
+ # external events while remaining responsive to cancellation.
65
+ #
66
+ # @return [void]
67
+ #
68
+ # @example Waiting for cancellation
69
+ # def on_message(context, message)
70
+ # # Do some work, then wait for cancellation
71
+ # context.on_cancel
72
+ # end
73
+ def on_cancel
74
+ raise NotImplementedError, "This method is implemented natively in Rust"
75
+ end
76
+
77
+ # Schedules a timer to fire at the specified time.
78
+ #
79
+ # Timers allow you to delay execution or implement timeout behavior within
80
+ # your message handlers. When a timer fires, your handler's #on_timer method
81
+ # will be called with the timer object.
82
+ #
83
+ # @param time [Time] When the timer should fire
84
+ # @return [void]
85
+ # @raise [ArgumentError] If the time is invalid or outside the supported range (1970-2106)
86
+ # @raise [RuntimeError] If timer scheduling fails
87
+ #
88
+ # @example Scheduling a delayed action
89
+ # def on_message(context, message)
90
+ # # Schedule a timer to fire in 30 seconds
91
+ # context.schedule(Time.now + 30)
92
+ # end
93
+ #
94
+ # def on_timer(context, timer)
95
+ # puts "Timer fired for key: #{timer.key}"
96
+ # end
97
+ def schedule(time)
98
+ raise NotImplementedError, "This method is implemented natively in Rust"
99
+ end
100
+
101
+ # Clears all scheduled timers and schedules a new one at the specified time.
102
+ #
103
+ # This is equivalent to calling clear_scheduled followed by schedule, but
104
+ # performed atomically.
105
+ #
106
+ # @param time [Time] When the new timer should fire
107
+ # @return [void]
108
+ # @raise [ArgumentError] If the time is invalid or outside the supported range
109
+ # @raise [RuntimeError] If timer operations fail
110
+ #
111
+ # @example Replacing all timers with a new one
112
+ # def on_message(context, message)
113
+ # # Clear any existing timers and schedule a new one
114
+ # context.clear_and_schedule(Time.now + 60)
115
+ # end
116
+ def clear_and_schedule(time)
117
+ raise NotImplementedError, "This method is implemented natively in Rust"
118
+ end
119
+
120
+ # Unschedules a timer that was scheduled for the specified time.
121
+ #
122
+ # If multiple timers were scheduled for the same time, this will remove one
123
+ # of them. If no timer exists for the specified time, this method does nothing.
124
+ #
125
+ # @param time [Time] The time for which to unschedule the timer
126
+ # @return [void]
127
+ # @raise [ArgumentError] If the time is invalid
128
+ # @raise [RuntimeError] If timer unscheduling fails
129
+ #
130
+ # @example Canceling a specific timer
131
+ # def on_message(context, message)
132
+ # timer_time = Time.now + 30
133
+ # context.schedule(timer_time)
134
+ #
135
+ # # Later, cancel that specific timer
136
+ # context.unschedule(timer_time)
137
+ # end
138
+ def unschedule(time)
139
+ raise NotImplementedError, "This method is implemented natively in Rust"
140
+ end
141
+
142
+ # Clears all scheduled timers.
143
+ #
144
+ # After calling this method, no timers will be scheduled to fire for this
145
+ # message context.
146
+ #
147
+ # @return [void]
148
+ # @raise [RuntimeError] If clearing timers fails
149
+ #
150
+ # @example Canceling all timers
151
+ # def on_message(context, message)
152
+ # context.clear_scheduled
153
+ # end
154
+ def clear_scheduled
155
+ raise NotImplementedError, "This method is implemented natively in Rust"
156
+ end
157
+
158
+ # Returns all currently scheduled timer times.
159
+ #
160
+ # The returned array contains Time objects representing when each scheduled
161
+ # timer will fire. The array may be empty if no timers are scheduled.
162
+ #
163
+ # @return [Array<Time>] Array of scheduled timer times
164
+ # @raise [RuntimeError] If retrieving scheduled times fails
165
+ #
166
+ # @example Checking scheduled timers
167
+ # def on_message(context, message)
168
+ # scheduled_times = context.scheduled
169
+ # puts "#{scheduled_times.length} timers scheduled"
170
+ # scheduled_times.each do |time|
171
+ # puts "Timer will fire at: #{time}"
172
+ # end
173
+ # end
174
+ def scheduled
175
+ raise NotImplementedError, "This method is implemented natively in Rust"
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Native stubs for the event types that reach a handler: {Prosody::Message},
4
+ # {Prosody::ExciseMessage}, and {Prosody::Timer}.
5
+
6
+ module Prosody
7
+ # Represents a Kafka message with its metadata and payload.
8
+ #
9
+ # Instances of this class are created by the native code and passed to your
10
+ # EventHandler's #on_message method. In RBS, +Message[Payload]+ carries the
11
+ # statically declared payload shape; bare +Message+ defaults to
12
+ # +Prosody::json_value+. This annotation does not add runtime validation.
13
+ #
14
+ # @see ext/prosody/src/handler/message.rs for implementation
15
+ class Message
16
+ # Returns the Kafka topic this message was published to.
17
+ #
18
+ # @return [String] The topic name
19
+ def topic
20
+ raise NotImplementedError, "This method is implemented natively in Rust"
21
+ end
22
+
23
+ # Returns the Kafka partition number for this message.
24
+ #
25
+ # @return [Integer] The partition number
26
+ def partition
27
+ raise NotImplementedError, "This method is implemented natively in Rust"
28
+ end
29
+
30
+ # Returns the Kafka offset of this message within its partition.
31
+ #
32
+ # @return [Integer] The message offset
33
+ def offset
34
+ raise NotImplementedError, "This method is implemented natively in Rust"
35
+ end
36
+
37
+ # Returns the message key used for partitioning.
38
+ #
39
+ # @return [String] The message key
40
+ def key
41
+ raise NotImplementedError, "This method is implemented natively in Rust"
42
+ end
43
+
44
+ # Returns the timestamp when the message was created.
45
+ #
46
+ # @return [Time] The message timestamp
47
+ def timestamp
48
+ raise NotImplementedError, "This method is implemented natively in Rust"
49
+ end
50
+
51
+ # Returns the deserialized message payload.
52
+ #
53
+ # The payload is automatically deserialized from JSON to Ruby objects.
54
+ #
55
+ # @return [Payload] The message content
56
+ def payload
57
+ raise NotImplementedError, "This method is implemented natively in Rust"
58
+ end
59
+
60
+ # Returns the source system of the producer that sent this message.
61
+ #
62
+ # @return [String, nil] The source system, or nil when the message has none
63
+ def source_system
64
+ raise NotImplementedError, "This method is implemented natively in Rust"
65
+ end
66
+
67
+ # Reports whether the producer requested a response to this message.
68
+ # Prosody discards the handler result when it is false.
69
+ #
70
+ # @return [Boolean]
71
+ def response_requested?
72
+ raise NotImplementedError, "This method is implemented natively in Rust"
73
+ end
74
+ end
75
+
76
+ # An excise record with Kafka metadata and no payload.
77
+ class ExciseMessage
78
+ # @return [String] The topic name
79
+ def topic = raise NotImplementedError, "This method is implemented natively in Rust"
80
+
81
+ # @return [Integer] The partition number
82
+ def partition = raise NotImplementedError, "This method is implemented natively in Rust"
83
+
84
+ # @return [Integer] The message offset
85
+ def offset = raise NotImplementedError, "This method is implemented natively in Rust"
86
+
87
+ # @return [String] The message key
88
+ def key = raise NotImplementedError, "This method is implemented natively in Rust"
89
+
90
+ # @return [Time] The record timestamp
91
+ def timestamp = raise NotImplementedError, "This method is implemented natively in Rust"
92
+
93
+ # @return [String, nil] The producer's source system, or nil when the record has none
94
+ def source_system = raise NotImplementedError, "This method is implemented natively in Rust"
95
+
96
+ # @return [Boolean] Whether the producer requested a response to this record
97
+ def response_requested? = raise NotImplementedError, "This method is implemented natively in Rust"
98
+ end
99
+
100
+ # Represents a timer that was scheduled to fire at a specific time.
101
+ #
102
+ # Timer instances are created by the native code and passed to your
103
+ # EventHandler's #on_timer method when a scheduled timer fires.
104
+ #
105
+ # @see ext/prosody/src/handler/trigger.rs for implementation
106
+ class Timer
107
+ # @private
108
+ def initialize
109
+ raise NotImplementedError, "This class is implemented natively in Rust"
110
+ end
111
+
112
+ # Returns the entity key identifying what this timer belongs to.
113
+ #
114
+ # The key is typically the same as the message key that was being processed
115
+ # when the timer was scheduled.
116
+ #
117
+ # @return [String] The entity key
118
+ def key
119
+ raise NotImplementedError, "This method is implemented natively in Rust"
120
+ end
121
+
122
+ # Returns the time when this timer was scheduled to fire.
123
+ #
124
+ # Note: Due to CompactDateTime's second-level precision, the returned time
125
+ # will have zero nanoseconds even if the original scheduled time had
126
+ # sub-second precision.
127
+ #
128
+ # @return [Time] The scheduled execution time
129
+ def time
130
+ raise NotImplementedError, "This method is implemented natively in Rust"
131
+ end
132
+ end
133
+ end