mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
This commit extends the documentation effort by adding detailed explanatory comments to key CLI components and core functionality throughout the cmd/ directory. The changes focus on explaining WHY certain logic is needed, not just WHAT the code does, improving code maintainability and helping developers understand complex business decisions. Key improvements: - Main entry points: Document CLI initialization, logging setup, and cache configuration with reasoning for design decisions - DNS proxy core: Explain DNS proxy constants, data structures, and core processing pipeline for handling DNS queries - Service management: Document service command structure, configuration patterns, and platform-specific service handling - Logging infrastructure: Explain log buffer management, level encoders, and log formatting decisions for different use cases - Metrics and monitoring: Document Prometheus metrics structure, HTTP endpoints, and conditional metric collection for performance - Network handling: Explain Linux-specific network interface filtering, virtual interface detection, and DNS configuration management - Hostname validation: Document RFC1123 compliance and DNS naming standards for system compatibility - Mobile integration: Explain HTTP retry logic, fallback mechanisms, and mobile platform integration patterns - Connection management: Document connection wrapper design to prevent log pollution during process lifecycle Technical details: - Added explanatory comments to 11 additional files in cmd/cli/ - Maintained consistent documentation style and format - Preserved all existing functionality while improving code clarity - Enhanced understanding of complex business logic and platform-specific behavior These comments help future developers understand the reasoning behind complex decisions, making the codebase more maintainable and reducing the risk of incorrect modifications during maintenance.
68 lines
2.2 KiB
Go
68 lines
2.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// logConn wraps a net.Conn, override the Write behavior.
|
|
// runCmd uses this wrapper, so as long as startCmd finished,
|
|
// ctrld log won't be flushed with un-necessary write errors.
|
|
// This prevents log pollution when the parent process closes the connection
|
|
type logConn struct {
|
|
conn net.Conn
|
|
}
|
|
|
|
// Read delegates to the underlying connection
|
|
// This maintains normal read behavior for the wrapped connection
|
|
func (lc *logConn) Read(b []byte) (n int, err error) {
|
|
return lc.conn.Read(b)
|
|
}
|
|
|
|
// Close delegates to the underlying connection
|
|
// This ensures proper cleanup of the wrapped connection
|
|
func (lc *logConn) Close() error {
|
|
return lc.conn.Close()
|
|
}
|
|
|
|
// LocalAddr delegates to the underlying connection
|
|
// This provides access to local address information
|
|
func (lc *logConn) LocalAddr() net.Addr {
|
|
return lc.conn.LocalAddr()
|
|
}
|
|
|
|
// RemoteAddr delegates to the underlying connection
|
|
// This provides access to remote address information
|
|
func (lc *logConn) RemoteAddr() net.Addr {
|
|
return lc.conn.RemoteAddr()
|
|
}
|
|
|
|
// SetDeadline delegates to the underlying connection
|
|
// This maintains timeout functionality for the wrapped connection
|
|
func (lc *logConn) SetDeadline(t time.Time) error {
|
|
return lc.conn.SetDeadline(t)
|
|
}
|
|
|
|
// SetReadDeadline delegates to the underlying connection
|
|
// This maintains read timeout functionality for the wrapped connection
|
|
func (lc *logConn) SetReadDeadline(t time.Time) error {
|
|
return lc.conn.SetReadDeadline(t)
|
|
}
|
|
|
|
// SetWriteDeadline delegates to the underlying connection
|
|
// This maintains write timeout functionality for the wrapped connection
|
|
func (lc *logConn) SetWriteDeadline(t time.Time) error {
|
|
return lc.conn.SetWriteDeadline(t)
|
|
}
|
|
|
|
// Write performs writes with underlying net.Conn, ignore any errors happen.
|
|
// "ctrld run" command use this wrapper to report errors to "ctrld start".
|
|
// If no error occurred, "ctrld start" may finish before "ctrld run" attempt
|
|
// to close the connection, so ignore errors conservatively here, prevent
|
|
// un-necessary error "write to closed connection" flushed to ctrld log.
|
|
// This prevents log pollution when the parent process closes the connection prematurely
|
|
func (lc *logConn) Write(b []byte) (int, error) {
|
|
_, _ = lc.conn.Write(b)
|
|
return len(b), nil
|
|
}
|