Class: ApnsGatling::Client
- Inherits:
-
Object
- Object
- ApnsGatling::Client
- Defined in:
- lib/apns_gatling/apns_client.rb
Constant Summary collapse
- DRAFT =
'h2'.freeze
Instance Attribute Summary collapse
-
#sandbox ⇒ Object
readonly
Returns the value of attribute sandbox.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#token_maker ⇒ Object
readonly
Returns the value of attribute token_maker.
Instance Method Summary collapse
- #close ⇒ Object
-
#connection ⇒ Object
connection.
- #connection_error(e) ⇒ Object
- #ensure_sent_before_receiving ⇒ Object
-
#ensure_socket_open ⇒ Object
scoket.
- #exit_thread(thread) ⇒ Object
- #host ⇒ Object
- #init_vars ⇒ Object
-
#initialize(team_id, auth_key_id, ecdsa_key, sandbox = false) ⇒ Client
constructor
A new instance of Client.
- #join ⇒ Object
- #new_socket ⇒ Object
- #provider_token ⇒ Object
-
#push(message, &block) ⇒ Object
push message.
- #socket_loop ⇒ Object
Constructor Details
#initialize(team_id, auth_key_id, ecdsa_key, sandbox = false) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 |
# File 'lib/apns_gatling/apns_client.rb', line 14 def initialize(team_id, auth_key_id, ecdsa_key, sandbox = false) @token_maker = Token.new(team_id, auth_key_id, ecdsa_key) @sandbox = sandbox @mutex = Mutex.new @requests = {} @cv = ConditionVariable.new init_vars end |
Instance Attribute Details
#sandbox ⇒ Object (readonly)
Returns the value of attribute sandbox.
12 13 14 |
# File 'lib/apns_gatling/apns_client.rb', line 12 def sandbox @sandbox end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
12 13 14 |
# File 'lib/apns_gatling/apns_client.rb', line 12 def token @token end |
#token_maker ⇒ Object (readonly)
Returns the value of attribute token_maker.
12 13 14 |
# File 'lib/apns_gatling/apns_client.rb', line 12 def token_maker @token_maker end |
Instance Method Details
#close ⇒ Object
204 205 206 207 208 |
# File 'lib/apns_gatling/apns_client.rb', line 204 def close exit_thread(@socket_thread) init_vars @connection = nil end |
#connection ⇒ Object
connection
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/apns_gatling/apns_client.rb', line 128 def connection @connection ||= HTTP2::Client.new.tap do |conn| conn.on(:frame) do |bytes| @mutex.synchronize do @socket.write bytes @socket.flush @first_data_sent = true end end end end |
#connection_error(e) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/apns_gatling/apns_client.rb', line 55 def connection_error(e) @mutex.synchronize do @requests.values.map do | request | block = request[:block] response = request[:response] if block && response response.error_with("connection failed #{e}") block.call response end end @requests = {} @connection = nil end end |
#ensure_sent_before_receiving ⇒ Object
182 183 184 185 186 |
# File 'lib/apns_gatling/apns_client.rb', line 182 def ensure_sent_before_receiving while !@first_data_sent sleep 0.01 end end |
#ensure_socket_open ⇒ Object
scoket
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/apns_gatling/apns_client.rb', line 141 def ensure_socket_open @mutex.synchronize do return if @socket_thread @socket = new_socket @socket_thread = Thread.new do begin socket_loop rescue EOFError init_vars connection_error(SocketError.new('Socket was remotely closed')) rescue Exception => e init_vars connection_error(e) end end end end |
#exit_thread(thread) ⇒ Object
210 211 212 213 214 |
# File 'lib/apns_gatling/apns_client.rb', line 210 def exit_thread(thread) return unless thread thread.exit thread.join end |
#host ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/apns_gatling/apns_client.rb', line 47 def host if sandbox APPLE_DEVELOPMENT_SERVER else APPLE_PRODUCTION_SERVER end end |
#init_vars ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/apns_gatling/apns_client.rb', line 23 def init_vars @mutex.synchronize do @socket.close if @socket && !@socket.closed? @socket = nil @socket_thread = nil @first_data_sent = false @token_generated_at = 0 @blocking = true end end |
#join ⇒ Object
216 217 218 |
# File 'lib/apns_gatling/apns_client.rb', line 216 def join @socket_thread.join if @socket_thread end |
#new_socket ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/apns_gatling/apns_client.rb', line 159 def new_socket ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE # For ALPN support, Ruby >= 2.3 and OpenSSL >= 1.0.2 are required ctx.alpn_protocols = [DRAFT] ctx.alpn_select_cb = lambda do |protocols| DRAFT if protocols.include? DRAFT end tcp = TCPSocket.new(host, 443) socket = OpenSSL::SSL::SSLSocket.new(tcp, ctx) socket.sync_close = true socket.hostname = host socket.connect if socket.alpn_protocol != DRAFT puts "Failed to negotiate #{DRAFT} via ALPN" exit end socket end |
#provider_token ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/apns_gatling/apns_client.rb', line 34 def provider_token = Time.new.to_i if - @token_generated_at > 3550 @mutex.synchronize do @token_generated_at = @token = @token_maker.new_token end @token else @token end end |
#push(message, &block) ⇒ Object
push message
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/apns_gatling/apns_client.rb', line 71 def push(, &block) request = Request.new(, provider_token, host) response = Response.new() @mutex.synchronize do @requests[request.id] = {block: block, response: response} end begin ensure_socket_open stream = connection.new_stream rescue SocketError => e response.error_with("create connection failed #{e}") block.call response return rescue HTTP2::Error::StreamLimitExceeded response.error_with('stream limit exceeded') block.call response return rescue HTTP2::Error::ConnectionClosed close response.error_with('connection closed') block.call response return rescue StandardError => e close response.error_with("standard error #{e}") block.call response return end stream.on(:close) do @mutex.synchronize do @requests.delete request.id @token_generated_at = 0 if response.status == '403' && response.error[:reason] == 'ExpiredProviderToken' if @blocking @blocking = false @cv.signal end block.call response if block end end stream.on(:headers) do |h| hs = Hash[*h.flatten] response.headers.merge!(hs) end stream.on(:data) do |d| response.data << d end stream.headers(request.headers, end_stream: false) stream.data(request.data) @mutex.synchronize { @cv.wait(@mutex, 60) } if @blocking end |
#socket_loop ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/apns_gatling/apns_client.rb', line 188 def socket_loop ensure_sent_before_receiving loop do begin data = @socket.read_nonblock(1024) connection << data # in rescue IO::WaitReadable IO.select([@socket]) retry rescue IO::WaitWritable IO.select(nil, [@socket]) retry end end end |