Class: Yrpc::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/yrpc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yrpc/server.rb', line 4

def initialize(options = {})
  @options = options || {}
  @interceptors = options.fetch(:interceptor_registry, Yrpc.interceptors)
  @interceptors = Yrpc::Interceptors::Registry.new unless @interceptors.is_a?(Yrpc::Interceptors::Registry)
  @services = []
  @started = false
  @stop_server = false
  @stop_server_cv = ConditionVariable.new
  @stop_server_mu = Monitor.new
  @server_mu = Monitor.new
  @hostname = options.fetch(:hostname, Yrpc.server_binding_url)
  load_models
  load_controllers
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/yrpc/server.rb', line 19

def options
  @options
end

Instance Method Details

#add_service(service) ⇒ Object



39
40
41
# File 'lib/yrpc/server.rb', line 39

def add_service(service)
  @services << service unless @services.include?(service)
end

#controllers_pathObject



21
22
23
# File 'lib/yrpc/server.rb', line 21

def controllers_path
  options.fetch(:controllers_path, Yrpc.controllers_path)
end

#grpc_serverObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yrpc/server.rb', line 25

def grpc_server
  # 互斥锁 操作相同的资源
  @server_mu.synchronize do
    @server ||= begin
      server = GRPC::RpcServer.new(@options)
      @port = server.add_http2_port(@hostname, ssl_credentials)
      @services.each do |service|
        server.handle(service)
      end
      server
    end
  end
end

#load_controllersObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/yrpc/server.rb', line 54

def load_controllers
  return unless File.directory?(controllers_path)
  path = File.realpath(controllers_path)
  $LOAD_PATH.unshift(path)
  p path
  Dir["#{path}/**/*.rb"].each do |f|
    # 把pb文件给exclue
    next if f.include?('_pb')
    Yrpc.logger.info "- Loading gRPC service file: #{f}"
    load File.realpath(f)
  end
end

#load_modelsObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yrpc/server.rb', line 67

def load_models
  return unless File.directory?("app/models")
  path = File.realpath("app/models")
  $LOAD_PATH.unshift(path)
  Dir["#{path}/**/*.rb"].each do |f|
    # 把pb文件给exclue
    next if f.include?('_pb')
    Yrpc.logger.info "- Loading gRPC Model file: #{f}"
    load File.realpath(f)
  end
end

#ssl_credentialsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/yrpc/server.rb', line 43

def ssl_credentials
  if options.fetch(:use_ssl, Yrpc.use_ssl)
    private_key = File.read(options.fetch(:ssl_key_file, Yrpc.ssl_key_file))
    cert_chain = File.read(options.fetch(:ssl_crt_file, Yrpc.ssl_crt_file))
    certs = [nil, [{private_key: private_key, cert_chain: cert_chain}], false]
    GRPC::Core::ServerCredentials.new(*certs)
  else
    :this_port_is_insecure
  end
end