Documentation
¶
Index ¶
- Constants
- Variables
- func NewClient(opts ...ClientOpt) *clientS
- func NewServer(addr string, opts ...ServerOpt) *serverWrap
- type BasicLogger
- type CacheWriteable
- type Client
- type ClientOpt
- type Closable
- type Closers
- type CorruptDataFinder
- type DataProcessor
- type ExtraPrinters
- type Handler
- type HandlerFunc
- type LogEntry
- type Logger
- type OnHotReload
- type OnNewResponse
- type OnRestart
- type OnShutdown
- type OnTcpServerConnectedWithClient
- type OnTcpServerCorruptData
- type OnTcpServerCreateReadWriter
- type OnTcpServerDisconnectedWithClient
- type OnTcpServerListening
- type OnTcpServerProcessData
- type Printer
- type Runnable
- type Server
- type ServerOpt
- func WithNetwork(network string) ServerOpt
- func WithServerHandler(h Handler) ServerOpt
- func WithServerHandlerFunc(h HandlerFunc) ServerOpt
- func WithServerListenConfig(c *net.ListenConfig) ServerOpt
- func WithServerLogger(l Logger) ServerOpt
- func WithServerLoggerHandler(h slog.Handler) ServerOpt
- func WithServerMaxMessageLength(l int) ServerOpt
- func WithServerOnClientConnected(cb ...OnTcpServerConnectedWithClient) ServerOpt
- func WithServerOnClientDisconnected(cb ...OnTcpServerDisconnectedWithClient) ServerOpt
- func WithServerOnCorruptData(cb OnTcpServerCorruptData) ServerOpt
- func WithServerOnCreateReadWriter(cb OnTcpServerCreateReadWriter) ServerOpt
- func WithServerOnListening(cb ...OnTcpServerListening) ServerOpt
- func WithServerOnNewResponse(nr OnNewResponse) ServerOpt
- func WithServerOnProcessData(cb OnTcpServerProcessData) ServerOpt
- func WithServerOnShutdown(cb OnShutdown) ServerOpt
- func WithServerQuiet(b bool) ServerOpt
- func WithServerTLSConfig(c *tls.Config) ServerOpt
Constants ¶
const ( LevelVerbose = slog.Level(-16) LevelTrace = slog.Level(-8) LevelNotice = slog.Level(2) LevelHint = slog.Level(3) LevelFatal = slog.Level(16) LevelPanic = slog.Level(17) )
const (
// Alphabets gets the a to z and A to Z
Alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
Variables ¶
var LevelNames = map[slog.Leveler]string{ LevelVerbose: "VERBOSE", LevelTrace: "TRACE", LevelNotice: "NOTICE", LevelHint: "HINT", LevelFatal: "FATAL", LevelPanic: "PANIC", }
Functions ¶
Types ¶
type BasicLogger ¶
type BasicLogger interface {
Printer
}
type CacheWriteable ¶
type CacheWriteable interface {
// WrChannel is only available without user Handler specified.
// The default connS.serve will received the data from WrChannel and write
// to internal connection rawly.
WrChannel() chan<- []byte
}
type ClientOpt ¶
type ClientOpt func(s *clientS)
func WithClientInterceptor ¶
func WithClientInterceptor(ci api.Interceptor) ClientOpt
func WithClientLogger ¶
func WithClientLoggerHandler ¶
func WithClientSendCacheSize ¶
type CorruptDataFinder ¶
type CorruptDataFinder interface {
OnCorruptData(data []byte, w api.Response, r api.Request) (ate int)
}
CorruptDataFinder try finding bounds of next good diagram and returns ate bytes.
If ate ok, buf[ate:] should be a good diagram. Or returns 0 to declare a wrong state.
type DataProcessor ¶
type DataProcessor interface {
// Process implements Processor interface to announce that i will process the incoming data in Read().
Process(data []byte, w api.Response, r api.Request) (nn int, err error)
}
DataProcessor handles data and extract one or more data diagrams.
If must necessary, reading more bytes from r and writing something to w.
type ExtraPrinters ¶
type ExtraPrinters interface {
Verbose(msg string, args ...any) // only for -tags=verbose
Print(msg string, args ...any) // logging always
Println(args ...any) // synonym to Print, NOTE first elem of args decoded as msg here
Trace(msg string, args ...any) // only for state.Env().InTracing()
Panic(msg string, args ...any) // error and panic
Fatal(msg string, args ...any) // error and os.Exit(-3)
}
type Handler ¶
type Handler interface {
Serve(ctx context.Context, w api.Response, r api.Request) (processed bool, err error)
}
Handler is implemented by any value that implements ServeDNS.
type HandlerFunc ¶
type HandlerFunc func(ctx context.Context, w api.Response, r api.Request) (processed bool, err error)
The HandlerFunc type is an adapter to allow the use of ordinary functions as DNS handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type LogEntry ¶
type LogEntry interface {
BasicLogger
}
type OnNewResponse ¶
type OnShutdown ¶
type OnTcpServerCorruptData ¶
type OnTcpServerListening ¶
type OnTcpServerProcessData ¶
type Printer ¶
type Printer interface {
Error(msg string, args ...any) // error
Warn(msg string, args ...any) // warning
Info(msg string, args ...any) // info. Attr, Attrs in args will be recognized as is
Debug(msg string, args ...any) // only for state.Env().InDebugging() or IsDebugBuild()
Log(ctx context.Context, lvl slog.Level, msg string, args ...any)
}
type Server ¶
type Server interface {
Listen(ctx context.Context) (err error)
ListenAndServe(ctx context.Context, handler Handler) (err error) // Start server with block
ListenAndServeTLS(ctx context.Context, addr, certFile, keyFile string, handler Handler) (err error)
Start(ctx context.Context) (err error) // Start server without block
Stop() (err error)
Restart(ctx context.Context) (err error)
HotReload(ctx context.Context) (err error)
Shutdown() (err error) // = Stop
Close() // = Stop
WithOnShutdown(cb OnShutdown) Server // set OnShutdown handler
}
type ServerOpt ¶
type ServerOpt func(s *serverWrap)
func WithNetwork ¶
WithNetwork sets network protocol: tcp, tcp4, tcp6, unix and unixpacket
func WithServerHandler ¶
func WithServerHandlerFunc ¶
func WithServerHandlerFunc(h HandlerFunc) ServerOpt
func WithServerListenConfig ¶
func WithServerListenConfig(c *net.ListenConfig) ServerOpt
WithServerListenConfig gives a user-defined listening config structure
func WithServerLogger ¶
func WithServerLoggerHandler ¶
func WithServerMaxMessageLength ¶
WithServerMaxMessageLength assumes a message/diagram have the given max-length.
Specifying a proper buffer size is useful for parsing incoming data buffer efficiently.
Too large buffer size takes wasted spaces and too small causes a large message cannot be split from incoming data stream.
Default buffer size is 4096.
serverWrap allocates 2 x bufferSize bytes to internal buffer, and reads 1 x bufferSize bytes into it. After one message extracted from internal buffer, the rest bytes are moved to beginning of buffer, and next reading position following it.
The double bufferSize allows the above algorithm always works properly.
func WithServerOnClientConnected ¶
func WithServerOnClientConnected(cb ...OnTcpServerConnectedWithClient) ServerOpt
func WithServerOnClientDisconnected ¶
func WithServerOnClientDisconnected(cb ...OnTcpServerDisconnectedWithClient) ServerOpt
func WithServerOnCorruptData ¶
func WithServerOnCorruptData(cb OnTcpServerCorruptData) ServerOpt
func WithServerOnCreateReadWriter ¶
func WithServerOnCreateReadWriter(cb OnTcpServerCreateReadWriter) ServerOpt
func WithServerOnListening ¶
func WithServerOnListening(cb ...OnTcpServerListening) ServerOpt
WithServerOnListening sets callback to OnTcpServerListening so that you can do something while the server started ready.
func WithServerOnNewResponse ¶
func WithServerOnNewResponse(nr OnNewResponse) ServerOpt
WithServerOnNewResponse sets a user-defined Response maker.
The default ones does make new connS and run its looper. You may replace it with yours.
func WithServerOnProcessData ¶
func WithServerOnProcessData(cb OnTcpServerProcessData) ServerOpt
func WithServerOnShutdown ¶
func WithServerOnShutdown(cb OnShutdown) ServerOpt
func WithServerQuiet ¶
func WithServerTLSConfig ¶
WithServerTLSConfig enables a tls link