inutil

package
v2.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package inutil provides common utility functions for identifier generation, byte manipulation, JSON helpers, network operations, statistics, and time formatting.

Index

Constants

View Source
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
)
View Source
const (
	Window1Min  int64 = 60
	Window5Min  int64 = 300
	Window15Min int64 = 900
)

Variables

This section is empty.

Functions

func FormatUptime

func FormatUptime(v int64) string

func FsMakeDir

func FsMakeDir(path string, uid, gid int, mode os.FileMode) error

func FsMakeFileDir

func FsMakeFileDir(path string, uid, gid int, mode os.FileMode) error

func FsWrite

func FsWrite(file string, bs []byte) error

func GenerateSecretKey

func GenerateSecretKey(byteSize int) (string, error)

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

func GenerateSecretKeyBase62(length int) (string, error)

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

func GenerateSecretKeyBase64(byteSize int) (string, error)

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 HashToHexString(bs []byte, strlen int) string

func IsLocalIP

func IsLocalIP(ip string) bool

IsLocalIP checks if the given IP belongs to this host.

func JsonDecodeFromFile

func JsonDecodeFromFile(path string, v interface{}) error

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

func JsonEncodeToFile(path string, v interface{}, perm os.FileMode) error

JsonEncodeToFile encodes the provided value to JSON and writes it to a file. The value must be a JSON-serializable type.

func JsonEncodeToFileIndent

func JsonEncodeToFileIndent(path string, v interface{}, perm os.FileMode) error

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

func LookupPrivateIP() (string, error)

LookupPrivateIP returns the first private IPv4 address of this host.

func ParseBytes

func ParseBytes(s string) (int64, error)

ParseBytes parses human-readable byte string to int64.

func ParseCPUs

func ParseCPUs(s string) (int64, error)

func PrettyBytes

func PrettyBytes(v, base int64) string

PrettyBytes formats bytes into human-readable string.

func PrettyCPUs

func PrettyCPUs(value int64) string

func RandHexString

func RandHexString(n int) string

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

func SeqRandHexString(slen, rlen int) string

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

func Uint32ToHexString(v uint32) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL