bidirectional ip reflection for listener address discovery

This commit is contained in:
2026-02-09 16:02:23 -03:00
parent 59e9e420be
commit 7b47ef155c
31 changed files with 3192 additions and 269 deletions

View File

@@ -16,8 +16,8 @@ pub const RateLimiter = struct {
max_tokens: u64,
/// Token refill rate (tokens per second)
refill_rate: u64,
/// Last refill timestamp (nanoseconds)
last_refill: std.atomic.Value(i128),
/// Last refill timestamp (milliseconds) - using i64 for cross-platform atomic support
last_refill: std.atomic.Value(i64),
/// Mutex for non-atomic operations
mutex: std.Thread.Mutex = .{},
@@ -26,7 +26,7 @@ pub const RateLimiter = struct {
.tokens = std.atomic.Value(u64).init(config.burst_size),
.max_tokens = config.burst_size,
.refill_rate = config.tokens_per_second,
.last_refill = std.atomic.Value(i128).init(time.nanoTimestamp()),
.last_refill = std.atomic.Value(i64).init(time.milliTimestamp()),
};
}
@@ -79,14 +79,14 @@ pub const RateLimiter = struct {
/// Refill tokens based on elapsed time
fn refill(self: *RateLimiter) void {
const now = time.nanoTimestamp();
const now = time.milliTimestamp();
const last = self.last_refill.load(.acquire);
const elapsed_ns = now - last;
const elapsed_ms = now - last;
if (elapsed_ns <= 0) return;
if (elapsed_ms <= 0) return;
// Calculate tokens to add (using nanoseconds for precision)
const elapsed_seconds = @as(u64, @intCast(@divFloor(elapsed_ns, time.ns_per_s)));
// Calculate tokens to add (using milliseconds)
const elapsed_seconds = @as(u64, @intCast(@divFloor(elapsed_ms, time.ms_per_s)));
if (elapsed_seconds == 0) return;
const new_tokens = elapsed_seconds * self.refill_rate;