Class: GrpcKit::Session::ClientSession

Inherits:
DS9::Client
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grpc_kit/session/client_session.rb

Defined Under Namespace

Classes: ConnectionClosing

Constant Summary collapse

MAX_STREAM_ID =
2**31 - 1

Instance Method Summary collapse

Constructor Details

#initialize(io, opts = {}) ⇒ ClientSession

Returns a new instance of ClientSession.



19
20
21
22
23
24
25
26
27
# File 'lib/grpc_kit/session/client_session.rb', line 19

def initialize(io, opts = {})
  super() # initialize DS9::Session

  @io = io
  @streams = {}
  @opts = opts
  @draining = false
  @stop = false
end

Instance Method Details

#run_onceObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/grpc_kit/session/client_session.rb', line 57

def run_once
  return if @stop

  if @draining && @drain_time < Time.now
    raise 'trasport is closing'
  end

  if want_read?
    do_read
  end

  if want_write?
    send
  end
end

#send_request(data, headers) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/grpc_kit/session/client_session.rb', line 29

def send_request(data, headers)
  if @draining
    raise ConnectionClosing, "You can't send new request. becuase this connection will shuting down"
  end

  stream_id = submit_request(headers, data).to_i
  stream = GrpcKit::Session::Stream.new(stream_id: stream_id, send_data: data)
  stream.stream_id = stream_id
  @streams[stream_id] = stream
  stream
end

#start(stream_id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grpc_kit/session/client_session.rb', line 42

def start(stream_id)
  stream = @streams.fetch(stream_id)

  loop do
    if (!want_read? && !want_write?) || stream.close?
      break
    end

    run_once
  end
rescue Errno::ECONNRESET, IOError => e
  GrpcKit.logger.debug(e.message)
  shutdown
end