add allocator parameter

This commit is contained in:
Emil Lerch 2023-08-01 19:42:48 -07:00
parent 28e9944596
commit b69bc44aaf
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 10 additions and 8 deletions

View File

@ -2,7 +2,9 @@ const std = @import("std");
const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8; const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8;
pub fn run(event_handler: HandlerFn) !void { // TODO: remove inferred error set? /// Starts the lambda framework. Handler will be called when an event is processing
/// If an allocator is not provided, an approrpriate allocator will be selected and used
pub fn run(allocator: ?std.mem.Allocator, event_handler: HandlerFn) !void { // TODO: remove inferred error set?
const prefix = "http://"; const prefix = "http://";
const postfix = "/2018-06-01/runtime/invocation"; const postfix = "/2018-06-01/runtime/invocation";
const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API").?; const lambda_runtime_uri = std.os.getenv("AWS_LAMBDA_RUNTIME_API").?;
@ -10,20 +12,20 @@ pub fn run(event_handler: HandlerFn) !void { // TODO: remove inferred error set?
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const alloc = allocator orelse gpa.allocator();
const url = try std.fmt.allocPrint(allocator, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix }); const url = try std.fmt.allocPrint(alloc, "{s}{s}{s}/next", .{ prefix, lambda_runtime_uri, postfix });
defer allocator.free(url); defer alloc.free(url);
const uri = try std.Uri.parse(url); const uri = try std.Uri.parse(url);
var client: std.http.Client = .{ .allocator = allocator }; var client: std.http.Client = .{ .allocator = alloc };
defer client.deinit(); defer client.deinit();
var empty_headers = std.http.Headers.init(allocator); var empty_headers = std.http.Headers.init(alloc);
defer empty_headers.deinit(); defer empty_headers.deinit();
std.log.info("Bootstrap initializing with event url: {s}", .{url}); std.log.info("Bootstrap initializing with event url: {s}", .{url});
while (true) { while (true) {
var req_alloc = std.heap.ArenaAllocator.init(allocator); var req_alloc = std.heap.ArenaAllocator.init(alloc);
defer req_alloc.deinit(); defer req_alloc.deinit();
const req_allocator = req_alloc.allocator(); const req_allocator = req_alloc.allocator();
var req = try client.request(.GET, uri, empty_headers, .{}); var req = try client.request(.GET, uri, empty_headers, .{});

View File

@ -2,7 +2,7 @@ const std = @import("std");
const lambda = @import("lambda.zig"); const lambda = @import("lambda.zig");
pub fn main() anyerror!void { pub fn main() anyerror!void {
try lambda.run(handler); try lambda.run(null, handler);
} }
fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 { fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 {