try some stuffs with nginx

This commit is contained in:
Marcos Uchoa 2025-08-17 20:48:32 -03:00
parent de2e425fae
commit 2bfbc31dcb
8 changed files with 102 additions and 34 deletions

60
docker-compose-nginx.yml Normal file
View file

@ -0,0 +1,60 @@
services:
nginx:
container_name: balance-zig-pay
image: nginx:1.28-alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- zig1
- zig2
ports:
- "9999:9999"
networks:
- payment-processor
deploy:
resources:
limits:
cpus: "0.50"
memory: "150MB"
zig1: &zig
container_name: zig-pay-1
build: .
environment:
- SERVER_ENV=PROD
- DEFAULT_HOST=payment-processor-default
- DEFAULT_PORT=8080
- DEFAULT_POOL=300
- FALLBACK_HOST=payment-processor-fallback
- FALLBACK_PORT=8080
- FALLBACK_POOL=100
- PORT=8080
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig2
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969
networks:
- payment-processor
deploy:
resources:
limits:
cpus: "0.50"
memory: "100MB"
zig2:
<<: *zig
container_name: zig-pay-2
environment:
- SERVER_ENV=PROD
- DEFAULT_HOST=payment-processor-default
- DEFAULT_PORT=8080
- DEFAULT_POOL=300
- FALLBACK_HOST=payment-processor-fallback
- FALLBACK_PORT=8080
- FALLBACK_POOL=100
- PORT=8080
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig1
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969
networks:
backend:
driver: bridge
payment-processor:
external: true

View file

@ -19,8 +19,8 @@ services:
deploy: deploy:
resources: resources:
limits: limits:
cpus: "0.20" cpus: "1.5"
memory: "50MB" memory: "130MB"
zig1: &zig zig1: &zig
container_name: zig-pay-1 container_name: zig-pay-1
build: . build: .
@ -28,10 +28,10 @@ services:
- SERVER_ENV=PROD - SERVER_ENV=PROD
- DEFAULT_HOST=payment-processor-default - DEFAULT_HOST=payment-processor-default
- DEFAULT_PORT=8080 - DEFAULT_PORT=8080
- DEFAULT_POOL=100 - DEFAULT_POOL=700
- FALLBACK_HOST=payment-processor-fallback - FALLBACK_HOST=payment-processor-fallback
- FALLBACK_PORT=8080 - FALLBACK_PORT=8080
- FALLBACK_POOL=0 - FALLBACK_POOL=400
- PORT=8080 - PORT=8080
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig2 - PAYMENT_SUMMARY_EXCHANGE_HOST=zig2
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969 - PAYMENT_SUMMARY_EXCHANGE_PORT=6969
@ -40,8 +40,8 @@ services:
deploy: deploy:
resources: resources:
limits: limits:
cpus: "0.20" cpus: "0.45"
memory: "50MB" memory: "110MB"
zig2: zig2:
<<: *zig <<: *zig
container_name: zig-pay-2 container_name: zig-pay-2
@ -49,10 +49,10 @@ services:
- SERVER_ENV=PROD - SERVER_ENV=PROD
- DEFAULT_HOST=payment-processor-default - DEFAULT_HOST=payment-processor-default
- DEFAULT_PORT=8080 - DEFAULT_PORT=8080
- DEFAULT_POOL=100 - DEFAULT_POOL=700
- FALLBACK_HOST=payment-processor-fallback - FALLBACK_HOST=payment-processor-fallback
- FALLBACK_PORT=8080 - FALLBACK_PORT=8080
- FALLBACK_POOL=0 - FALLBACK_POOL=400
- PORT=8080 - PORT=8080
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig1 - PAYMENT_SUMMARY_EXCHANGE_HOST=zig1
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969 - PAYMENT_SUMMARY_EXCHANGE_PORT=6969

View file

