Documentation ¶
Overview ¶
Package utils 一些常用工具
Index ¶
- Constants
- Variables
- func CheckResp(resp *http.Response) error
- func FallBack(orig func() interface{}, fallback interface{}) (ret interface{})
- func FlattenMap(data map[string]interface{}, delimiter string)
- func FloorDivision(val int, divisor int) int
- func ForceGC()
- func GeneratePasswordHash(password []byte) ([]byte, error)
- func GetFuncName(f interface{}) string
- func HTTPInvalidStatusError(statusCode int) error
- func ParseTs2String(ts int64, layout string) string
- func ParseTs2Time(ts int64) time.Time
- func RandomStringWithLength(n int) string
- func RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error
- func RequestJSON(method, url string, request *RequestData, resp interface{}) (err error)
- func RequestJSONWithClient(httpClient *http.Client, method, url string, request *RequestData, ...) (err error)
- func Round(val float64, roundOn float64, places int) (newVal float64)
- func SetupClock(refreshInterval time.Duration)
- func SetupLogger(level string)
- func TemplateWithMap(tpl string, data map[string]interface{}) string
- func TemplateWithMapAndRegexp(tplReg *regexp.Regexp, tpl string, data map[string]interface{}) string
- func TriggerGC()
- func UTCNow() time.Time
- func ValidatePasswordHash(hashedPassword, password []byte) bool
- type ClockType
- type Config
- type ConfigServerCfg
- type ConfigSource
- type ConfigSrv
- func (c *ConfigSrv) Fetch() error
- func (c *ConfigSrv) Get(name string) (interface{}, bool)
- func (c *ConfigSrv) GetBool(name string) (bool, bool)
- func (c *ConfigSrv) GetInt(name string) (int, bool)
- func (c *ConfigSrv) GetString(name string) (string, bool)
- func (c *ConfigSrv) Map(set func(string, interface{}))
- type Counter
- type DivideJWT
- func (j *DivideJWT) GenerateToken(user JWTUserModel, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)
- func (j *DivideJWT) Validate(user JWTUserModel, tokenStr string) (payload jwt.MapClaims, err error)
- func (j *DivideJWT) VerifyAndReplaceExp(payload jwt.MapClaims) (err error)
- type DivideJWTCfg
- type GZCompressor
- type GZCompressorCfg
- type JWT
- func (j *JWT) Generate(expiresAt int64, payload map[string]interface{}) (string, error)
- func (j *JWT) GenerateToken(userID interface{}, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)
- func (j *JWT) Validate(tokenStr string) (payload jwt.MapClaims, err error)
- func (j *JWT) VerifyAndReplaceExp(payload map[string]interface{}) (err error)
- type JWTUserModel
- type JwtCfg
- type LoggerType
- type Mail
- type MonotonicRotateCounter
- type Mutex
- type PairList
- type RequestData
- type RotateCounter
- type SettingsType
- func (s *SettingsType) BindPFlags(p *pflag.FlagSet) error
- func (s *SettingsType) Get(key string) interface{}
- func (s *SettingsType) GetBool(key string) bool
- func (s *SettingsType) GetDuration(key string) time.Duration
- func (s *SettingsType) GetInt(key string) int
- func (s *SettingsType) GetInt64(key string) int64
- func (s *SettingsType) GetString(key string) string
- func (s *SettingsType) GetStringMap(key string) map[string]interface{}
- func (s *SettingsType) GetStringMapString(key string) map[string]string
- func (s *SettingsType) GetStringSlice(key string) []string
- func (s *SettingsType) IsSet(key string) bool
- func (s *SettingsType) LoadSettings()
- func (s *SettingsType) Set(key string, val interface{})
- func (s *SettingsType) Setup(configPath string) error
- func (s *SettingsType) SetupFromConfigServer(cfg *ConfigServerCfg) (err error)
- func (s *SettingsType) SetupFromConfigServerWithRawYaml(cfg *ConfigServerCfg, key string) (err error)
- func (s *SettingsType) SetupFromDir(dirPath string) error
- func (s *SettingsType) SetupFromFile(filePath string) error
- type SortItemItf
- type Throttle
- type ThrottleCfg
- type Uint32Counter
Examples ¶
Constants ¶
const ( HTTPJSONHeader = "Content-Type" HTTPJSONHeaderVal = "application/json" )
HTTP defines
const ( // JWTExpiresLayout default expires date stores in payload JWTExpiresLayout = time.RFC3339 // JWTUserIDKey default key of user_id stores in token payload JWTUserIDKey = "uid" // JWTExpiresAtKey default key of expires_at stores in token payload JWTExpiresAtKey = "exp" )
const SampleRateDenominator = 1000
SampleRateDenominator sample rate = sample / SampleRateDenominator
Variables ¶
var Clock = NewClock(defaultClockInterval)
Clock high performance time utils
var ( // JWTSigningMethod default method to signing JWTSigningMethod = jwt.SigningMethodHS512 )
var Settings = &SettingsType{
YamlExt: "yaml",
}
Settings is the settings for this project
Functions ¶
func FallBack ¶
func FallBack(orig func() interface{}, fallback interface{}) (ret interface{})
FallBack return the fallback when orig got error utils.FallBack(func() interface{} { return getIOStatMetric(fs) }, &IOStat{}).(*IOStat)
Example ¶
targetFunc := func() interface{} { panic("someting wrong") } utils.FallBack(targetFunc, 10) // got 10
Output:
func FlattenMap ¶
FlattenMap make embedded map into flatten map
Example ¶
data := map[string]interface{}{ "a": "1", "b": map[string]interface{}{ "c": 2, "d": map[string]interface{}{ "e": 3, }, }, } utils.FlattenMap(data, "__") // {"a": "1", "b__c": 2, "b__d__e": 3}
Output:
func FloorDivision ¶
FloorDivision (deprecated) 205//100 = 2
func GeneratePasswordHash ¶ added in v1.2.0
GeneratePasswordHash generate hashed password by origin password
Example ¶
// generate hashed password rawPassword := []byte("1234567890") hashedPassword, err := utils.GeneratePasswordHash(rawPassword) if err != nil { utils.Logger.Error("try to generate password got error", zap.Error(err)) return } fmt.Printf("got new hashed pasword: %v\n", string(hashedPassword)) // validate passowrd if !utils.ValidatePasswordHash(hashedPassword, rawPassword) { utils.Logger.Error("password invalidate", zap.Error(err)) return }
Output:
func GetFuncName ¶
func GetFuncName(f interface{}) string
GetFuncName return the name of func
Example ¶
utils.GetFuncName(foo) // "github.com/Laisky/go-utils_test.foo"
Output:
func HTTPInvalidStatusError ¶
HTTPInvalidStatusError return error about status code
func ParseTs2String ¶
ParseTs2String can parse unix timestamp(int64) to string
func ParseTs2Time ¶
ParseTs2Time can parse unix timestamp(int64) to time.Time
func RandomStringWithLength ¶
RandomStringWithLength generate random string with specific length
func RegexNamedSubMatch ¶
RegexNamedSubMatch extract key:val map from string by group match
Example ¶
reg := regexp.MustCompile(`(?P<key>\d+.*)`) str := "12345abcde" groups := map[string]string{} if err := utils.RegexNamedSubMatch(reg, str, groups); err != nil { utils.Logger.Error("try to group match got error", zap.Error(err)) } fmt.Printf("got: %+v", groups) // map[string]string{"key": 12345}
Output:
func RequestJSON ¶
func RequestJSON(method, url string, request *RequestData, resp interface{}) (err error)
RequestJSON request JSON and return JSON by default client
func RequestJSONWithClient ¶
func RequestJSONWithClient(httpClient *http.Client, method, url string, request *RequestData, resp interface{}) (err error)
RequestJSONWithClient request JSON and return JSON with specific client
func Round ¶
Round Golang does not include a round function in the standard math package Round(123.555555, .5, 3) -> 123.556
Example ¶
utils.Round(123.555555, .5, 3) // got 123.556
Output:
func SetupClock ¶
SetupClock setup internal Clock with step
func TemplateWithMap ¶ added in v1.5.4
TemplateWithMap replace `${var}` in template string
func TemplateWithMapAndRegexp ¶ added in v1.5.4
func TemplateWithMapAndRegexp(tplReg *regexp.Regexp, tpl string, data map[string]interface{}) string
TemplateWithMapAndRegexp replace `${var}` in template string
func ValidatePasswordHash ¶ added in v1.2.0
ValidatePasswordHash validate password is match with hashedPassword
Types ¶
type ClockType ¶
ClockType high performance clock with lazy refreshing
func (*ClockType) GetTimeInRFC3339Nano ¶
GetTimeInRFC3339Nano return Clock current time in string
func (*ClockType) SetupInterval ¶
SetupInterval setup update interval
type Config ¶
type Config struct { Name string `json:"name"` Profiles []string `json:"profiles"` Label string `json:"label"` Version string `json:"version"` Sources []*ConfigSource `json:"propertySources"` }
Config whole configuation return by config-server
type ConfigServerCfg ¶
type ConfigServerCfg struct { URL string // config-server api Profile string // env Label string // branch App string // app name }
ConfigServerCfg configs to connect to config-server
type ConfigSource ¶
type ConfigSource struct { Name string `json:"name"` Source map[string]interface{} `json:"source"` }
ConfigSource config item in config-server
type ConfigSrv ¶
type ConfigSrv struct { *ConfigServerCfg Cfg *Config }
ConfigSrv can load configuration from Spring-Cloud-Config-Server
Example ¶
package main import ( utils "github.com/Laisky/go-utils" ) func main() { var ( url = "http://config-server.un.org" app = "appname" profile = "sit" label = "master" ) c := utils.NewConfigSrv(&utils.ConfigServerCfg{ URL: url, App: app, Profile: profile, Label: label, }) c.Get("management.context-path") c.GetString("management.context-path") c.GetBool("endpoints.health.sensitive") c.GetInt("spring.cloud.config.retry") }
Output:
func NewConfigSrv ¶
func NewConfigSrv(cfg *ConfigServerCfg) *ConfigSrv
NewConfigSrv create ConfigSrv
type Counter ¶
Counter int64 counter
Example ¶
package main import ( utils "github.com/Laisky/go-utils" ) func main() { counter := utils.NewCounter() counter.Count() counter.CountN(10) counter.Get() // get current count }
Output:
func NewCounterFromN ¶
NewCounterFromN create Counter from custom number
type DivideJWT ¶ added in v1.5.0
type DivideJWT struct {
*DivideJWTCfg
}
DivideJWT jwt utils to generate and validate token.
use seperate secret for each token
Example ¶
jwt, err := utils.NewDivideJWT(utils.NewDivideJWTCfg()) if err != nil { utils.Logger.Panic("try to init jwt got error", zap.Error(err)) } // generate jwt token for user // GenerateToken(userId string, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error) u := &jwtUser{secret: []byte("secret for this user")} token, err := jwt.GenerateToken(u, time.Now().Add(7*24*time.Hour), map[string]interface{}{"display_name": "Laisky"}) if err != nil { utils.Logger.Error("try to generate jwt token got error", zap.Error(err)) return } fmt.Println("got token:", token) // validate token payload, err := jwt.Validate(u, token) if err != nil { utils.Logger.Error("token invalidate") // you can get the payload even the token is invalidate utils.Logger.Info("got payload", zap.String("payload", fmt.Sprint(payload))) return } fmt.Printf("got payload from token: %+v\n", payload)
Output:
func NewDivideJWT ¶ added in v1.5.0
func NewDivideJWT(cfg *DivideJWTCfg) (*DivideJWT, error)
NewDivideJWT create new JWT with JwtCfg
func (*DivideJWT) GenerateToken ¶ added in v1.5.0
func (j *DivideJWT) GenerateToken(user JWTUserModel, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)
GenerateToken generate JWT token. do not use `expires_at` & `uid` as keys.
type DivideJWTCfg ¶ added in v1.5.0
type DivideJWTCfg struct {
// contains filtered or unexported fields
}
DivideJWTCfg configuration
func NewDivideJWTCfg ¶ added in v1.5.0
func NewDivideJWTCfg() *DivideJWTCfg
NewDivideJWTCfg create new JwtCfg with secret
type GZCompressor ¶ added in v1.6.0
type GZCompressor struct { *GZCompressorCfg // contains filtered or unexported fields }
GZCompressor compress by gz with buf
func NewGZCompressor ¶ added in v1.6.0
func NewGZCompressor(cfg *GZCompressorCfg) *GZCompressor
NewGZCompressor create new GZCompressor
func (*GZCompressor) Flush ¶ added in v1.6.0
func (c *GZCompressor) Flush() (err error)
Flush flush buffer bytes into bottom writer with gz meta footer
func (*GZCompressor) Write ¶ added in v1.6.0
func (c *GZCompressor) Write(d []byte) (int, error)
Write write bytes via compressor
func (*GZCompressor) WriteString ¶ added in v1.6.0
func (c *GZCompressor) WriteString(d string) (int, error)
WriteString write string via compressor
type GZCompressorCfg ¶ added in v1.6.0
type GZCompressorCfg struct { BufSizeByte int // buf size in bytes Writer io.Writer // bottom writer }
GZCompressorCfg configuration for GZCompressor
type JWT ¶
type JWT struct {
*JwtCfg
}
JWT struct to generate and validate jwt tokens
use a global uniform secret to signing all token.
Example ¶
jwt, err := utils.NewJWT(utils.NewJWTCfg([]byte("your secret key"))) if err != nil { utils.Logger.Panic("try to init jwt got error", zap.Error(err)) } // generate jwt token for user // GenerateToken(userId string, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error) token, err := jwt.GenerateToken("laisky", time.Now().Add(7*24*time.Hour), map[string]interface{}{"display_name": "Laisky"}) if err != nil { utils.Logger.Error("try to generate jwt token got error", zap.Error(err)) return } fmt.Println("got token:", token) // validate token payload, err := jwt.Validate(token) if err != nil { utils.Logger.Error("token invalidate") return } fmt.Printf("got payload from token: %+v\n", payload)
Output:
func (*JWT) GenerateToken ¶ added in v1.2.0
func (j *JWT) GenerateToken(userID interface{}, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)
GenerateToken generate JWT token with userID(interface{})
func (*JWT) Validate ¶
Validate validate the token and return the payload
if token is invalidate, err will not be nil.
func (*JWT) VerifyAndReplaceExp ¶ added in v1.5.2
VerifyAndReplaceExp check expires and replace expires to time.Time if validated
type JWTUserModel ¶ added in v1.5.0
type JWTUserModel interface { GetUID() interface{} LoadSecretByUID(uid interface{}) ([]byte, error) }
JWTUserModel load secret by uid
type JwtCfg ¶ added in v1.3.3
type JwtCfg struct { Secret []byte // contains filtered or unexported fields }
JwtCfg configuration of JWT
type LoggerType ¶
LoggerType extend from zap.Logger
var ( // Logger logging tool. // Info(msg string, fields ...Field) // Debug(msg string, fields ...Field) // Warn(msg string, fields ...Field) // Error(msg string, fields ...Field) // Panic(msg string, fields ...Field) Logger *LoggerType )
func (*LoggerType) DebugSample ¶
func (l *LoggerType) DebugSample(sample int, msg string, fields ...zap.Field)
DebugSample emit debug log with propability sample/SampleRateDenominator. sample could be [0, 1000], less than 0 means never, great than 1000 means certainly
func (*LoggerType) InfoSample ¶
func (l *LoggerType) InfoSample(sample int, msg string, fields ...zap.Field)
InfoSample emit info log with propability sample/SampleRateDenominator
func (*LoggerType) WarnSample ¶
func (l *LoggerType) WarnSample(sample int, msg string, fields ...zap.Field)
WarnSample emit warn log with propability sample/SampleRateDenominator
type Mail ¶
type Mail struct {
// contains filtered or unexported fields
}
Mail easy way to send basic email
Example ¶
sender := utils.NewMail("smtp_host", 53) if err := sender.Send( "fromAddr", "toAddr", "frName", "toName", "Title", "Content", ); err != nil { utils.Logger.Error("try to send email got error", zap.Error(err)) }
Output:
type MonotonicRotateCounter ¶
type MonotonicRotateCounter struct {
// contains filtered or unexported fields
}
MonotonicRotateCounter monotonic increse counter uncontinuity, has much better performance than RotateCounter.
func NewMonotonicCounterFromN ¶
func NewMonotonicCounterFromN(n, rotatePoint int64) (*MonotonicRotateCounter, error)
NewMonotonicCounterFromN return new MonotonicRotateCounter with threshold from n
func NewMonotonicRotateCounter ¶
func NewMonotonicRotateCounter(rotatePoint int64) (*MonotonicRotateCounter, error)
NewMonotonicRotateCounter return new MonotonicRotateCounter with threshold from 0
func (*MonotonicRotateCounter) Count ¶
func (c *MonotonicRotateCounter) Count() (n int64)
Count increse and return the result
func (*MonotonicRotateCounter) CountN ¶
func (c *MonotonicRotateCounter) CountN(n int64) (r int64)
CountN increse N and return the result
type Mutex ¶ added in v1.3.7
type Mutex struct {
// contains filtered or unexported fields
}
Mutex mutex that support unblocking lock
Example ¶
l := utils.NewMutex() if !l.TryLock() { utils.Logger.Info("can not acquire lock") return } defer l.ForceRealse()
Output:
func (*Mutex) ForceRealse ¶ added in v1.3.7
func (m *Mutex) ForceRealse()
ForceRealse force release lock
func (*Mutex) TryRelease ¶ added in v1.3.7
TryRelease return true if succeed release
type PairList ¶
type PairList []SortItemItf
PairList array of sort items
func SortBiggest ¶
SortBiggest sort from biggest to smallest
func SortSmallest ¶
SortSmallest sort from smallest to biggest
type RequestData ¶
RequestData 发起请求的结构体
type RotateCounter ¶
type RotateCounter struct {
// contains filtered or unexported fields
}
RotateCounter rotate counter
Example ¶
package main import ( utils "github.com/Laisky/go-utils" ) func main() { counter, err := utils.NewRotateCounter(10) if err != nil { panic(err) } counter.Count() // 1 counter.CountN(10) // 1 }
Output:
func NewRotateCounter ¶
func NewRotateCounter(rotatePoint int64) (*RotateCounter, error)
NewRotateCounter create new RotateCounter with threshold from 0
func NewRotateCounterFromN ¶
func NewRotateCounterFromN(n, rotatePoint int64) (*RotateCounter, error)
NewRotateCounterFromN create new RotateCounter with threshold from N
func (*RotateCounter) Count ¶
func (c *RotateCounter) Count() int64
Count increse and return the result
func (*RotateCounter) CountN ¶
func (c *RotateCounter) CountN(n int64) (r int64)
CountN increse N and return the result
type SettingsType ¶
SettingsType type of project settings
func (*SettingsType) BindPFlags ¶
func (s *SettingsType) BindPFlags(p *pflag.FlagSet) error
BindPFlags bind pflags to settings
func (*SettingsType) Get ¶
func (s *SettingsType) Get(key string) interface{}
Get get setting by key
func (*SettingsType) GetBool ¶
func (s *SettingsType) GetBool(key string) bool
GetBool get setting by key
func (*SettingsType) GetDuration ¶
func (s *SettingsType) GetDuration(key string) time.Duration
GetDuration get setting by key
func (*SettingsType) GetInt ¶
func (s *SettingsType) GetInt(key string) int
GetInt get setting by key
func (*SettingsType) GetInt64 ¶
func (s *SettingsType) GetInt64(key string) int64
GetInt64 get setting by key
func (*SettingsType) GetString ¶
func (s *SettingsType) GetString(key string) string
GetString get setting by key
func (*SettingsType) GetStringMap ¶ added in v1.5.4
func (s *SettingsType) GetStringMap(key string) map[string]interface{}
GetStringMap return map contains interface
func (*SettingsType) GetStringMapString ¶ added in v1.5.4
func (s *SettingsType) GetStringMapString(key string) map[string]string
GetStringMapString return map contains strings
func (*SettingsType) GetStringSlice ¶
func (s *SettingsType) GetStringSlice(key string) []string
GetStringSlice get setting by key
func (*SettingsType) IsSet ¶ added in v1.5.4
func (s *SettingsType) IsSet(key string) bool
IsSet check whether exists
func (*SettingsType) LoadSettings ¶
func (s *SettingsType) LoadSettings()
LoadSettings load settings file
func (*SettingsType) Set ¶
func (s *SettingsType) Set(key string, val interface{})
Set set setting by key
func (*SettingsType) Setup ¶
func (s *SettingsType) Setup(configPath string) error
Setup load config file settings.yml
func (*SettingsType) SetupFromConfigServer ¶
func (s *SettingsType) SetupFromConfigServer(cfg *ConfigServerCfg) (err error)
SetupFromConfigServer load configs from config-server, endpoint `{url}/{app}/{profile}/{label}`
func (*SettingsType) SetupFromConfigServerWithRawYaml ¶
func (s *SettingsType) SetupFromConfigServerWithRawYaml(cfg *ConfigServerCfg, key string) (err error)
SetupFromConfigServerWithRawYaml load configs from config-server
endpoint `{url}/{app}/{profile}/{label}`
load raw yaml content and parse.
func (*SettingsType) SetupFromDir ¶
func (s *SettingsType) SetupFromDir(dirPath string) error
SetupFromDir load settings from dir, default fname is `settings.yml`
func (*SettingsType) SetupFromFile ¶
func (s *SettingsType) SetupFromFile(filePath string) error
SetupFromFile load settings from file
type SortItemItf ¶
type SortItemItf interface { GetValue() int GetKey() interface{} }
SortItemItf interface of sort item
type Throttle ¶
type Throttle struct { *ThrottleCfg // contains filtered or unexported fields }
Throttle current limitor
Example ¶
throttle := utils.NewThrottle(&utils.ThrottleCfg{ NPerSec: 10, Max: 100, }) throttle.Run() inChan := make(chan int) for msg := range inChan { if !throttle.Allow() { continue } // do something with msg fmt.Println(msg) }
Output:
type ThrottleCfg ¶
type ThrottleCfg struct {
Max, NPerSec int
}
ThrottleCfg Throttle's configuration
type Uint32Counter ¶
type Uint32Counter struct {
// contains filtered or unexported fields
}
Uint32Counter uint32 counter
func NewUint32Counter ¶
func NewUint32Counter() *Uint32Counter
NewUint32Counter return new Uint32Counter from 0
func NewUint32CounterFromN ¶
func NewUint32CounterFromN(n uint32) *Uint32Counter
NewUint32CounterFromN return new Uint32Counter from n
func (*Uint32Counter) Count ¶
func (c *Uint32Counter) Count() uint32
Count increse and return the result
func (*Uint32Counter) CountN ¶
func (c *Uint32Counter) CountN(n uint32) uint32
CountN increse N and return the result
func (*Uint32Counter) Get ¶
func (c *Uint32Counter) Get() uint32
Get return current counter's number
func (*Uint32Counter) Set ¶
func (c *Uint32Counter) Set(n uint32)
Set overwrite the counter's number
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package consistenthash contains some implementation of consistent hashing.
|
Package consistenthash contains some implementation of consistent hashing. |