not intialize repository when load balancer

This commit is contained in:
Marcos Uchoa 2025-08-11 19:35:34 -03:00
parent 7e9f49e46f
commit 3506cb37bf
7 changed files with 28 additions and 18 deletions

View file

@ -19,7 +19,7 @@ services:
deploy:
resources:
limits:
cpus: "0.20"
cpus: "0.30"
memory: "50MB"
zig1: &zig
container_name: zig-pay-1
@ -40,7 +40,7 @@ services:
deploy:
resources:
limits:
cpus: "0.15"
cpus: "0.60"
memory: "50MB"
zig2:
<<: *zig

View file

@ -15,6 +15,8 @@ pub const ServerSettings = struct {
const payments = data.payments;
var config_allocated: [1024 * 100]u8 = undefined;
var heap_alocated: []u8 = undefined;
pub var config: Config = undefined;
@ -25,15 +27,19 @@ pub var summary_exchange: exchange.PaymentSummaryExchange = undefined;
pub var server_settings: ServerSettings = .{};
pub fn init() !void {
const total_elements_expected = 200_000;
const alloc_payments_repository = payments.calculateNecessaryMemory(total_elements_expected);
heap_alocated = try std.heap.page_allocator.alloc(u8, 1024 * 100 + alloc_payments_repository);
var pba = FixedBufferAllocator.init(heap_alocated);
pub fn initConfig() !void {
var pba = FixedBufferAllocator.init(&config_allocated);
const allocator = pba.allocator();
config = try Config.init(allocator);
}
pub fn intiRepository(total_elements_expected: usize) !void {
const alloc_payments_repository = payments.calculateNecessaryMemory(total_elements_expected);
heap_alocated = try std.heap.page_allocator.alloc(u8, alloc_payments_repository);
var pba = FixedBufferAllocator.init(heap_alocated);
const allocator = pba.allocator();
const data_payments = try allocator.alloc(u8, alloc_payments_repository);
var pba_data_payments = FixedBufferAllocator.init(data_payments);

View file

@ -137,8 +137,8 @@ pub const PaymentsRepository = struct {
return Allocator.Error.OutOfMemory;
self.payments[self.len] = payment;
const id_hash = self.hash(&payment.id);
try self.indexes[id_hash].add(&self.payments[self.len]);
//const id_hash = self.hash(&payment.id);
//try self.indexes[id_hash].add(&self.payments[self.len]);
for (self.subscribers_insert[0..self.subscribers_insert_len]) |func| {
func(&self.payments[self.len]);

View file

@ -95,6 +95,7 @@ const template_json_summary: []const u8 =
;
fn getSummary(req: *Request, res: *Response) void {
std.Thread.sleep(1_000_000 * 10);
var from: ?DateTime = null;
var to: ?DateTime = null;

View file

@ -64,7 +64,7 @@ pub const ExchangeConnection = struct {
}
for (0..10) |_| {
Thread.sleep(1_000_000 * 1000);
Thread.sleep(1_000_000 * 500);
self.out = net.tcpConnectToAddress(self.out_address) catch |err| {
log.err("Error connect to secret server {any}", .{err});
@ -98,11 +98,13 @@ pub const ExchangeConnection = struct {
self.server_status = .active;
self.in = server_conn.stream;
server.deinit();
return;
}
server.deinit();
Thread.sleep(1_000_000 * 1000);
Thread.sleep(1_000_000 * 500);
}
self.server_status = .dead;

View file

@ -41,7 +41,7 @@ const UpstreamConnection = struct {
};
pub const UpstreamServer = struct {
pool: [512]UpstreamConnection,
pool: [300]UpstreamConnection,
address: net.Address,
pub fn init(host: []const u8, port: u16) UpstreamServer {
@ -63,7 +63,7 @@ pub const UpstreamServer = struct {
std.debug.assert(final_addr != null);
var connections: [512]UpstreamConnection = undefined;
var connections: [300]UpstreamConnection = undefined;
for (&connections) |*conn| {
conn.* = UpstreamConnection.init(final_addr.?);

View file

@ -15,8 +15,7 @@ const lb = @import("load_balancer.zig");
pub fn main() !void {
std.debug.print("Init context\n", .{});
try ctx.init();
defer ctx.deinit();
try ctx.initConfig();
std.debug.print("Context: OK\n", .{});
if (ctx.config.isLoadBalancer()) {
@ -27,6 +26,8 @@ pub fn main() !void {
}
fn startServer() !void {
try ctx.intiRepository(100_000);
std.debug.print("Starting server...\n", .{});
var get_endpoints = endpoints.EndpointsManager{};
@ -68,12 +69,12 @@ fn startServer() !void {
var myserver = try server.HttpServer.init(ip_map, ctx.config.port, &get_endpoints, &post_endpoints, &put_endpoints);
std.Thread.sleep(1_000_000 * 1000 * 2);
std.Thread.sleep(1_000_000 * 1000 * 1);
try myserver.start();
}
fn startLoadBalancer() !void {
std.Thread.sleep(1_000_000 * 1000 * 3);
std.Thread.sleep(1_000_000 * 1000 * 2);
std.debug.print("Starting load balancer...\n", .{});
var ip_map: []const u8 = "127.0.0.1";