Class: PgVersions::ConnectionInner

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_versions/pg_versions.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionInner

Returns a new instance of ConnectionInner.



200
201
202
203
204
205
206
207
208
# File 'lib/pg_versions/pg_versions.rb', line 200

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



289
290
291
292
293
294
295
296
297
# File 'lib/pg_versions/pg_versions.rb', line 289

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



300
301
302
303
304
305
306
307
# File 'lib/pg_versions/pg_versions.rb', line 300

def bump_nonblock(channels)
  @mutex.synchronize {
    raise ConnectionClosed  if @state == :closing || @state == :closed
    @bumps << [nil, channels]
  }
  wake_processor
  nil
end

#closeObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/pg_versions/pg_versions.rb', line 354

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_channelsObject



257
258
259
260
261
# File 'lib/pg_versions/pg_versions.rb', line 257

def get_channels
  @mutex.synchronize {
    return @subscriptions.keys
  }
end

#is_closingObject



347
348
349
350
351
# File 'lib/pg_versions/pg_versions.rb', line 347

def is_closing
  @mutex.synchronize {
    return @state == :closing
  }
end

#notify(channel, version) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/pg_versions/pg_versions.rb', line 264

def notify(channel, version)
  @mutex.synchronize {
    (@subscriptions[channel] or []).each { |subscriber|
      subscriber.notify({ channel => version })
    }
  }
end

#processObject



211
212
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
# File 'lib/pg_versions/pg_versions.rb', line 211

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



310
311
312
313
314
315
316
317
318
# File 'lib/pg_versions/pg_versions.rb', line 310

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



321
322
323
324
325
326
327
328
329
330
331
# File 'lib/pg_versions/pg_versions.rb', line 321

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_bumpsObject



273
274
275
276
277
278
# File 'lib/pg_versions/pg_versions.rb', line 273

def taking_bumps
  @mutex.synchronize {
    yield @bumps
    @bumps = []
  }
end

#taking_readsObject



281
282
283
284
285
286
# File 'lib/pg_versions/pg_versions.rb', line 281

def taking_reads
  @mutex.synchronize {
    yield @reads
    @reads = []
  }
end

#unsubscribe(subscriber, channels) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/pg_versions/pg_versions.rb', line 334

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_processorObject



251
252
253
254
# File 'lib/pg_versions/pg_versions.rb', line 251

def wake_processor
  @command_notify_w&.write('!')
  @command_notify_w&.flush
end