Class: GRPC::RpcServer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_grpc/extension/rpc_server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#application_reloadObject

Returns the value of attribute application_reload.



5
6
7
# File 'lib/rails_grpc/extension/rpc_server.rb', line 5

def application_reload
  @application_reload
end

Instance Method Details

#add_force_rpc_descs_for(service) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_grpc/extension/rpc_server.rb', line 51

def add_force_rpc_descs_for(service)
  cls = service.is_a?(Class) ? service : service.class
  specs, handlers = (@rpc_descs ||= {}), (@rpc_handlers ||= {})
  cls.rpc_descs.each_pair do |name, spec|
    route = "/#{cls.service_name}/#{name}".to_sym
    # fail "already registered: rpc #{route} from #{spec}" if specs.key? route
    specs[route] = spec
    rpc_name = GenericService.underscore(name.to_s).to_sym
    if service.is_a?(Class)
      handlers[route] = cls.new.method(rpc_name)
    else
      handlers[route] = service.method(rpc_name)
    end
    GRPC.logger.info("handling #{route} with #{handlers[route]}")
  end
end

#loop_handle_server_callsObject

handles calls to the server



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_grpc/extension/rpc_server.rb', line 8

def loop_handle_server_calls
  fail 'not started' if running_state == :not_started
  while running_state == :running
    begin
      an_rpc = @server.request_call
      break if (!an_rpc.nil?) && an_rpc.call.nil?
      active_call = new_active_server_call(an_rpc)
      unless active_call.nil?
        @pool.schedule(active_call) do |ac|
          c, mth = ac
          begin
            # extended for reloader
            @application_reload.call unless @application_reload.nil?
            # extended for reloader

            rpc_descs[mth].run_server_method(
              c,
              rpc_handlers[mth],
              @interceptors.build_context
            )
          rescue StandardError
            c.send_status(GRPC::Core::StatusCodes::INTERNAL,
                          'Server handler failed')
          end
        end
      end
    rescue Core::CallError, RuntimeError => e
      # these might happen for various reasons.  The correct behavior of
      # the server is to log them and continue, if it's not shutting down.
      if running_state == :running
        GRPC.logger.warn("server call failed: #{e}")
      end
      next
    end
  end
  # @running_state should be :stopping here
  @run_mutex.synchronize do
    transition_running_state(:stopped)
    GRPC.logger.info("stopped: #{self}")
    @server.close
  end
end