Documentation
¶
Overview ¶
Package inutil provides common utility functions for identifier generation, byte manipulation, JSON helpers, network operations, statistics, and time formatting.
Index ¶
- Constants
- func FormatUptime(v int64) string
- func FsMakeDir(path string, uid, gid int, mode os.FileMode) error
- func FsMakeFileDir(path string, uid, gid int, mode os.FileMode) error
- func FsWrite(file string, bs []byte) error
- func GenerateSecretKey(byteSize int) (string, error)
- func GenerateSecretKeyBase62(length int) (string, error)
- func GenerateSecretKeyBase64(byteSize int) (string, error)
- func HashToHexString(bs []byte, strlen int) string
- func IsLocalIP(ip string) bool
- func JsonDecodeFromFile(path string, v interface{}) error
- func JsonEncodeToFile(path string, v interface{}, perm os.FileMode) error
- func JsonEncodeToFileIndent(path string, v interface{}, perm os.FileMode) error
- func LookupPrivateIP() (string, error)
- func ParseBytes(s string) (int64, error)
- func ParseCPUs(s string) (int64, error)
- func PrettyBytes(v, base int64) string
- func PrettyCPUs(value int64) string
- func RandHexString(n int) string
- func SeqRandHexString(slen, rlen int) string
- func Uint32ToHexString(v uint32) string
- type GroupSlidingCounter
- type SlidingCounter
Constants ¶
const ( Byte int64 = 1 // IEC units (base 1024) KiByte = 1024 * Byte MiByte = 1024 * KiByte GiByte = 1024 * MiByte TiByte = 1024 * GiByte PiByte = 1024 * TiByte EiByte = 1024 * PiByte // SI units (base 1000) KByte = 1000 * Byte MByte = 1000 * KByte GByte = 1000 * MByte TByte = 1000 * GByte PByte = 1000 * TByte EByte = 1000 * PByte )
const ( Window1Min int64 = 60 Window5Min int64 = 300 Window15Min int64 = 900 )
Variables ¶
This section is empty.
Functions ¶
func FormatUptime ¶
func GenerateSecretKey ¶
GenerateSecretKey generates a cryptographically random secret key encoded as a hexadecimal string. The output length is byteSize*2 characters.
Use this for generating API keys, tokens, or other secret values that require hexadecimal encoding.
func GenerateSecretKeyBase62 ¶
GenerateSecretKeyBase62 generates a cryptographically random key composed entirely of base62 characters (0-9, a-z, A-Z), producing a URL-safe, human-friendly identifier of the specified length.
To ensure uniform distribution and eliminate modulo bias, only random bytes in the range [0, maxValidByte) are accepted. Since 256 % 62 = 8, the maximum valid byte value is 248, guaranteeing that each of the 62 characters has an exactly equal probability of being selected.
An over-allocated random buffer (length/10 + 1 extra bytes) is used per iteration to minimize the number of syscalls to crypto/rand, as roughly 256-248 = 3.1% of random bytes will be rejected.
func GenerateSecretKeyBase64 ¶
GenerateSecretKeyBase64 generates a cryptographically random secret key encoded using URL-safe base64 (RFC 4648 §5). The output length is ceil(byteSize * 4 / 3) + padding characters.
Compared to hex encoding, base64 produces a shorter string for the same amount of entropy, making it suitable for space-constrained contexts such as URL parameters or HTTP headers.
func HashToHexString ¶
func JsonDecodeFromFile ¶
JsonDecodeFromFile reads JSON data from a file and decodes it into the provided value. The value must be a pointer to a JSON-serializable type.
func JsonEncodeToFile ¶
JsonEncodeToFile encodes the provided value to JSON and writes it to a file. The value must be a JSON-serializable type.
func JsonEncodeToFileIndent ¶
JsonEncodeToFileIndent encodes the provided value to JSON with indentation and writes it to a file. The value must be a JSON-serializable type.
func LookupPrivateIP ¶
LookupPrivateIP returns the first private IPv4 address of this host.
func ParseBytes ¶
ParseBytes parses human-readable byte string to int64.
func PrettyBytes ¶
PrettyBytes formats bytes into human-readable string.
func PrettyCPUs ¶
func RandHexString ¶
RandHexString generates a cryptographically random hexadecimal string using crypto/rand. The returned string length is n*2 characters (2 hex chars per byte).
Returns an empty string if the random source fails (should be exceedingly rare).
func SeqRandHexString ¶
SeqRandHexString generates a semi-sequential, unique identifier by combining a timestamp-derived hex prefix with a cryptographically random hex suffix.
This design ensures rough temporal ordering of generated IDs while maintaining sufficient randomness to avoid collisions, making it suitable for use cases such as resource identifiers that benefit from time-based sortability.
Parameters:
- slen: desired length of the sequential (timestamp) prefix in hex characters. Must be even; clamped to the range [2, 8]. The full Unix timestamp yields 8 hex characters (e.g., 0x6789ABCD). Shorter values truncate the most significant digits, reducing uniqueness granularity.
- rlen: desired length of the random suffix in hex characters. Must be even; clamped to the range [2, 1024].
The resulting string length is slen + rlen characters (or slen + rlen + 1 if either parameter was odd and got rounded up).
Example output with slen=4, rlen=12: "6789" + "a1b2c3d4e5f6"
func Uint32ToHexString ¶
Uint32ToHexString encodes a uint32 value into an 8-character hexadecimal string using big-endian byte order. The output is always fixed-length (8 hex chars).
Example: 0x1A2B3C4D → "1a2b3c4d"
Types ¶
type GroupSlidingCounter ¶
type GroupSlidingCounter struct {
// contains filtered or unexported fields
}
GroupSlidingCounter manages multiple named SlidingCounters.
func NewGroupSlidingCounter ¶
func NewGroupSlidingCounter(maxSeconds int64) *GroupSlidingCounter
func (*GroupSlidingCounter) Counter ¶
func (c *GroupSlidingCounter) Counter(name string) *SlidingCounter
type SlidingCounter ¶
type SlidingCounter struct {
// contains filtered or unexported fields
}
SlidingCounter implements a ring buffer for time-series delta calculations.
func NewSlidingCounter ¶
func NewSlidingCounter(maxSeconds int64) *SlidingCounter
NewSlidingCounter creates a counter with the specified window size.
func (*SlidingCounter) Delta ¶
func (c *SlidingCounter) Delta(seconds int64) int64
Delta returns the increment over the past N seconds.
func (*SlidingCounter) Record ¶
func (c *SlidingCounter) Record(totalValue int64) *SlidingCounter
Record stores a cumulative value and returns the counter for chaining.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package autofill provides auto-fill value generation for AppSpecConfigItem auto-fill expressions.
|
Package autofill provides auto-fill value generation for AppSpecConfigItem auto-fill expressions. |