try some stuffs with nginx
This commit is contained in:
parent
de2e425fae
commit
2bfbc31dcb
8 changed files with 102 additions and 34 deletions
60
docker-compose-nginx.yml
Normal file
60
docker-compose-nginx.yml
Normal 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
|
|
@ -19,8 +19,8 @@ services:
|
|||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.20"
|
||||
memory: "50MB"
|
||||
cpus: "1.5"
|
||||
memory: "130MB"
|
||||
zig1: &zig
|
||||
container_name: zig-pay-1
|
||||
build: .
|
||||
|
@ -28,10 +28,10 @@ services:
|
|||
- SERVER_ENV=PROD
|
||||
- DEFAULT_HOST=payment-processor-default
|
||||
- DEFAULT_PORT=8080
|
||||
- DEFAULT_POOL=100
|
||||
- DEFAULT_POOL=700
|
||||
- FALLBACK_HOST=payment-processor-fallback
|
||||
- FALLBACK_PORT=8080
|
||||
- FALLBACK_POOL=0
|
||||
- FALLBACK_POOL=400
|
||||
- PORT=8080
|
||||
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig2
|
||||
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969
|
||||
|
@ -40,8 +40,8 @@ services:
|
|||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.20"
|
||||
memory: "50MB"
|
||||
cpus: "0.45"
|
||||
memory: "110MB"
|
||||
zig2:
|
||||
<<: *zig
|
||||
container_name: zig-pay-2
|
||||
|
@ -49,10 +49,10 @@ services:
|
|||
- SERVER_ENV=PROD
|
||||
- DEFAULT_HOST=payment-processor-default
|
||||
- DEFAULT_PORT=8080
|
||||
- DEFAULT_POOL=100
|
||||
- DEFAULT_POOL=700
|
||||
- FALLBACK_HOST=payment-processor-fallback
|
||||
- FALLBACK_PORT=8080
|
||||
- FALLBACK_POOL=0
|
||||
- FALLBACK_POOL=400
|
||||
- PORT=8080
|
||||
- PAYMENT_SUMMARY_EXCHANGE_HOST=zig1
|
||||
- PAYMENT_SUMMARY_EXCHANGE_PORT=6969
|
||||
|
|
|
@ -3,11 +3,14 @@ worker_rlimit_nofile 100000;
|
|||
|
||||
events {
|
||||
use epoll;
|
||||
worker_connections 1024;
|
||||
worker_connections 2000;
|
||||
}
|
||||
|
||||
http {
|
||||
access_log off;
|
||||
error_log /dev/null crit;
|
||||
keepalive_requests 8000;
|
||||
keepalive_timeout 230;
|
||||
|
||||
|
||||
tcp_nopush on;
|
||||
|
@ -17,12 +20,14 @@ http {
|
|||
upstream backend_servers {
|
||||
server zig1:8080;
|
||||
server zig2:8080;
|
||||
keepalive 500;
|
||||
keepalive 600;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 9999;
|
||||
location / {
|
||||
proxy_no_cache 1;
|
||||
proxy_cache_bypass 1;
|
||||
proxy_buffering off;
|
||||
proxy_http_version 1.1;
|
||||
proxy_pass http://backend_servers;
|
||||
|
|
|
@ -12,10 +12,11 @@ const Address = net.Address;
|
|||
const Stream = net.Stream;
|
||||
|
||||
const BUFFER_SIZE = 1024;
|
||||
const MAX_CONNECTION_UPSTREAM = 2050;
|
||||
|
||||
var metrics_mutex = std.Thread.Mutex{};
|
||||
var metrics_num_requests: u64 = 0;
|
||||
var metrics_sum_req_time: u64 = 0;
|
||||
//var metrics_mutex = std.Thread.Mutex{};
|
||||
//var metrics_num_requests: u64 = 0;
|
||||
//var metrics_sum_req_time: u64 = 0;
|
||||
|
||||
const UpstreamConnectionState = enum { inactive, available, occupied };
|
||||
|
||||
|
@ -45,7 +46,7 @@ const UpstreamConnection = struct {
|
|||
};
|
||||
|
||||
pub const UpstreamServer = struct {
|
||||
pool: [300]UpstreamConnection,
|
||||
pool: [MAX_CONNECTION_UPSTREAM]UpstreamConnection,
|
||||
address: net.Address,
|
||||
|
||||
pub fn init(host: []const u8, port: u16) UpstreamServer {
|
||||
|
@ -67,7 +68,7 @@ pub const UpstreamServer = struct {
|
|||
|
||||
std.debug.assert(final_addr != null);
|
||||
|
||||
var connections: [300]UpstreamConnection = undefined;
|
||||
var connections: [MAX_CONNECTION_UPSTREAM]UpstreamConnection = undefined;
|
||||
|
||||
for (&connections) |*conn| {
|
||||
conn.* = UpstreamConnection.init(final_addr.?);
|
||||
|
@ -128,7 +129,7 @@ pub const LoadBalancer = struct {
|
|||
|
||||
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});
|
||||
conn.stream.close();
|
||||
continue;
|
||||
|
@ -151,7 +152,7 @@ pub const LoadBalancer = struct {
|
|||
|
||||
buffer_request[0] = 0;
|
||||
|
||||
var timer = std.time.Timer.start() catch return;
|
||||
//var timer = std.time.Timer.start() catch return;
|
||||
|
||||
while (true) {
|
||||
var req_len: usize = 1;
|
||||
|
@ -174,7 +175,8 @@ pub const LoadBalancer = struct {
|
|||
break;
|
||||
}
|
||||
}
|
||||
timer.reset();
|
||||
|
||||
//timer.reset();
|
||||
|
||||
upstream.stream.writeAll(buffer_request[0..req_len]) catch |err| {
|
||||
log.err("Error when writing to upstream {}\n", .{err});
|
||||
|
@ -195,17 +197,17 @@ pub const LoadBalancer = struct {
|
|||
return;
|
||||
};
|
||||
|
||||
const req_time_ns = timer.lap();
|
||||
//const req_time_ns = timer.lap();
|
||||
|
||||
metrics_mutex.lock();
|
||||
metrics_num_requests += 1;
|
||||
metrics_sum_req_time += req_time_ns;
|
||||
//metrics_mutex.lock();
|
||||
//metrics_num_requests += 1;
|
||||
//metrics_sum_req_time += req_time_ns;
|
||||
|
||||
if (metrics_num_requests % 5000 == 0) {
|
||||
std.debug.print("average requests time ns: {d}\n", .{metrics_sum_req_time / metrics_num_requests});
|
||||
}
|
||||
//if (metrics_num_requests % 5000 == 0) {
|
||||
// std.debug.print("average requests time ns: {d}\n", .{metrics_sum_req_time / metrics_num_requests});
|
||||
//}
|
||||
|
||||
metrics_mutex.unlock();
|
||||
//metrics_mutex.unlock();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -26,7 +26,7 @@ pub fn main() !void {
|
|||
}
|
||||
|
||||
fn startServer() !void {
|
||||
try ctx.intiRepository(100_000);
|
||||
try ctx.intiRepository(1_000_000);
|
||||
|
||||
std.debug.print("Starting server...\n", .{});
|
||||
|
||||
|
|
|
@ -12,8 +12,8 @@ const DateTime = @import("things").DateTime;
|
|||
|
||||
const is_test = @import("builtin").is_test;
|
||||
|
||||
const MAX_QUEUE_SIZE = if (is_test) 100 else 100_000;
|
||||
const MAX_RETRY = if (is_test) 2 else 10_000_000;
|
||||
const MAX_QUEUE_SIZE = if (is_test) 100 else 1_000_000;
|
||||
const MAX_RETRY = if (is_test) 2 else 1_000_000;
|
||||
|
||||
const PaymentIntegration = struct {
|
||||
payment: *payments.Payment,
|
||||
|
@ -53,7 +53,7 @@ pub const PaymentIntegrator = struct {
|
|||
|
||||
fn startProcess(self: *PaymentIntegrator) void {
|
||||
while (true) {
|
||||
Thread.sleep(50_000_000);
|
||||
Thread.sleep(40_000_000);
|
||||
|
||||
self.verifyTailSize();
|
||||
self.processPayments();
|
||||
|
@ -88,6 +88,7 @@ pub const PaymentIntegrator = struct {
|
|||
pi.payment.integration_status = .not_integrated;
|
||||
pi.is_integrated = true; // liar
|
||||
} else {
|
||||
//pi.payment.requested_at = DateTime.now();
|
||||
pi.ticket = self.service_pool.dive(pi.getMessage()) catch null;
|
||||
pi.retries -= 1;
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ pub const HttpServer = struct {
|
|||
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});
|
||||
conn.stream.close();
|
||||
continue;
|
||||
|
|
|
@ -194,7 +194,7 @@ pub const HttpService = struct {
|
|||
|
||||
pub fn startMessenger(self: *HttpService, connections: []ServiceConnection, tickets: []ServiceTicket, thread_id: usize) void {
|
||||
while (true) {
|
||||
Thread.sleep(30_000_000);
|
||||
Thread.sleep(20_000_000);
|
||||
if (self.thread_stop[thread_id]) {
|
||||
var conn_open = false;
|
||||
|
||||
|
@ -241,7 +241,7 @@ pub const HttpService = struct {
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -305,7 +305,7 @@ pub const HttpService = struct {
|
|||
|
||||
fn updateCapacity(self: *HttpService, thread_id: usize) void {
|
||||
while (false) {
|
||||
Thread.sleep(500_000_000);
|
||||
Thread.sleep(10_000_000 * 1000);
|
||||
var count: usize = 0;
|
||||
|
||||
for (self.connections) |conn| {
|
||||
|
|
Loading…
Reference in a new issue