Class: GrpcKit::Calls::Client::BidiStreamer

Inherits:
GrpcKit::Call show all
Includes:
Enumerable
Defined in:
lib/grpc_kit/calls/client_bidi_streamer.rb

Instance Attribute Summary

Attributes inherited from GrpcKit::Call

#metadata, #method, #method_name, #service_name

Instance Method Summary collapse

Methods inherited from GrpcKit::Call

#deadline

Constructor Details

#initializeBidiStreamer

Returns a new instance of BidiStreamer.



13
14
15
16
17
# File 'lib/grpc_kit/calls/client_bidi_streamer.rb', line 13

def initialize(*)
  super
  @mutex = Mutex.new
  @send = false
end

Instance Method Details

#close_and_sendObject



55
56
57
58
59
# File 'lib/grpc_kit/calls/client_bidi_streamer.rb', line 55

def close_and_send
  @mutex.synchronize do
    @stream.close_and_send
  end
end

#each {|response| ... } ⇒ Object

Yield Parameters:

  • response (Object)

    each response object of bidi streaming RPC



62
63
64
# File 'lib/grpc_kit/calls/client_bidi_streamer.rb', line 62

def each
  loop { yield(recv) }
end

#recvObject

This method not is expected to be call in the main thread where #send_msg is called

Returns:

  • (Object)

    response object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/grpc_kit/calls/client_bidi_streamer.rb', line 36

def recv
  sleep 0.1 until @send

  loop do
    msg = @mutex.synchronize do
      @stream.recv_msg(blocking: false)
    end

    unless msg == :wait_readable
      return msg
    end
  end

  raise StopIteration
rescue GrpcKit::Errors::BadStatus => e
  @reason = e
  raise e
end

#send_msg(data) ⇒ void

This method returns an undefined value.

Parameters:

  • data (Object)

    request message



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grpc_kit/calls/client_bidi_streamer.rb', line 21

def send_msg(data)
  if @reason
    raise "Upstream returns an error status: #{@reason}"
  end

  @mutex.synchronize do
    @stream.send_msg(data, metadata: )
  end

  @send = true
end