Class: LambdaServer

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_lambda_ric/lambda_server.rb

Constant Summary collapse

LAMBDA_DEFAULT_SERVER_ADDRESS =
'127.0.0.1:9001'
LAMBDA_RUNTIME_API_VERSION =
'2018-06-01'
MAX_HEADER_SIZE =
1024 * 1024
LONG_TIMEOUT =
1_000_000

Instance Method Summary collapse

Constructor Details

#initialize(server_address) ⇒ LambdaServer

Returns a new instance of LambdaServer.



16
17
18
19
# File 'lib/aws_lambda_ric/lambda_server.rb', line 16

def initialize(server_address)
  server_address ||= LAMBDA_DEFAULT_SERVER_ADDRESS
  @server_address = 'http://' + server_address + '/' + LAMBDA_RUNTIME_API_VERSION
end

Instance Method Details

#next_invocationObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aws_lambda_ric/lambda_server.rb', line 21

def next_invocation
  next_invocation_uri = URI(@server_address + '/runtime/invocation/next')
  begin
    http = Net::HTTP.new(next_invocation_uri.host, next_invocation_uri.port)
    http.read_timeout = LONG_TIMEOUT
    resp = http.start do |connection|
      connection.get(next_invocation_uri.path)
    end
    if resp.is_a?(Net::HTTPSuccess)
      request_id = resp['Lambda-Runtime-Aws-Request-Id']
      [request_id, resp]
    else
      raise LambdaErrors::InvocationError.new(
          "Received #{resp.code} when waiting for next invocation."
      )
    end
  rescue LambdaErrors::InvocationError => e
    raise e
  rescue StandardError => e
    raise LambdaErrors::InvocationError.new(e)
  end
end

#send_error_response(request_id:, error_object:, error:, xray_cause:) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aws_lambda_ric/lambda_server.rb', line 61

def send_error_response(request_id:, error_object:, error:, xray_cause:)
  response_uri = URI(@server_address + "/runtime/invocation/#{request_id}/error")
  begin
    headers = { 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type }
    headers['Lambda-Runtime-Function-XRay-Error-Cause'] = xray_cause if xray_cause.bytesize < MAX_HEADER_SIZE
    Net::HTTP.post(
        response_uri,
        error_object.to_json,
        headers
    )
  rescue StandardError => e
    raise LambdaErrors::LambdaRuntimeError.new(e)
  end
end

#send_init_error(error_object:, error:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/aws_lambda_ric/lambda_server.rb', line 76

def send_init_error(error_object:, error:)
  uri = URI("#{@server_address}/runtime/init/error")
  begin
    Net::HTTP.post(
        uri,
        error_object.to_json,
        { 'Lambda-Runtime-Function-Error-Type' => error.runtime_error_type }
    )
  rescue StandardError => e
    raise LambdaErrors::LambdaRuntimeInitError.new(e)
  end
end

#send_response(request_id:, response_object:, content_type: 'application/json') ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/aws_lambda_ric/lambda_server.rb', line 44

def send_response(request_id:, response_object:, content_type: 'application/json')
  response_uri = URI(@server_address + "/runtime/invocation/#{request_id}/response")
  begin
    # unpack IO at this point
    if content_type == 'application/unknown'
      response_object = response_object.read
    end
    Net::HTTP.post(
        response_uri,
        response_object,
        { 'Content-Type' => content_type }
    )
  rescue StandardError => e
    raise LambdaErrors::LambdaRuntimeError.new(e)
  end
end