Documentation
¶
Index ¶
- func BuildTLSConfig(certFile, keyFile, caFile string, insecureSkipVerify bool) (*tls.Config, error)
- func BytesToString(b []byte) string
- func CharSpliter(data []byte, atEOF bool) (advance int, token []byte, err error)
- func CheckErr(err error)
- func CombineError(fns ...func() error) error
- func Contains[T comparable](slice []T, elem T) bool
- func Depointer[T comparable](t *T) T
- func Deref[T any](p *T) T
- func DerefOr[T any](p *T, defaultVal T) T
- func FileExists(filename string) bool
- func FormatDurationSmart(d time.Duration, precisions ...int) string
- func GetFdFromConn(c net.Conn) int
- func GetFdFromListener(l net.Listener) int
- func GetFunctionName(x any) string
- func HandleErr(err error, notExit ...bool)
- func HashID(fields ...string) string
- func IPv6ToIPv4(ipStr string) string
- func IsConnClosed(err error) bool
- func LogDuration(d time.Duration) zap.Field
- func NoError(fn func() error) error
- func ParseScheme(req *http.Request) string
- func Ping(ip string, timeout time.Duration) (bool, error)
- func Pointer[T comparable](t T) *T
- func Round[T float32 | float64](value T, precision uint) T
- func RunOrDie(fn func() error)
- func SafePointer[T any](v T) T
- func SpanID() string
- func SplitByDoublePipe(data []byte, atEOF bool) (advance int, token []byte, err error)
- func StringAny(x any) string
- func StringToBytes(s string) []byte
- func Tcping(host string, port int, timeout time.Duration) bool
- func TraceFunction(ctx context.Context, functionName string, fn func(context.Context) error) error
- func TraceFunctionWithResult[T any](ctx context.Context, functionName string, fn func(context.Context) (T, error)) (T, error)
- func TraceID() string
- func ValueOf[T any](v T) *T
- type Connection
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildTLSConfig ¶
BuildTLSConfig creates a TLS configuration from the etcd config
func BytesToString ¶
BytesToString converts byte slice to string without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func CharSpliter ¶
CharSpliter is the custom split function for bufio.Scanner.
func CombineError ¶
CombineError combine error from fns.
func Deref ¶
func Deref[T any](p *T) T
Deref returns the value pointed to by the pointer. If the pointer is nil, returns the zero value for the type.
func DerefOr ¶
func DerefOr[T any](p *T, defaultVal T) T
DerefOr returns the value pointed to by the pointer. If the pointer is nil, returns the default value.
func FormatDurationSmart ¶
FormatDurationSmart formats a time.Duration into a string, picking the unit that keeps the value readable:
- Less than 1µs: nanoseconds (e.g., "900ns").
- Less than 1ms: microseconds with the specified precision (e.g., "18.40µs").
- Less than 1s: milliseconds with the specified precision (e.g., "123.451ms").
- Less than 1min: seconds with the specified precision (e.g., "2.013s").
- 1min or more: minutes with the specified precision (e.g., "1.502min").
Nanoseconds are the resolution of time.Duration itself, so that tier prints an integer and ignores precision; decimals there would only claim a resolution that does not exist.
Negative durations are supported and formatted with a '-' sign. The precision parameter controls the number of digits after the decimal point (minimum 0, maximum 9).
func GetFdFromConn ¶
GetFdFromConn get net.Conn's file descriptor.
func GetFdFromListener ¶
GetFdFromListener get net.Listener's file descriptor.
func GetFunctionName ¶
func IsConnClosed ¶
func LogDuration ¶
LogDuration is the only supported way to log how long an operation took. It emits one field pair, integer nanoseconds plus the same value rendered for reading:
{"msg": "executed query", "duration": 2120000, "duration_human": "2.12ms"}
Both halves are needed and neither pays for the other. Log stores index the nanosecond field as an integer, so "slower than 500ms" is a range filter and averages and percentiles are aggregations; because time.Duration is integral there is never a mix of integer and floating point values for the store to reconcile, and no unit is left implicit in the field name. The rendered string is what a person reads while tailing a log, where raw nanoseconds are unreadable.
Field names come from consts and are written here alone, so no call site can spell them differently and no query has to know which component produced the entry.
func ParseScheme ¶
func Ping ¶
Ping work like command `ping`. If target ip is reachable, return true, nil, If target ip is unreachable, return false, nil, If error encountered, return false, error. More usage see tests in `pkg/util/util_test.go`.
func Pointer ¶
func Pointer[T comparable](t T) *T
Pointer will return a pointer to T with given value.
func Round ¶
Round returns a rounded version of x with a specified precision.
The precision parameter specifies the number of decimal places to round to. Round uses the "round half away from zero" rule to break ties.
Examples:
Round(3.14159, 2) returns 3.14 Round(3.14159, 1) returns 3.1 Round(-3.14159, 1) returns -3.1
Example ¶
fmt.Printf("%.3f\n", Round(3.14159, 3))
fmt.Printf("%.2f\n", Round(2.71828, 2))
fmt.Printf("%.1f\n", Round(-3.14159, 1))
Output: 3.142 2.72 -3.1
func RunOrDie ¶
func RunOrDie(fn func() error)
RunOrDie runs fn and terminates the process when it returns an error. The failure is recorded in the global zap logger before the process exits, so it reaches file logs collected by log shippers; the stdout report in HandleErr alone is invisible to them, which made crash-looping startups undiagnosable from the log store.
func SafePointer ¶
func SafePointer[T any](v T) T
func SplitByDoublePipe ¶
SplitByDoublePipe is the custom split function for bufio.Scanner.
func StringToBytes ¶
StringToBytes converts string to byte slice without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.
func TraceFunction ¶
TraceFunction is a helper to trace function execution
Types ¶
type Connection ¶
func GetConnection ¶
func GetConnection(conn net.Conn) Connection