Documentation ¶
Overview ¶
Package util provides utility functions and types
Index ¶
- func BytesFromString(str MutableString) []byte
- func CleanUTF8(s []byte) []byte
- func CollectFromChannel[T any](closedChan <-chan T) []T
- func CopySlice[T any](slice []T) []T
- func DeepCopyString(str string) string
- func DeepCopyStringFromBytes(str []byte) string
- func DeepCopyStrings(strList []string) []string
- func GetFDFromTCPConn(conn *net.TCPConn) (uintptr, error)
- func GetFDFromTCPConnOrPanic(conn *net.TCPConn) uintptr
- func GetYamlLocation(node *yaml.Node) string
- func IsNetworkClosed(err error) bool
- func IsNetworkError(err error) bool
- func IsNetworkTimeout(err error) bool
- func IsTestGenerationMode() bool
- func ListFiles(directoryOrFilePattern string) ([]string, error)
- func MD5ToHexdigest(content string) string
- func MarshalYaml(source interface{}) (string, error)
- func MaxInt(x int, y int) int
- func MinInt(x int, y int) int
- func NewYamlError(node *yaml.Node, message string) error
- func OverwriteNTruncate(main []byte, start int, tail string) []byte
- func ReadFileAt(dir *os.File, filename string) ([]byte, error)
- func SHA512ToHexdigest(content string) string
- func Stack() string
- func StatFileAt(dir *os.File, filename string) (unix.Stat_t, error)
- func TimeFromTimeval(val syscall.Timeval) time.Time
- func TimeToUnixFloat(tm time.Time) float64
- func TrySetTCPReadBuffer(conn *net.TCPConn, max int, min int) (int, error)
- func UnlinkFileAt(dir *os.File, filename string) error
- func UnmarshalYamlFile(path string, output interface{}) error
- func UnmarshalYamlReader(reader io.Reader, output interface{}) error
- func UnmarshalYamlString(contents string, output interface{}) error
- func WriteFileAt(dir *os.File, filename string, data []byte, perm os.FileMode) error
- type BytesPoolBy2n
- type MutableString
- type NetConnWrapper
- type RunOnce
- type TrackedWaitGroup
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesFromString ¶
func BytesFromString(str MutableString) []byte
BytesFromString makes a []byte pointing to the contents of a string.
The string must come from StringFromBytes if the new []byte is to be modified, as normal Go strings have their data allocated in the immutable memory area and any write operation would trigger panics.
func CollectFromChannel ¶
func CollectFromChannel[T any](closedChan <-chan T) []T
CollectFromChannel collect remaining items from a CLOSED channel
func CopySlice ¶
func CopySlice[T any](slice []T) []T
CopySlice copies the slice to a newly-allocated one
func DeepCopyString ¶
DeepCopyString copies the given string to a newly-allocated one
Without references to the original backing bytes
func DeepCopyStringFromBytes ¶
DeepCopyStringFromBytes copies the given []byte to a newly-allocated string
Without references to the original backing bytes
func DeepCopyStrings ¶
DeepCopyStrings copies the given string list including each of item to newly allocated fields
Without references to the original backing slices
func GetFDFromTCPConn ¶
GetFDFromTCPConn reads socket FD from the given connection
func GetFDFromTCPConnOrPanic ¶
GetFDFromTCPConnOrPanic tries to get socket FD from the given connection or panic
The connection must have been established first
func GetYamlLocation ¶
func GetYamlLocation(node *yaml.Node) string
GetYamlLocation fetches a descriptive location of YAML node
func IsNetworkClosed ¶
IsNetworkClosed checks if the given error tells closing of network connection
func IsNetworkError ¶
IsNetworkError checks whether the given error should be considered a network issue, as opposed to e.g. expired certificate or permission denied
DO NOT use net.Error directly, as we may need to check for other error types in future
func IsNetworkTimeout ¶
IsNetworkTimeout checks if the given error is network timeout
func IsTestGenerationMode ¶
func IsTestGenerationMode() bool
IsTestGenerationMode returns true if we're running in "go test -args gen" to generate expected test outputs
func ListFiles ¶
ListFiles lists non-dir files or first level files under the directories in the given path pattern
func MD5ToHexdigest ¶
MD5ToHexdigest computes MD5 for given string and returns hex
func MarshalYaml ¶
MarshalYaml marshals the given source to a YAML string
func NewYamlError ¶
NewYamlError creates a new error with location information of YAML node
func ReadFileAt ¶
ReadFileAt reads full contents of a file in given directory
func SHA512ToHexdigest ¶
SHA512ToHexdigest computes SHA512 for given string and returns hex
func StatFileAt ¶
StatFileAt queries the stat of an existing file in given directory
func TimeFromTimeval ¶
TimeFromTimeval creates a Time structure from syscall.Timeval
func TimeToUnixFloat ¶
TimeToUnixFloat creates Unix epoch seconds from a Time structure
func TrySetTCPReadBuffer ¶
TrySetTCPReadBuffer attempts to set read buffer within the range given
func UnlinkFileAt ¶
UnlinkFileAt unlinks an existing file in given directory
func UnmarshalYamlFile ¶
UnmarshalYamlFile loads and unmarshals YAML from file to interface or pointer to struct
func UnmarshalYamlReader ¶
UnmarshalYamlReader loads and unmarshals YAML from IO reader to interface or pointer to struct
func UnmarshalYamlString ¶
UnmarshalYamlString loads and unmarshals YAML in string to interface or pointer to struct
Types ¶
type BytesPoolBy2n ¶
BytesPoolBy2n is a set of pools by size
BytesPoolBy2n[1] = pool of make([]byte, 2) BytesPoolBy2n[2] = pool of make([]byte, 4) BytesPoolBy2n[8] = pool of make([]byte, 256) ... Local-cache with buffers of recycled []byte has been tried and made minimal improvement
func NewBytesPoolBy2n ¶
func NewBytesPoolBy2n() BytesPoolBy2n
NewBytesPoolBy2n creates a BytesPoolBy2n with all pools initialized
func (BytesPoolBy2n) Get ¶
func (pools BytesPoolBy2n) Get(length int) *[]byte
Get fetches an empty byte array suitable for given length of data The array returned may be longer than the given length
func (BytesPoolBy2n) Put ¶
func (pools BytesPoolBy2n) Put(buf *[]byte)
Put recycles the given byte array into pool The array specified must come from BytesPoolBy2n.Get()
type MutableString ¶
type MutableString = string
MutableString is a string backed by raw []byte, instead of in the immutable memory area like normal Go strings.
Its contents may be changed. But we cannot create a new type or string functions wouldn't work with it.
func StringFromBytes ¶
func StringFromBytes(buf []byte) MutableString
StringFromBytes makes a string backed by a specified []byte.
There is no copying and the resulting string shares the same []byte contents.
If data in the backing slice is changed, the string contents would reflect the changes (NOT normal Go string behavior).
DO NOT use this in tests.
type NetConnWrapper ¶
type NetConnWrapper struct {
// contains filtered or unexported fields
}
NetConnWrapper wraps a connection with with timeouts updated infrequently in trade of accuracy The real timeouts could be anything from specified timeout values to double of them
func WrapNetConn ¶
func WrapNetConn(conn net.Conn, readTimeout time.Duration, writeTimeout time.Duration) *NetConnWrapper
WrapNetConn creates a NetConnWrapper for given network connection
func (*NetConnWrapper) ReadDeadline ¶
func (cw *NetConnWrapper) ReadDeadline() time.Time
ReadDeadline returns the current read deadline
func (*NetConnWrapper) WriteDeadline ¶
func (cw *NetConnWrapper) WriteDeadline() time.Time
WriteDeadline returns the current write deadline
type RunOnce ¶
type RunOnce func(beforeRunning func())
RunOnce is a function wrapper that calls the underlying function at most once
beforeRunning is invoked right before the underlying function is invoked and only if it's going to be invoked. It may be nil.
This can be used to protect e.g. resource closing or cleanup, which should be called exactly once
func NewRunOnce ¶
func NewRunOnce(f func()) RunOnce
NewRunOnce creates a function that would call the given "f" at most once
type TrackedWaitGroup ¶
type TrackedWaitGroup struct {
// contains filtered or unexported fields
}
func (*TrackedWaitGroup) Add ¶
func (twg *TrackedWaitGroup) Add(delta int)
func (*TrackedWaitGroup) Done ¶
func (twg *TrackedWaitGroup) Done()
func (*TrackedWaitGroup) Peek ¶
func (twg *TrackedWaitGroup) Peek() int
func (*TrackedWaitGroup) Wait ¶
func (twg *TrackedWaitGroup) Wait()
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package localcachedmap provides a map with thread-local caches (copy-on-reference)
|
Package localcachedmap provides a map with thread-local caches (copy-on-reference) |
Package stringtemplate provides string expansion by pre-compiled templates, for example:
|
Package stringtemplate provides string expansion by pre-compiled templates, for example: |
Package stringunescape provides Unescaper(s) for escaped strings
|
Package stringunescape provides Unescaper(s) for escaped strings |