Class: PgVersions::ConnectionInner
- Inherits:
-
Object
- Object
- PgVersions::ConnectionInner
- Defined in:
- lib/pg_versions/pg_versions.rb
Instance Method Summary collapse
- #bump(channels) ⇒ Object
- #bump_nonblock(channels) ⇒ Object
- #close ⇒ Object
- #get_channels ⇒ Object
-
#initialize ⇒ ConnectionInner
constructor
A new instance of ConnectionInner.
- #is_closing ⇒ Object
- #notify(channel, version) ⇒ Object
- #process ⇒ Object
- #read(channels) ⇒ Object
- #subscribe(subscriber, channels) ⇒ Object
- #taking_bumps ⇒ Object
- #taking_reads ⇒ Object
- #unsubscribe(subscriber, channels) ⇒ Object
- #wake_processor ⇒ Object
Constructor Details
#initialize ⇒ ConnectionInner
Returns a new instance of ConnectionInner.
202 203 204 205 206 207 208 209 210 |
# File 'lib/pg_versions/pg_versions.rb', line 202 def initialize() @mutex = Mutex.new @command_notify_w = nil @subscriptions = {} @bumps = [] @reads = [] @closers = [] @state = :idle # idle, processing, closing, closed end |
Instance Method Details
#bump(channels) ⇒ Object
291 292 293 294 295 296 297 298 299 |
# File 'lib/pg_versions/pg_versions.rb', line 291 def bump(channels) result = Queue.new @mutex.synchronize { raise ConnectionClosed if @state == :closing || @state == :closed @bumps << [result, channels] } wake_processor result.pop end |
#bump_nonblock(channels) ⇒ Object
302 303 304 305 306 307 308 309 |
# File 'lib/pg_versions/pg_versions.rb', line 302 def bump_nonblock(channels) @mutex.synchronize { raise ConnectionClosed if @state == :closing || @state == :closed @bumps << [nil, channels] } wake_processor nil end |
#close ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/pg_versions/pg_versions.rb', line 356 def close result = Queue.new @mutex.synchronize { case @state when :idle @state = :closed return when :processing @state = :closing @closers << result wake_processor when :closing @closers << result when :closed return end } result.pop end |
#get_channels ⇒ Object
259 260 261 262 263 |
# File 'lib/pg_versions/pg_versions.rb', line 259 def get_channels @mutex.synchronize { return @subscriptions.keys } end |
#is_closing ⇒ Object
349 350 351 352 353 |
# File 'lib/pg_versions/pg_versions.rb', line 349 def is_closing @mutex.synchronize { return @state == :closing } end |
#notify(channel, version) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/pg_versions/pg_versions.rb', line 266 def notify(channel, version) @mutex.synchronize { (@subscriptions[channel] or []).each { |subscriber| subscriber.notify({ channel => version }) } } end |
#process ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/pg_versions/pg_versions.rb', line 213 def process Thread.handle_interrupt(Object => :never) do command_notify_r = nil @mutex.synchronize { case @state when :idle @state = :processing when :processing raise "Attempt to run processing on a connection that is already being processed" when :closing, :closed return end } begin command_notify_r, @command_notify_w = IO.pipe Thread.handle_interrupt(Object => :immediate) { yield command_notify_r } ensure @mutex.synchronize { command_notify_r&.close @command_notify_w&.close @command_notify_w = nil case @state when :idle, :closed raise "'processor exit in #{@state} state. Please inform the developer of this gem." when :processing @state = :idle when :closing @state = :closed @closers.each { |closer| closer.push true } end } end end end |
#read(channels) ⇒ Object
312 313 314 315 316 317 318 319 320 |
# File 'lib/pg_versions/pg_versions.rb', line 312 def read(channels) result = Queue.new @mutex.synchronize { raise ConnectionClosed if @state == :closing || @state == :closed @reads << [result, channels] } wake_processor result.pop end |
#subscribe(subscriber, channels) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/pg_versions/pg_versions.rb', line 323 def subscribe(subscriber, channels) @mutex.synchronize { raise ConnectionClosed if @state == :closing || @state == :closed channels.each { |channel| @subscriptions[channel] = [] if @subscriptions[channel].nil? @subscriptions[channel].push(subscriber) } } subscriber.notify(read(channels)) # this runs wake_processor, so not doing it explicitly true end |
#taking_bumps ⇒ Object
275 276 277 278 279 280 |
# File 'lib/pg_versions/pg_versions.rb', line 275 def taking_bumps @mutex.synchronize { yield @bumps @bumps = [] } end |
#taking_reads ⇒ Object
283 284 285 286 287 288 |
# File 'lib/pg_versions/pg_versions.rb', line 283 def taking_reads @mutex.synchronize { yield @reads @reads = [] } end |
#unsubscribe(subscriber, channels) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/pg_versions/pg_versions.rb', line 336 def unsubscribe(subscriber, channels) @mutex.synchronize { raise ConnectionClosed if @state == :closing || @state == :closed channels.each { |channel| @subscriptions[channel].delete(subscriber) @subscriptions.delete(channel) if @subscriptions[channel].size == 0 } } wake_processor true end |
#wake_processor ⇒ Object
253 254 255 256 |
# File 'lib/pg_versions/pg_versions.rb', line 253 def wake_processor @command_notify_w&.write('!') @command_notify_w&.flush end |