shikibu 0.4.0 → 0.4.1

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: cbb66011f1639dc9b15ce02aee8a2cc9fd9d1039004621817e8ce58df5f9dff3
4
- data.tar.gz: d573acc5e190480e7825f7373a357b029456def581c340b0d6f4de95e3b58587
3
+ metadata.gz: d4fceb6e4ad9c8a4a1c5c3ba6dc682fe5f68f31933581add87e600da5d04a8f7
4
+ data.tar.gz: 34c21641626f7de81ff63bc3c21c0cfdda794cc755b259a591152070871f6faf
5
5
  SHA512:
6
- metadata.gz: eacc89618a9b72ff69ae7777031b0c1f10554728f34a16869cafc0d38c03c9507ee5d4c8b4b87f4b983bd5d059f24fd3fd6ef769c625f0a4ef055c86770ec780
7
- data.tar.gz: fb7a2a5acd71407de4b295c5fc23dd2afc815eb87aef002c55e1777ed9110d78bd1c7c04c5805599c3abd199873aafd391a6a9d8db9d4fc924f1c5baac97af36
6
+ metadata.gz: 13fdb9b3d467458478f61a49fb31d3da0ca449a75a34392ba0b5cdba4555584e3cba2325778ec73b2813c8dfa34d5845be46220636ce9b88b9d229052f2178a3
7
+ data.tar.gz: 432aa0c50717dc9fdbf1ab5d5c770952e4e62e12611bf88e39e22c7d3d5424f793e1d6a28611d0a3bde6d64b41570bfc99eb4535ef3c4905e5c37d85d77d1319
data/lib/shikibu/app.rb CHANGED
@@ -100,13 +100,13 @@ module Shikibu
100
100
 
101
101
  @running = true
102
102
 
103
- # Start notify listener before worker
104
- @notify_listener&.start
105
-
106
103
  @worker = Worker.new(self)
107
104
 
108
- # Register notification handlers
105
+ # Register the notification handlers before the listener connects: it
106
+ # LISTENs on them while connecting, before its thread owns the
107
+ # connection (a libpq connection must not be used from two threads).
109
108
  register_notify_handlers if @notify_listener
109
+ @notify_listener&.start
110
110
 
111
111
  @worker.start
112
112
  end
@@ -31,6 +31,7 @@ module Shikibu
31
31
  @running = false
32
32
  @listen_thread = nil
33
33
  @mutex = Mutex.new
34
+ @pending_commands = [] # [:listen | :unlisten, channel] for the listen thread
34
35
  @reconnect_count = 0
35
36
  @executor = Concurrent::ThreadPoolExecutor.new(
36
37
  min_threads: 1,
@@ -73,7 +74,14 @@ module Shikibu
73
74
  @callbacks[channel] ||= []
74
75
  @callbacks[channel] << callback
75
76
 
76
- listen_to_channel(channel) if connected?
77
+ # While the listen thread runs it owns the connection: it runs the
78
+ # LISTEN before its next wait. Before start, connecting LISTENs on
79
+ # every registered channel.
80
+ if listen_thread_running?
81
+ @pending_commands << [:listen, channel]
82
+ elsif connected?
83
+ listen_to_channel(channel)
84
+ end
77
85
  end
78
86
  end
79
87
 
@@ -82,7 +90,11 @@ module Shikibu
82
90
  def unsubscribe(channel)
83
91
  @mutex.synchronize do
84
92
  @callbacks.delete(channel)
85
- unlisten_from_channel(channel) if connected?
93
+ if listen_thread_running?
94
+ @pending_commands << [:unlisten, channel]
95
+ elsif connected?
96
+ unlisten_from_channel(channel)
97
+ end
86
98
  end
87
99
  end
88
100
 
@@ -99,7 +111,7 @@ module Shikibu
99
111
  @connection.setnonblocking(true)
100
112
 
101
113
  # Subscribe to all registered channels
102
- @callbacks.each_key { |channel| listen_to_channel(channel) }
114
+ @mutex.synchronize { @callbacks.keys }.each { |channel| listen_to_channel(channel) }
103
115
 
104
116
  @reconnect_count = 0
105
117
  log_info('Connected to PostgreSQL')
@@ -164,6 +176,7 @@ module Shikibu
164
176
 
165
177
  def listen_for_notifications
166
178
  while @running && connected?
179
+ run_pending_commands
167
180
  # Wait for notifications with 1 second timeout
168
181
  # This allows checking @running flag periodically
169
182
  @connection.wait_for_notify(1) do |channel, _pid, payload|
@@ -172,6 +185,18 @@ module Shikibu
172
185
  end
173
186
  end
174
187
 
188
+ # LISTEN/UNLISTEN requested by other threads (see #subscribe)
189
+ def run_pending_commands
190
+ commands = @mutex.synchronize { @pending_commands.slice!(0..) }
191
+ commands.each do |command, channel|
192
+ command == :listen ? listen_to_channel(channel) : unlisten_from_channel(channel)
193
+ end
194
+ end
195
+
196
+ def listen_thread_running?
197
+ !@listen_thread.nil? && @listen_thread.alive? && !Thread.current.equal?(@listen_thread)
198
+ end
199
+
175
200
  def dispatch_notification(channel, payload)
176
201
  callbacks = @mutex.synchronize { @callbacks[channel]&.dup }
177
202
  return unless callbacks
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Shikibu
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shikibu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yasushi Itoh