Class: AnyCable::GRPC::Server
- Inherits:
-
Object
- Object
- AnyCable::GRPC::Server
- Defined in:
- lib/anycable/grpc/server.rb,
lib/anycable/grpc_kit/server.rb
Overview
Wrapper over gRPC kit server.
Basic example:
# create new server listening on the loopback interface with 50051 port
server = AnyCable::GrpcKit::Server.new(host: "127.0.0.1:50051")
# run gRPC server in bakground
server.start
# stop server
server.stop
Instance Attribute Summary collapse
-
#grpc_server ⇒ Object
readonly
Returns the value of attribute grpc_server.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#sock ⇒ Object
readonly
Returns the value of attribute sock.
Instance Method Summary collapse
-
#initialize(host:, logger: nil, **options) ⇒ Server
constructor
A new instance of Server.
- #running? ⇒ Boolean
-
#start ⇒ Object
Start gRPC server in background and wait untill it ready to accept connections.
-
#stop ⇒ Object
Stop gRPC server if it’s running.
- #stopped? ⇒ Boolean
- #wait_till_running ⇒ Object
- #wait_till_terminated ⇒ Object
Constructor Details
#initialize(host:, logger: nil, **options) ⇒ Server
Returns a new instance of Server.
32 33 34 35 36 |
# File 'lib/anycable/grpc/server.rb', line 32 def initialize(host:, logger: nil, **) @logger = logger @host = host @grpc_server = build_server(**) end |
Instance Attribute Details
#grpc_server ⇒ Object (readonly)
Returns the value of attribute grpc_server.
30 31 32 |
# File 'lib/anycable/grpc/server.rb', line 30 def grpc_server @grpc_server end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
30 31 32 |
# File 'lib/anycable/grpc/server.rb', line 30 def host @host end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
36 37 38 |
# File 'lib/anycable/grpc_kit/server.rb', line 36 def hostname @hostname end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
36 37 38 |
# File 'lib/anycable/grpc_kit/server.rb', line 36 def port @port end |
#sock ⇒ Object (readonly)
Returns the value of attribute sock.
36 37 38 |
# File 'lib/anycable/grpc_kit/server.rb', line 36 def sock @sock end |
Instance Method Details
#running? ⇒ Boolean
69 70 71 |
# File 'lib/anycable/grpc/server.rb', line 69 def running? grpc_server.running_state == :running end |
#start ⇒ Object
Start gRPC server in background and wait untill it ready to accept connections
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/anycable/grpc/server.rb', line 40 def start return if running? raise "Cannot re-start stopped server" if stopped? logger.info "RPC server is starting..." @start_thread = Thread.new { grpc_server.run } grpc_server.wait_till_running logger.info "RPC server is listening on #{host} (workers_num: #{grpc_server.pool_size})" end |
#stop ⇒ Object
Stop gRPC server if it’s running
61 62 63 64 65 66 67 |
# File 'lib/anycable/grpc/server.rb', line 61 def stop return unless running? grpc_server.stop logger.info "RPC server stopped" end |
#stopped? ⇒ Boolean
73 74 75 |
# File 'lib/anycable/grpc/server.rb', line 73 def stopped? grpc_server.running_state == :stopped end |
#wait_till_running ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/anycable/grpc_kit/server.rb', line 87 def wait_till_running raise "Server is not running" unless running? timeout = 5 loop do sock = TCPSocket.new(hostname, port, connect_timeout: 1) if @tls_credentials&.any? sock = OpenSSL::SSL::SSLSocket.new(sock, tls_context) sock.sync_close = true sock.connect end stub = ::Grpc::Health::V1::Health::Stub.new(sock) stub.check(::Grpc::Health::V1::HealthCheckRequest.new) sock.close break rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError, OpenSSL::SSL::SSLError => e timeout -= 1 raise "Server is not responding: #{e}" if timeout.zero? end end |
#wait_till_terminated ⇒ Object
54 55 56 57 58 |
# File 'lib/anycable/grpc/server.rb', line 54 def wait_till_terminated raise "Server is not running" unless running? start_thread.join end |