@ -3,11 +3,14 @@ worker_rlimit_nofile 100000;
events { events {
use epoll; use epoll;
worker_connections 1024; worker_connections 2000;
} }
http { http {
access_log off; access_log off;
error_log /dev/null crit; error_log /dev/null crit;
keepalive_requests 8000;
keepalive_timeout 230;
tcp_nopush on; tcp_nopush on;
@ -17,12 +20,14 @@ http {
upstream backend_servers { upstream backend_servers {
server zig1:8080; server zig1:8080;
server zig2:8080; server zig2:8080;
keepalive 500; keepalive 600;
} }
server { server {
listen 9999; listen 9999;
location / { location / {
proxy_no_cache 1;
proxy_cache_bypass 1;
proxy_buffering off; proxy_buffering off;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_pass http://backend_servers; proxy_pass http://backend_servers;

View file

@ -12,10 +12,11 @@ const Address = net.Address;
const Stream = net.Stream; const Stream = net.Stream;
const BUFFER_SIZE = 1024; const BUFFER_SIZE = 1024;
const MAX_CONNECTION_UPSTREAM = 2050;
var metrics_mutex = std.Thread.Mutex{}; //var metrics_mutex = std.Thread.Mutex{};
var metrics_num_requests: u64 = 0; //var metrics_num_requests: u64 = 0;
var metrics_sum_req_time: u64 = 0; //var metrics_sum_req_time: u64 = 0;
const UpstreamConnectionState = enum { inactive, available, occupied }; const UpstreamConnectionState = enum { inactive, available, occupied };
@ -45,7 +46,7 @@ const UpstreamConnection = struct {
}; };
pub const UpstreamServer = struct { pub const UpstreamServer = struct {
pool: [300]UpstreamConnection, pool: [MAX_CONNECTION_UPSTREAM]UpstreamConnection,
address: net.Address, address: net.Address,
pub fn init(host: []const u8, port: u16) UpstreamServer { pub fn init(host: []const u8, port: u16) UpstreamServer {
@ -67,7 +68,7 @@ pub const UpstreamServer = struct {
std.debug.assert(final_addr != null); std.debug.assert(final_addr != null);
var connections: [300]UpstreamConnection = undefined; var connections: [MAX_CONNECTION_UPSTREAM]UpstreamConnection = undefined;
for (&connections) |*conn| { for (&connections) |*conn| {
conn.* = UpstreamConnection.init(final_addr.?); conn.* = UpstreamConnection.init(final_addr.?);
@ -128,7 +129,7 @@ pub const LoadBalancer = struct {
upstream.?.occupy(); upstream.?.occupy();
var thread = std.Thread.spawn(.{ .stack_size = 1024 * 16 }, handleConnection, .{ self, conn, upstream.? }) catch |err| { var thread = std.Thread.spawn(.{ .stack_size = 1024 * 6 }, handleConnection, .{ self, conn, upstream.? }) catch |err| {
log.err("Creating thread error: {}\n", .{err}); log.err("Creating thread error: {}\n", .{err});
conn.stream.close(); conn.stream.close();
continue; continue;
@ -151,7 +152,7 @@ pub const LoadBalancer = struct {
buffer_request[0] = 0; buffer_request[0] = 0;
var timer = std.time.Timer.start() catch return; //var timer = std.time.Timer.start() catch return;
while (true) { while (true) {
var req_len: usize = 1; var req_len: usize = 1;
@ -174,7 +175,8 @@ pub const LoadBalancer = struct {
break; break;
} }
} }
timer.reset();
//timer.reset();
upstream.stream.writeAll(buffer_request[0..req_len]) catch |err| { upstream.stream.writeAll(buffer_request[0..req_len]) catch |err| {
log.err("Error when writing to upstream {}\n", .{err}); log.err("Error when writing to upstream {}\n", .{err});
@ -195,17 +197,17 @@ pub const LoadBalancer = struct {
return; return;
}; };
const req_time_ns = timer.lap(); //const req_time_ns = timer.lap();
metrics_mutex.lock(); //metrics_mutex.lock();
metrics_num_requests += 1; //metrics_num_requests += 1;
metrics_sum_req_time += req_time_ns; //metrics_sum_req_time += req_time_ns;
if (metrics_num_requests % 5000 == 0) { //if (metrics_num_requests % 5000 == 0) {
std.debug.print("average requests time ns: {d}\n", .{metrics_sum_req_time / metrics_num_requests}); // std.debug.print("average requests time ns: {d}\n", .{metrics_sum_req_time / metrics_num_requests});
} //}
metrics_mutex.unlock(); //metrics_mutex.unlock();
} }
} }
}; };

View file

@ -26,7 +26,7 @@ pub fn main() !void {
} }
fn startServer() !void { fn startServer() !void {
try ctx.intiRepository(100_000); try ctx.intiRepository(1_000_000);
std.debug.print("Starting server...\n", .{}); std.debug.print("Starting server...\n", .{});

View file

@ -12,8 +12,8 @@ const DateTime = @import("things").DateTime;
const is_test = @import("builtin").is_test; const is_test = @import("builtin").is_test;
const MAX_QUEUE_SIZE = if (is_test) 100 else 100_000; const MAX_QUEUE_SIZE = if (is_test) 100 else 1_000_000;
const MAX_RETRY = if (is_test) 2 else 10_000_000; const MAX_RETRY = if (is_test) 2 else 1_000_000;
const PaymentIntegration = struct { const PaymentIntegration = struct {
payment: *payments.Payment, payment: *payments.Payment,
@ -53,7 +53,7 @@ pub const PaymentIntegrator = struct {
fn startProcess(self: *PaymentIntegrator) void { fn startProcess(self: *PaymentIntegrator) void {
while (true) { while (true) {
Thread.sleep(50_000_000); Thread.sleep(40_000_000);
self.verifyTailSize(); self.verifyTailSize();
self.processPayments(); self.processPayments();
@ -88,6 +88,7 @@ pub const PaymentIntegrator = struct {
pi.payment.integration_status = .not_integrated; pi.payment.integration_status = .not_integrated;
pi.is_integrated = true; // liar pi.is_integrated = true; // liar
} else { } else {
//pi.payment.requested_at = DateTime.now();
pi.ticket = self.service_pool.dive(pi.getMessage()) catch null; pi.ticket = self.service_pool.dive(pi.getMessage()) catch null;
pi.retries -= 1; pi.retries -= 1;
} }

View file

@ -64,7 +64,7 @@ pub const HttpServer = struct {
continue; continue;
}; };
var thread = std.Thread.spawn(.{ .stack_size = 1024 * 64 }, handleConnection, .{ self, conn }) catch |err| { var thread = std.Thread.spawn(.{ .stack_size = 1024 * 6 }, handleConnection, .{ self, conn }) catch |err| {
log.err("Creating thread error: {}\n", .{err}); log.err("Creating thread error: {}\n", .{err});
conn.stream.close(); conn.stream.close();
continue; continue;

View file

@ -194,7 +194,7 @@ pub const HttpService = struct {
pub fn startMessenger(self: *HttpService, connections: []ServiceConnection, tickets: []ServiceTicket, thread_id: usize) void { pub fn startMessenger(self: *HttpService, connections: []ServiceConnection, tickets: []ServiceTicket, thread_id: usize) void {
while (true) { while (true) {
Thread.sleep(30_000_000); Thread.sleep(20_000_000);
if (self.thread_stop[thread_id]) { if (self.thread_stop[thread_id]) {
var conn_open = false; var conn_open = false;
@ -241,7 +241,7 @@ pub const HttpService = struct {
} }
self.checkHealth(); self.checkHealth();
//log.info("Service {s} health {any} {d} ms\n", .{ self.name, self.health, self.response_time }); log.info("Service {s} health {any} {d} ms\n", .{ self.name, self.health, self.response_time });
Thread.sleep(1_000_000 * 1000); Thread.sleep(1_000_000 * 1000);
} }
} }
@ -305,7 +305,7 @@ pub const HttpService = struct {
fn updateCapacity(self: *HttpService, thread_id: usize) void { fn updateCapacity(self: *HttpService, thread_id: usize) void {
while (false) { while (false) {
Thread.sleep(500_000_000); Thread.sleep(10_000_000 * 1000);
var count: usize = 0; var count: usize = 0;
for (self.connections) |conn| { for (self.connections) |conn| {