@@ -10,21 +10,22 @@ class LambdaServer
10
10
LAMBDA_DEFAULT_SERVER_ADDRESS = '127.0.0.1:9001'
11
11
LAMBDA_RUNTIME_API_VERSION = '2018-06-01'
12
12
13
- MAX_HEADER_SIZE = 1024 * 1024
14
- LONG_TIMEOUT = 1_000_000
13
+ MAX_HEADER_SIZE_BYTES = 1024 * 1024
14
+ LONG_TIMEOUT_MS = 1_000_000
15
15
16
- def initialize ( server_address )
16
+ def initialize ( server_address , user_agent )
17
17
server_address ||= LAMBDA_DEFAULT_SERVER_ADDRESS
18
- @server_address = 'http://' + server_address + '/' + LAMBDA_RUNTIME_API_VERSION
18
+ @server_address = "http://#{ server_address } /#{ LAMBDA_RUNTIME_API_VERSION } "
19
+ @user_agent = user_agent
19
20
end
20
21
21
22
def next_invocation
22
23
next_invocation_uri = URI ( @server_address + '/runtime/invocation/next' )
23
24
begin
24
25
http = Net ::HTTP . new ( next_invocation_uri . host , next_invocation_uri . port )
25
- http . read_timeout = LONG_TIMEOUT
26
+ http . read_timeout = LONG_TIMEOUT_MS
26
27
resp = http . start do |connection |
27
- connection . get ( next_invocation_uri . path )
28
+ connection . get ( next_invocation_uri . path , { 'User-Agent' => @user_agent } )
28
29
end
29
30
if resp . is_a? ( Net ::HTTPSuccess )
30
31
request_id = resp [ 'Lambda-Runtime-Aws-Request-Id' ]
@@ -51,7 +52,7 @@ def send_response(request_id:, response_object:, content_type: 'application/json
51
52
Net ::HTTP . post (
52
53
response_uri ,
53
54
response_object ,
54
- { 'Content-Type' => content_type }
55
+ { 'Content-Type' => content_type , 'User-Agent' => @user_agent }
55
56
)
56
57
rescue StandardError => e
57
58
raise LambdaErrors ::LambdaRuntimeError . new ( e )
@@ -61,8 +62,8 @@ def send_response(request_id:, response_object:, content_type: 'application/json
61
62
def send_error_response ( request_id :, error_object :, error :, xray_cause :)
62
63
response_uri = URI ( @server_address + "/runtime/invocation/#{ request_id } /error" )
63
64
begin
64
- headers = { 'Lambda-Runtime-Function-Error-Type' => error . runtime_error_type }
65
- headers [ 'Lambda-Runtime-Function-XRay-Error-Cause' ] = xray_cause if xray_cause . bytesize < MAX_HEADER_SIZE
65
+ headers = { 'Lambda-Runtime-Function-Error-Type' => error . runtime_error_type , 'User-Agent' => @user_agent }
66
+ headers [ 'Lambda-Runtime-Function-XRay-Error-Cause' ] = xray_cause if xray_cause . bytesize < MAX_HEADER_SIZE_BYTES
66
67
Net ::HTTP . post (
67
68
response_uri ,
68
69
error_object . to_json ,
@@ -79,7 +80,7 @@ def send_init_error(error_object:, error:)
79
80
Net ::HTTP . post (
80
81
uri ,
81
82
error_object . to_json ,
82
- { 'Lambda-Runtime-Function-Error-Type' => error . runtime_error_type }
83
+ { 'Lambda-Runtime-Function-Error-Type' => error . runtime_error_type , 'User-Agent' => @user_agent }
83
84
)
84
85
rescue StandardError => e
85
86
raise LambdaErrors ::LambdaRuntimeInitError . new ( e )
0 commit comments