Skip to content

Commit f0a0118

Browse files
committed
perf: optimize TAIN protocol response and validation handling
- Improve TAIN protocol performance through allocation reduction and direct byte comparison optimizations. - UDP is best-effort protocol, so error logging removal is acceptable for high-throughput time server applications. Signed-off-by: Nagy Károly Gábriel <[email protected]>
1 parent 5771f3a commit f0a0118

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

cmd/gtclockd.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"flag"
65
"fmt"
76
"net"
@@ -49,26 +48,31 @@ var configDir string
4948
// - TAI64 epoch: 1970-01-01 00:00:10 TAI (10 seconds after Unix epoch)
5049
// - Current offset: TAI = UTC + 37 seconds (as of 2025)
5150

51+
var responseHeader = []byte("s")
52+
5253
// sendResponse handles TAIN protocol response
5354
func sendResponse(conn *net.UDPConn, _ int, remoteaddr *net.UDPAddr, buf []byte) {
54-
s := []byte("s")
55-
copy(buf[0:], s)
56-
copy(buf[4:], glibtai.TAINPack(glibtai.TAINNow()))
57-
_, err := conn.WriteToUDP(buf, remoteaddr)
58-
if err != nil {
59-
_, _ = fmt.Printf("Couldn't send response %v", err)
60-
}
55+
56+
copy(buf[0:1], responseHeader)
57+
taiTime := glibtai.TAINNow()
58+
copy(buf[4:16], glibtai.TAINPack(taiTime))
59+
// Send response - ignore errors for performance (UDP is best-effort anyway)
60+
_, _ = conn.WriteToUDP(buf, remoteaddr)
6161
}
6262

6363
// validateTAINRequest validates TAIN protocol requests
6464
func validateTAINRequest(config *gtudpd.Config) gtudpd.RequestValidator {
6565
return func(n int, buf []byte, remoteIP net.IP) bool {
66-
// Check protocol format: minimum 20 bytes, maximum config.MaxRequestSize, starts with "ctai"
67-
if n < 20 || n > config.MaxRequestSize || !bytes.Equal(buf[:4], []byte("ctai")) {
66+
if n < 20 || n > config.MaxRequestSize {
6867
return false
6968
}
70-
// Check client permissions using gtudpd
69+
if buf[0] != 'c' || buf[1] != 't' || buf[2] != 'a' || buf[3] != 'i' {
70+
return false
71+
}
72+
73+
// Check client permissions (this may involve filesystem operations)
7174
return config.ClientOK(remoteIP)
75+
7276
}
7377
}
7478

0 commit comments

Comments
 (0)