nx_net_polite_gate.nx source
↩ module page · 45 lines · 2886 B
1// nx_net_polite_gate.nx -- deterministic gate for the citizenship decision layer (NO network/sleep): locks
2// pacing math, exponential backoff (+cap), retry-status classification, and Retry-After parsing. Imports the
3// lib (pure fns; pol_pace/pol_backoff_sleep that actually sleep are NOT called here).
4import "nx_net_polite.nx"
5import "nx_syscalls.nx"
6
7func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func gnum(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(28); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } while k>0 { k=k-1; sys_write(1,(((t as i64)+k) as *u8),1) } return 0 }
9func glen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
10func chk(name: *u8, got: i64, want: i64, pass: *i64, fail: *i64) -> i64 {
11 if got==want { pass[0]=pass[0]+1; gp(" ok " as *u8) } else { fail[0]=fail[0]+1; gp(" FAIL " as *u8) }
12 gp(name); gp(" got=" as *u8); gnum(got); gp(" want=" as *u8); gnum(want); gp("\n" as *u8)
13 return 0
14}
15
16func main() -> i64 {
17 gp("=== nx_net_polite_gate ===\n" as *u8)
18 let pass: *i64=sys_mmap(16) as *i64; let fail: *i64=sys_mmap(16) as *i64; pass[0]=0; fail[0]=0
19
20 // pacing: wait only if inside the min interval
21 chk("T1 pace within-interval waits" as *u8, pol_wait_sec(100, 98, 3), 1, pass, fail)
22 chk("T2 pace enough-time no wait" as *u8, pol_wait_sec(100, 90, 3), 0, pass, fail)
23 chk("T3 pace first-request no wait" as *u8, pol_wait_sec(100, 0, 3), 0, pass, fail)
24
25 // exponential backoff + cap
26 chk("T4 backoff attempt0==base" as *u8, pol_backoff_sec(0, 2, 60), 2, pass, fail)
27 chk("T5 backoff attempt2==8" as *u8, pol_backoff_sec(2, 2, 60), 8, pass, fail)
28 chk("T6 backoff capped==60" as *u8, pol_backoff_sec(6, 2, 60), 60, pass, fail)
29
30 // retry classification
31 chk("T7 retry on 429" as *u8, pol_should_retry(429), 1, pass, fail)
32 chk("T8 retry on 503" as *u8, pol_should_retry(503), 1, pass, fail)
33 chk("T9 no retry on 200" as *u8, pol_should_retry(200), 0, pass, fail)
34 chk("T10 no retry on 404" as *u8, pol_should_retry(404), 0, pass, fail)
35
36 // Retry-After parsing (honor the source's own signal)
37 let resp: *u8="HTTP/1.1 429 Too Many Requests\r\nRetry-After: 30\r\nContent-Length: 0\r\n\r\n" as *u8
38 chk("T11 retry-after==30" as *u8, pol_retry_after(resp, glen(resp)), 30, pass, fail)
39 let resp2: *u8="HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello" as *u8
40 chk("T12 no retry-after==-1" as *u8, pol_retry_after(resp2, glen(resp2)), 0-1, pass, fail)
41
42 gp("\n-- RESULT pass=" as *u8); gnum(pass[0]); gp(" fail=" as *u8); gnum(fail[0]); gp("\n" as *u8)
43 if fail[0]==0 { gp("GATE verdict=GREEN\n" as *u8); sys_exit(0) } else { gp("GATE verdict=RED\n" as *u8); sys_exit(1) }
44 return 0
45}