Class: RailsGrpc::GeneralServer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port:, pool_size: nil, logger: Rails.logger) ⇒ GeneralServer

Returns a new instance of GeneralServer.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_grpc/general_server.rb', line 13

def initialize(port:, pool_size: nil, logger: Rails.logger)
  self.port = port
  self.pool_size = pool_size
  self.logger = logger
  self.handlers = []

  self.interceptors = [
    RailsGrpc::Interceptor::LoggingInterceptor.new(RailsGrpc::Logger.logger(logger))
  ]

  self.grpc_server = if pool_size.present?
                       GRPC::RpcServer.new(pool_size: pool_size, interceptors: self.interceptors)
                     else
                       GRPC::RpcServer.new(interceptors: self.interceptors)
                     end
  self.grpc_server.add_http2_port(port, :this_port_is_insecure)
  set_reloader
end

Instance Attribute Details

#grpc_serverObject

Returns the value of attribute grpc_server.



7
8
9
# File 'lib/rails_grpc/general_server.rb', line 7

def grpc_server
  @grpc_server
end

#handlersObject

Returns the value of attribute handlers.



7
8
9
# File 'lib/rails_grpc/general_server.rb', line 7

def handlers
  @handlers
end

#interceptorsObject

Returns the value of attribute interceptors.



7
8
9
# File 'lib/rails_grpc/general_server.rb', line 7

def interceptors
  @interceptors
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/rails_grpc/general_server.rb', line 7

def logger
  @logger
end

#pool_sizeObject

When you would like to use autoreload in development mode, pool_size > 1 reloading is so slow, so you should set pool_size 1.



11
12
13
# File 'lib/rails_grpc/general_server.rb', line 11

def pool_size
  @pool_size
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/rails_grpc/general_server.rb', line 7

def port
  @port
end

Instance Method Details

#runObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_grpc/general_server.rb', line 58

def run
  @logger.info("GRPC server running on #{port}")
  begin
    @grpc_server.run_till_terminated
  rescue SystemExit, Interrupt
    # server does not stop gracefully because of it is a bug of grpc ruby.
    # see: https://github.com/grpc/grpc/issues/14043
    @logger.info("GRPC server goodbye!")
    @grpc_server.stop
  end
end

#set_handlers(handlers) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rails_grpc/general_server.rb', line 50

def set_handlers(handlers)
  hs = Array(handlers)
  hs.each do |h|
    @grpc_server.handle(h)
  end
  @handlers.concat(hs)
end

#set_reloaderObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_grpc/general_server.rb', line 32

def set_reloader
  unless RailsGrpc::Dependencies.cache_classes?
    @grpc_server.application_reload = lambda do
      changed = Rails.application.reloader.check.call
      if changed
        @logger.info("Reloading GRPC Server...")
        Rails.application.reloader.reload!
        @logger.info("Reset GRPC services...")

        @handlers.each do |h|
          reloaded_handler = h.to_s.constantize
          @grpc_server.send(:add_force_rpc_descs_for, reloaded_handler)
        end
      end
    end
  end
end