Class: THTP::Server

Inherits:
Object
  • Object
show all
Includes:
PubSub, Utils
Defined in:
lib/thtp/server.rb,
lib/thtp/server/pub_sub.rb,
lib/thtp/server/middleware.rb,
lib/thtp/server/instrumentation.rb

Overview

An HTTP (Rack middleware) implementation of Thrift-RPC

Defined Under Namespace

Modules: Instrumentation, Middleware, PubSub

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#args_class, #canonical_name, #deserialize, #deserialize_buffer, #deserialize_stream, #elapsed_ms, #extract_rpcs, #get_time, #jsonify, #result_class, #serialize, #serialize_buffer, #serialize_stream

Methods included from PubSub

#subscribe

Constructor Details

#initialize(app = NullRoute.new, service:, handlers: []) ⇒ Server

Returns a new instance of Server.

Parameters:

  • app (Object?) (defaults to: NullRoute.new)

    The Rack application underneath, if used as middleware

  • service (Thrift::Service)

    The service class handled by this server

  • handlers (Object, Array<Object>) (defaults to: [])

    The object(s) handling RPC requests



26
27
28
29
30
31
# File 'lib/thtp/server.rb', line 26

def initialize(app = NullRoute.new, service:, handlers: [])
  @app = app
  @service = service
  @handler = MiddlewareStack.new(service, handlers)
  @route = %r{^/#{canonical_name(service)}/(?<rpc>[\w.]+)/?$} # /:service/:rpc
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#call(rack_env) ⇒ Object

Rack implementation entrypoint



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/thtp/server.rb', line 39

def call(rack_env)
  start_time = get_time
  # verify routing
  request = Rack::Request.new(rack_env)
  protocol = Encoding.protocol(request.media_type) || Thrift::JsonProtocol
  return @app.call(rack_env) unless request.post? && @route.match(request.path_info)
  # get RPC name from route
  rpc = Regexp.last_match[:rpc]
  raise UnknownRpcError, rpc unless @handler.respond_to?(rpc)
  # read, perform, write
  args = read_args(request.body, rpc, protocol)
  result = @handler.public_send(rpc, *args)
  write_reply(result, rpc, protocol).tap do
    publish :rpc_success,
            request: request, rpc: rpc, args: args, result: result, time: elapsed_ms(start_time)
  end
rescue Thrift::Exception => e # known schema-defined Thrift errors
  write_reply(e, rpc, protocol).tap do
    publish :rpc_exception,
            request: request, rpc: rpc, args: args, exception: e, time: elapsed_ms(start_time)
  end
rescue ServerError => e # known server/communication errors
  write_error(e, protocol).tap do
    publish :rpc_error,
            request: request, rpc: rpc, args: args, error: e, time: elapsed_ms(start_time)
  end
rescue => e # a non-Thrift exception occurred; translate to Thrift as best we can
  write_error(InternalError.new(e), protocol).tap do
    publish :internal_error, request: request, error: e, time: elapsed_ms(start_time)
  end
end

#use(middleware_class, *middleware_args) ⇒ Object

delegate to RPC handler stack



34
35
36
# File 'lib/thtp/server.rb', line 34

def use(middleware_class, *middleware_args)
  @handler.use(middleware_class, *middleware_args)
end