code wiki / _hdl_build / nx_consul_dns.nx

nx_consul_dns.nx source

↩ module page · 28 lines · 1558 B

1// nx_consul_dns.nx -- sovereign SERVICE-DNS interface (HashiCorp Consul service-DNS-class; CONSUL-003). 2// A query for "<svc>.service.nishi" resolves to the A-record set = the addresses of all routable, registered 3// instances of <svc> -- inheriting the health gate from CONSUL-001 discovery, so DNS NEVER returns a down or 4// deregistered node. The answer is dynamic: when an instance goes CRITICAL or deregisters it drops from the 5// A-records on the next query. The DNS frontend (nx_dns_authoritative) only treats *.service.nishi names as 6// service lookups. Composes nx_consul_registry. license_tier: ORIGINAL 7import "nx_consul_registry.nx" 8import "nx_syscalls.nx" 9 10// resolve a service to its A-record set = addresses of routable+registered instances. returns count, fills 11// out_addrs[]. unknown service or all-unhealthy -> 0 A-records (the NXDOMAIN / empty-answer case). 12func csd_resolve(reg: *i64, names: *i64, addrs: *i64, healths: *i64, n: i64, qname: i64, out_addrs: *i64) -> i64 { 13 let idxbuf: *i64 = sys_mmap(256) as *i64 14 let c: i64 = cs_discover(reg, names, healths, n, qname, idxbuf) 15 var i: i64 = 0 16 while i < c { out_addrs[i] = addrs[idxbuf[i]]; i = i + 1 } 17 return c 18} 19 20// is this query a service-DNS FQDN (ends with ".service.nishi")? only these are service lookups. 21func csd_is_service_fqdn(q: *u8, qlen: i64) -> i64 { 22 let suf: *u8 = ".service.nishi" as *u8 23 let sl: i64 = 14 24 if qlen < sl { return 0 } 25 var i: i64 = 0 26 while i < sl { if q[qlen - sl + i] != suf[i] { return 0 } i = i + 1 } 27 return 1 28}