Module: PG::EM::Client::Watcher

Defined in:
lib/pg/em/client/watcher.rb

Overview

This module is used as a handler to ::EM.watch connection socket and it extracts query results in a non-blocking manner.

Author

Rafal Michalski

Instance Method Summary collapse

Instance Method Details

#cancel_notify_timerObject



79
80
81
82
83
84
# File 'lib/pg/em/client/watcher.rb', line 79

def cancel_notify_timer
  if @notify_timer
    @notify_timer.cancel
    @notify_timer = nil
  end
end

#cancel_timerObject



86
87
88
89
90
91
# File 'lib/pg/em/client/watcher.rb', line 86

def cancel_timer
  if @timer
    @timer.cancel
    @timer = nil
  end
end

#check_notifyObject



102
103
104
105
106
107
# File 'lib/pg/em/client/watcher.rb', line 102

def check_notify
  if notify_hash = @client.notifies
    cancel_notify_timer
    succeed_notify notify_hash
  end
end

#fetch_resultsObject

Carefully extract results without blocking the EventMachine reactor.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/pg/em/client/watcher.rb', line 111

def fetch_results
  result = false
  until @client.is_busy
    single_result = @client.blocking_get_result
    if one_result_mode?
      result = single_result
      break
    elsif single_result.nil?
      if result = @last_result
        result.check
      end
      break
    end
    @last_result.clear if @last_result
    @last_result = single_result
  end
rescue Exception => e
  handle_error e
else
  if result == false
    @readable_timestamp = Time.now if @timer
  else
    cancel_timer
    self.notify_readable = false unless @notify_deferrable
    df = @deferrable
    @deferrable = @send_proc = nil
    df.succeed result
  end
end

#initialize(client) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/pg/em/client/watcher.rb', line 11

def initialize(client)
  @client = client
  @is_connected = true
  @one_result_mode = false
  @deferrable = nil
  @notify_deferrable = nil
  @timer = nil
  @notify_timer = nil
end

#notify_readableObject



93
94
95
96
97
98
99
100
# File 'lib/pg/em/client/watcher.rb', line 93

def notify_readable
  @client.consume_input
rescue Exception => e
  handle_error e
else
  fetch_results if @deferrable
  check_notify if @notify_deferrable
end

#one_result_mode?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/pg/em/client/watcher.rb', line 25

def one_result_mode?
  @one_result_mode
end

#setup_timer(timeout, adjustment = 0) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pg/em/client/watcher.rb', line 58

def setup_timer(timeout, adjustment = 0)
  @timer = ::EM::Timer.new(timeout - adjustment) do
    if (last_interval = Time.now - @readable_timestamp) >= timeout
      @timer = nil
      cancel_notify_timer
      self.notify_readable = false
      @client.async_command_aborted = true
      @send_proc = nil
      begin
        @client.raise_error ConnectionBad, "query timeout expired (async)"
      rescue Exception => e
        fail_result e
        # notify should also fail: query timeout is like connection error
        fail_notify e
      end
    else
      setup_timer timeout, last_interval
    end
  end
end

#unbindObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pg/em/client/watcher.rb', line 141

def unbind
  @is_connected = false
  cancel_timer
  cancel_notify_timer
  if @deferrable || @notify_deferrable
    @client.raise_error ConnectionBad, "connection reset"
  end
rescue Exception => e
  fail_result e
  fail_notify e
end

#watch_notify(deferrable, timeout = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg/em/client/watcher.rb', line 43

def watch_notify(deferrable, timeout = nil)
  notify_df = @notify_deferrable
  @notify_deferrable = deferrable
  cancel_notify_timer
  self.notify_readable = true unless notify_readable?
  if timeout
    @notify_timer = ::EM::Timer.new(timeout) do
      @notify_timer = nil
      succeed_notify
    end
  end
  notify_df.fail nil if notify_df
  check_notify
end

#watch_results(deferrable, send_proc = nil, one_result_mode = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pg/em/client/watcher.rb', line 29

def watch_results(deferrable, send_proc = nil, one_result_mode = false)
  @one_result_mode = one_result_mode
  @last_result = nil
  @deferrable = deferrable
  @send_proc = send_proc
  cancel_timer
  self.notify_readable = true unless notify_readable?
  if (timeout = @client.query_timeout) > 0
    @readable_timestamp = Time.now
    setup_timer timeout
  end
  fetch_results
end

#watching?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/pg/em/client/watcher.rb', line 21

def watching?
  @is_connected
end