From b32d69b9d099788ea603264376a524625083b274 Mon Sep 17 00:00:00 2001 From: Marcos Uchoa Date: Fri, 8 Aug 2025 20:33:03 -0300 Subject: [PATCH] giving some frash air to cpu --- docker-compose.yml | 10 +++++----- nginx.conf | 9 +++------ src/endpoints/payments.zig | 18 ++---------------- src/payment_integrator.zig | 4 ++-- src/services/http_service.zig | 19 ++++++++++--------- 5 files changed, 22 insertions(+), 38 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ea93b14..fa81e88 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,8 +14,8 @@ services: deploy: resources: limits: - cpus: "0.30" - memory: "330MB" + cpus: "0.50" + memory: "10MB" zig1: &zig container_name: zig-pay-1 build: . @@ -23,7 +23,7 @@ services: - SERVER_ENV=PROD - DEFAULT_HOST=payment-processor-default - DEFAULT_PORT=8080 - - DEFAULT_POOL=30 + - DEFAULT_POOL=20 - FALLBACK_HOST=payment-processor-fallback - FALLBACK_PORT=8080 - FALLBACK_POOL=0 @@ -35,7 +35,7 @@ services: deploy: resources: limits: - cpus: "0.60" + cpus: "0.50" memory: "10MB" zig2: <<: *zig @@ -44,7 +44,7 @@ services: - SERVER_ENV=PROD - DEFAULT_HOST=payment-processor-default - DEFAULT_PORT=8080 - - DEFAULT_POOL=30 + - DEFAULT_POOL=20 - FALLBACK_HOST=payment-processor-fallback - FALLBACK_PORT=8080 - FALLBACK_POOL=0 diff --git a/nginx.conf b/nginx.conf index 0748411..37da4de 100644 --- a/nginx.conf +++ b/nginx.conf @@ -1,9 +1,9 @@ worker_processes auto; -worker_rlimit_nofile 500000; +worker_rlimit_nofile 100000; events { use epoll; - worker_connections 1024; + worker_connections 550; } http { access_log off; @@ -12,16 +12,13 @@ http { upstream backend_servers { server zig1:8080; server zig2:8080; - keepalive 200; + keepalive 500; } server { listen 9999; location / { proxy_buffering off; - proxy_set_header Connection ""; proxy_http_version 1.1; - proxy_set_header Keep-Alive ""; - proxy_set_header Proxy-Connection "keep-alive"; proxy_pass http://backend_servers; } } diff --git a/src/endpoints/payments.zig b/src/endpoints/payments.zig index 2cd51fb..f6c466a 100644 --- a/src/endpoints/payments.zig +++ b/src/endpoints/payments.zig @@ -24,13 +24,6 @@ pub fn registerEndpoints(get_endpoints: *EndpointsManager, post_endpoints: *Endp } fn postPayments(req: *Request, res: *Response) void { - if (ctx.server_settings.failure) - return res.withStatus(.service_unavailable).end(); - - if (ctx.server_settings.delay > 0) { - std.time.sleep(ctx.server_settings.delay * std.time.ns_per_ms); - } - res.withStatus(.unprocessable_entity).withContentType(.json).end(); var prop_correlationId = parses.PropertySpec{ .name = "correlationId" }; @@ -54,13 +47,6 @@ fn postPayments(req: *Request, res: *Response) void { } fn getPaymentsById(req: *Request, res: *Response) void { - if (ctx.server_settings.failure) - return res.withStatus(.service_unavailable).end(); - - if (ctx.server_settings.delay > 0) { - std.time.sleep(ctx.server_settings.delay * std.time.ns_per_ms); - } - const template_json_res: []const u8 = \\{{ \\ "correlationId": "{s}", @@ -93,8 +79,8 @@ fn getPaymentsById(req: *Request, res: *Response) void { const template_json_summary: []const u8 = \\{{ - \\ "default": {{ "totalRequests": "{d}", "totalAmount": {d:.2} }}, - \\ "fallback": {{ "totalRequests": "{d}", "totalAmount": {d:.2} }} + \\ "default": {{ "totalRequests": {d}, "totalAmount": {d:.2} }}, + \\ "fallback": {{ "totalRequests": {d}, "totalAmount": {d:.2} }} \\}} ; diff --git a/src/payment_integrator.zig b/src/payment_integrator.zig index c9158fb..abf018e 100644 --- a/src/payment_integrator.zig +++ b/src/payment_integrator.zig @@ -12,7 +12,7 @@ const DateTime = @import("things").DateTime; const is_test = @import("builtin").is_test; -const MAX_QUEUE_SIZE = if (is_test) 100 else 500_000; +const MAX_QUEUE_SIZE = if (is_test) 100 else 100_000; const MAX_RETRY = if (is_test) 2 else 10_000_000; const PaymentIntegration = struct { @@ -53,7 +53,7 @@ pub const PaymentIntegrator = struct { fn startProcess(self: *PaymentIntegrator) void { while (true) { - Thread.sleep(1_000_000); + Thread.sleep(10_000_000); self.verifyTailSize(); self.processPayments(); diff --git a/src/services/http_service.zig b/src/services/http_service.zig index a0bef21..a446ff7 100644 --- a/src/services/http_service.zig +++ b/src/services/http_service.zig @@ -31,7 +31,7 @@ pub const HttpService = struct { health_connection: ServiceConnection = ServiceConnection{}, capacity: usize = 0, - health: ServiceHealth = .unavailable, + health: ServiceHealth = .available, response_time: u64 = 0, mutex: Mutex, @@ -48,11 +48,11 @@ pub const HttpService = struct { var final_addr: ?Address = null; for (addrList.addrs) |addr| { - const ping = net.tcpConnectToAddress(addr) catch { - continue; - }; + //const ping = net.tcpConnectToAddress(addr) catch { + // continue; + //}; - ping.close(); + //ping.close(); final_addr = addr; } @@ -192,7 +192,7 @@ pub const HttpService = struct { pub fn startMessenger(self: *HttpService, connections: []ServiceConnection, tickets: []ServiceTicket, thread_id: usize) void { while (true) { - Thread.sleep(1_000_000); + Thread.sleep(5_000_000); if (self.thread_stop[thread_id]) { var conn_open = false; @@ -233,7 +233,7 @@ pub const HttpService = struct { } fn updateHealth(self: *HttpService, thread_id: usize) void { - while (true) { + while (false) { if (self.thread_stop[thread_id]) { return; } @@ -302,9 +302,10 @@ pub const HttpService = struct { } fn updateCapacity(self: *HttpService, thread_id: usize) void { - while (true) { - Thread.sleep(1_000_000); + while (false) { + Thread.sleep(500_000_000); var count: usize = 0; + for (self.connections) |conn| { if (conn.status == .ocuppied) { count += 1;