utils

package module
v1.9.4 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 35 Imported by: 22

README

Go-Utils

Many useful golang tools, support >= v1.12.

GitHub release Commitizen friendly Go Report Card GoDoc Build Status codecov

Install:

go get github.com/Laisky/go-utils

Usage

import (
    "github.com/Laisky/go-utils"
)

There are many useful tools including:

  • Clock: high performance lazy load clock
    • create
      • NewClock(context.Context, time.Duration) *ClockType
    • use
      • Clock.Close()
      • Clock.GetUTCNow
      • Clock.GetTimeInRFC3339Nano
      • Clock.SetupInterval(time.Duration)
      • Clock.GetTimeInHex() string
      • Clock.GetNanoTimeInHex() string
  • Settings: configuration manager that support yml and spring-cloud-config-server
    • setup
      • SetupFromDir(dirPath string) error
      • SetupFromFile(filePath string) error
      • SetupFromConfigServer(cfg *ConfigServerCfg) (err error)
      • SetupFromConfigServerWithRawYaml(cfg *ConfigServerCfg, key string)
    • use
      • Settings.Get(key string) interface{}
      • Settings.GetString(key string) string
      • Settings.GetStringSlice(key string) []string
      • Settings.GetBool(key string) bool
      • Settings.GetInt(key string) int
      • Settings.GetInt64(key string) int64
      • Settings.GetDuration(key string) time.Duration
      • Settings.Set(key string, val interface{})
      • Settings.IsSet(key string) bool
      • Settings.GetStringMap(key string) map[string]interface{}
      • Settings.GetStringMapString(key string) map[string]string
  • Counter: counter and rotate counter
    • create
      • NewCounter() *Counter
      • NewCounterFromN(n int64) *Counter
      • NewRotateCounterWithCtx(ctx context.Context, rotatePoint int64) (*RotateCounter, error)
      • NewRotateCounterFromNWithCtx(ctx context.Context, n, rotatePoint int64) (*RotateCounter, error)
      • NewUint32Counter() *Uint32Counter
      • NewUint32CounterFromN(n uint32) *Uint32Counter
      • NewParallelCounter(quoteStep, rotatePoint int64) (*ParallelCounter, error)
      • NewParallelCounterFromN(n, quoteStep, rotatePoint int64) (*ParallelCounter, error)
  • Mail: simply email sender
    • create
      • NewMail(host string, port int) *Mail
    • use
      • Mail.Login(username, password string)
      • Mail.Send(frAddr, toAddr, frName, toName, subject, content string) (err error)
  • encrypt.go:
    • create:
      • NewJWT(cfg *JwtCfg) (*JWT, error)
    • use:
      • GeneratePasswordHash(): generate hashed password
      • ValidatePasswordHash(): validate hashed password
      • JWT.GenerateToken(userID interface{}, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)
      • JWT.Validate(tokenStr string) (payload jwt.MapClaims, err error)
  • RequestJSON: simply http client that send json request and unmarshal response by json
  • Logger: high performance structrued logger based by zap
    • setup:
      • Logger.ChangeLevel(level string) (err error)
    • use
      • Logger.Info(msg string, fields ...zap.Field)
      • Logger.Debug(msg string, fields ...zap.Field)
      • Logger.Warn(msg string, fields ...zap.Field)
      • Logger.Error(msg string, fields ...zap.Field)
      • Logger.Panic(msg string, fields ...zap.Field)
      • Logger.DebugSample(sample int, msg string, fields ...zap.Field)
      • Logger.InfoSample(sample int, msg string, fields ...zap.Field)
      • Logger.WarnSample(sample int, msg string, fields ...zap.Field)
  • AlertHook add laisky-alert-bot pusher hook to Logger
  • Math: some simply math functions
    • Round(val float64, roundOn float64, places int) (newVal float64): get round of float
  • Throttle: throttling to limit throughput
  • time: some useful time functions
    • UTCNow() time.Time
    • ParseUnix2String(int64) string
    • ParseUnix2UTC(int64) time.Time
    • ParseUnixNano2UTC(int64) time.Time
    • ParseHex2UTC(string) time.Time
    • ParseHexNano2UTC(string) time.Time
  • utils: some tool functions
    • URLMasking(url, mask string) string
    • GetFuncName(f interface{}) string
    • FallBack(orig func() interface{}, fallback interface{}) (ret interface{})
    • RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error
    • FlattenMap(data map[string]interface{}, delimiter string)
    • ForceGCBlocking()
    • ForceGCUnBlocking()
    • TemplateWithMap(tpl string, data map[string]interface{}) string
  • GZCompressor
    • create
      • NewGZCompressor(cfg *GZCompressorCfg) (c *GZCompressor, err error)
    • use
      • GZCompressor.Write(d []byte) (int, error)
      • GZCompressor.WriteString(d string) (int, error)
      • GZCompressor.Flush() (err error)
      • GZCompressor.WriteFooter() (err error)

see more examples in tests or document

Usage

some examples

Settings

load settings from commandline arguments, yaml file or spring-cloud-config-server, then use it anywhere.

  1. load settings from commandline arguments:

    func setupArgs() {
        pflag.Bool("debug", false, "run in debug mode")
        pflag.Bool("dry", false, "run in dry mode")
        pflag.String("config", "/etc/go-fluentd/settings", "config file directory path")
        pflag.Parse()
        utils.Settings.BindPFlags(pflag.CommandLine)
    }
    
    func main() {
        // load settings
        setupArgs()
    
        // use settings anywhere
        if utils.Settings.GetBool("debug") {
            fmt.Println("run in debug mode")
        }
    }
    
  2. load settings from yaml file:

    prepare settings file:

    mkdir -p /etc/your-app-name/
    echo "key: val" > /etc/your-app-name/settings.yml
    

    load from yaml file:

    // load from yaml file
    utils.SetupFromFile("/etc/your-app-name/settings.yml")
    
    // load from directory (with default filename `settings.yml`)
    utils.Setup("/etc/your-app-name")
    

after loading, then you can use utils.Settings anywhere:


import "github.com/Laisky/go-utils"

func foo() {
    // set
    utils.Settings.Set("key", "anything")

    // get (latest setted value)
    utils.Settings.Get("key")  // return interface
    utils.Settings.GetString("key")  // return string
    utils.Settings.GetStringSlice("key")  // return []string
    utils.Settings.GetBool("key")  // return bool
    utils.Settings.GetInt64("key")  // return int64
    utils.Settings.GetDuration("key")  // return time.Duration
}
Logger

high performance and simple logging tool based on zap.

// setup basic log level
utils.Logger.ChangeLevel("info")  // info/debug/warn/error

// use as zap
utils.Logger.Debug("some msg", zap.String("key", "val"))
utils.Logger.Info("some msg", zap.String("key", "val"))
utils.Logger.Warn("some msg", zap.String("key", "val"))
utils.Logger.Error("some msg", zap.String("key", "val"))
utils.Logger.Panic("some msg", zap.String("key", "val"))  // will raise panic
Clock

high performance time.Now especially on heavy load.

BenchmarkClock/normal_time-4         	20000000	       107 ns/op	       0 B/op	       0 allocs/op
BenchmarkClock/clock_time_with_500ms-4         	20000000	        62.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkClock/clock_time_with_100ms-4         	20000000	        64.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkClock/clock_time_with_1ms-4           	20000000	        69.1 ns/op	       0 B/op	       0 allocs/op

usage:

// use default clock (update per 500ms)
utils.Clock.GetUTCNow()


// setup custom Clock
clock := utils.NewClock(1 * time.Second)
clock.GetUTCNow()
Encrypt

JWT token and hashed password tools.

  1. generate and validate JWT token

    Introduction to JSON Web Tokens

    func ExampleJWT() {
        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)
    }
    
  2. generate and validate hashed password

    Why should I hash passwords?

    func ExampleGeneratePasswordHash() {
        // 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
        }
    }
    
Math

some useful math functions

  1. Round

    func ExampleRound() {
        utils.Round(123.555555, .5, 3) // got 123.556
    }
    
Utils

some useful funtions

  1. GetFuncName(f interface{}) string

    func foo() {}
    
    func ExampleGetFuncName() {
        utils.GetFuncName(foo) // "github.com/Laisky/go-utils_test.foo"
    }
    
  2. FallBack(orig func() interface{}, fallback interface{}) (ret interface{})

    return fallback if origin func got error.

    func ExampleFallBack() {
        targetFunc := func() interface{} {
            panic("someting wrong")
        }
    
        utils.FallBack(targetFunc, 10) // got 10
    }
    
  3. RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error

    func ExampleRegexNamedSubMatch() {
        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}
    }
    
  4. FlattenMap(data map[string]interface{}, delimiter string)

    func ExampleFlattenMap() {
        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}
    }
    

Documentation

Overview

Package utils 一些常用工具

Index

Examples

Constants

View Source
const (
	HTTPJSONHeader    = "Content-Type"
	HTTPJSONHeaderVal = "application/json"
)

HTTP defines

View Source
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"
)
View Source
const SampleRateDenominator = 1000

SampleRateDenominator sample rate = sample / SampleRateDenominator

Variables

View Source
var (
	// ParseTs2UTC can parse unix timestamp(int64) to time.Time
	ParseTs2UTC = ParseUnix2UTC
	// ParseTs2String can parse unix timestamp(int64) to string
	ParseTs2String = ParseUnix2String
)
View Source
var (
	// ParseTs2Time can parse unix timestamp(int64) to time.Time
	ParseTs2Time = ParseTs2UTC
	// UnixNano2UTC convert unixnano to UTC time
	UnixNano2UTC = ParseUnixNano2UTC
)
View Source
var (
	// Clock high performance time utils, replace Clock1
	Clock = NewClock(context.Background(), defaultClockInterval)

	// Clock2 high performance time utils
	Clock2 = Clock
	// NewClock2 create new Clock
	NewClock2 = NewClock
)
View Source
var (
	// ForceGCBlocking force to start gc blocking
	ForceGCBlocking = ForceGC
	// ForceGCUnBlocking force to start gc unblocking
	ForceGCUnBlocking = TriggerGC
)
View Source
var (
	// JWTSigningMethod default method to signing
	JWTSigningMethod = jwt.SigningMethodHS512
)
View Source
var Settings = &SettingsType{
	YamlExt: "yaml",
}

Settings is the settings for this project

Functions

func CheckResp

func CheckResp(resp *http.Response) error

CheckResp check HTTP response's status code and return the error with body message

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

func FlattenMap(data map[string]interface{}, delimiter string)

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 ForceGC added in v1.3.7

func ForceGC()

ForceGC force to run blocking manual gc.

func GeneratePasswordHash added in v1.2.0

func GeneratePasswordHash(password []byte) ([]byte, error)

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

func HTTPInvalidStatusError(statusCode int) error

HTTPInvalidStatusError return error about status code

func ParseHex2UTC added in v1.8.0

func ParseHex2UTC(ts string) (t time.Time, err error)

ParseHex2UTC parse hex to UTC time

func ParseHexNano2UTC added in v1.8.0

func ParseHexNano2UTC(ts string) (t time.Time, err error)

ParseHexNano2UTC parse hex contains nano to UTC time

func ParseUnix2String added in v1.8.0

func ParseUnix2String(ts int64, layout string) string

ParseUnix2String can parse unix timestamp(int64) to string

func ParseUnix2UTC added in v1.8.0

func ParseUnix2UTC(ts int64) time.Time

ParseUnix2UTC convert unix to UTC time

func ParseUnixNano2UTC added in v1.8.0

func ParseUnixNano2UTC(ts int64) time.Time

ParseUnixNano2UTC convert unixnano to UTC time

func RandomStringWithLength

func RandomStringWithLength(n int) string

RandomStringWithLength generate random string with specific length

func RegexNamedSubMatch

func RegexNamedSubMatch(r *regexp.Regexp, str string, subMatchMap map[string]string) error

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

func Round(val float64, roundOn float64, places int) (newVal float64)

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

func SetupClock(refreshInterval time.Duration)

SetupClock setup internal Clock with step

func TemplateWithMap added in v1.5.4

func TemplateWithMap(tpl string, data map[string]interface{}) string

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 TriggerGC added in v1.3.8

func TriggerGC()

TriggerGC trigger GC unblocking

func URLMasking added in v1.9.3

func URLMasking(url, mask string) string

URLMasking masking password in url

func UTCNow

func UTCNow() time.Time

UTCNow 获取当前 UTC 时间

func ValidatePasswordHash added in v1.2.0

func ValidatePasswordHash(hashedPassword, password []byte) bool

ValidatePasswordHash validate password is match with hashedPassword

Types

type AlertHook added in v1.9.0

type AlertHook struct {
	// contains filtered or unexported fields
}

AlertHook hook for zap.Logger

Example
package main

import (
	"context"
	"time"

	utils "github.com/Laisky/go-utils"
	zap "github.com/Laisky/zap"
)

func main() {
	pusher, err := utils.NewAlertPusherWithAlertType(
		context.Background(),
		"https://blog.laisky.com/graphql/query/",
		"hello",
		"rwkpVuAgaBZQBASKndHK",
	)
	if err != nil {
		utils.Logger.Panic("create alert pusher", zap.Error(err))
	}
	defer pusher.Close()
	hook := utils.NewAlertHook(
		pusher,
		utils.WithAlertHookLevel(zap.WarnLevel),
	)
	logger, err := utils.NewLogger(
		"debug",
		zap.Fields(zap.String("logger", "test")),
		zap.HooksWithFields(hook.GetZapHook()),
	)
	if err != nil {
		utils.Logger.Error("create new logger", zap.Error(err))
	}

	logger.Debug("DEBUG", zap.String("yo", "hello"))
	logger.Info("Info", zap.String("yo", "hello"))
	logger.Warn("Warn", zap.String("yo", "hello"))
	logger.Error("Error", zap.String("yo", "hello"))

	time.Sleep(1 * time.Second)
}
Output:

func NewAlertHook added in v1.9.0

func NewAlertHook(pusher *AlertPusher, opts ...AlertHookOption) (a *AlertHook)

NewAlertHook create AlertHook

func (*AlertHook) GetZapHook added in v1.9.0

func (a *AlertHook) GetZapHook() func(zapcore.Entry, []zapcore.Field) (err error)

GetZapHook get hook for zap logger

type AlertHookOption added in v1.9.0

type AlertHookOption func(*AlertHook)

AlertHookOption option for create AlertHook

func WithAlertHookLevel added in v1.9.0

func WithAlertHookLevel(level zapcore.Level) AlertHookOption

WithAlertHookLevel level to trigger AlertHook

type AlertPushOption added in v1.9.0

type AlertPushOption func(*AlertPusher)

AlertPushOption is AlertPusher's options

func WithAlertPushTimeout added in v1.9.0

func WithAlertPushTimeout(timeout time.Duration) AlertPushOption

WithAlertPushTimeout set AlertPusher HTTP timeout

type AlertPusher added in v1.9.0

type AlertPusher struct {
	// contains filtered or unexported fields
}

AlertPusher send alert to laisky's alert API

https://github.com/Laisky/laisky-blog-graphql/tree/master/telegram

func NewAlertPusher added in v1.9.0

func NewAlertPusher(ctx context.Context, pushAPI string, opts ...AlertPushOption) (a *AlertPusher, err error)

NewAlertPusher create new AlertPusher

func NewAlertPusherWithAlertType added in v1.9.0

func NewAlertPusherWithAlertType(ctx context.Context, pushAPI string, alertType, pushToken string, opts ...AlertPushOption) (a *AlertPusher, err error)

NewAlertPusherWithAlertType create new AlertPusher with default type and token

func (*AlertPusher) Close added in v1.9.0

func (a *AlertPusher) Close()

Close close AlertPusher

func (*AlertPusher) Send added in v1.9.0

func (a *AlertPusher) Send(msg string) (err error)

Send send with default alertType and pushToken

func (*AlertPusher) SendWithType added in v1.9.0

func (a *AlertPusher) SendWithType(alertType, pushToken, msg string) (err error)

SendWithType send alert with specific type, token and msg

type ChildParallelCounter added in v1.6.3

type ChildParallelCounter struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ChildParallelCounter child of ParallelCounter

func (*ChildParallelCounter) Count added in v1.6.3

func (c *ChildParallelCounter) Count() (r int64)

Count count 1

func (*ChildParallelCounter) CountN added in v1.6.3

func (c *ChildParallelCounter) CountN(n int64) (r int64)

CountN count n

func (*ChildParallelCounter) Get added in v1.6.3

func (c *ChildParallelCounter) Get() int64

Get get current count

type Clock2Type added in v1.7.3

type Clock2Type ClockType

Clock2Type high performance clock with lazy refreshing

type ClockItf added in v1.7.3

type ClockItf interface {
	GetTimeInRFC3339Nano() string
	GetUTCNow() time.Time
	SetupInterval(time.Duration)
	Close()
}

ClockItf high performance lazy clock

type ClockType

type ClockType struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ClockType high performance clock with lazy refreshing

func NewClock

func NewClock(ctx context.Context, refreshInterval time.Duration) *ClockType

NewClock create new Clock

func (*ClockType) Close added in v1.7.3

func (c *ClockType) Close()

Close stop Clock update

func (*ClockType) GetNanoTimeInHex added in v1.8.0

func (c *ClockType) GetNanoTimeInHex() string

GetNanoTimeInHex return current time with nano in hex

func (*ClockType) GetTimeInHex added in v1.8.0

func (c *ClockType) GetTimeInHex() string

GetTimeInHex return current time in hex

func (*ClockType) GetTimeInRFC3339Nano

func (c *ClockType) GetTimeInRFC3339Nano() string

GetTimeInRFC3339Nano return Clock current time in string

func (*ClockType) GetUTCNow

func (c *ClockType) GetUTCNow() (t time.Time)

GetUTCNow return Clock current time.Time

func (*ClockType) SetupInterval

func (c *ClockType) SetupInterval(interval time.Duration)

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

func (*ConfigSrv) Fetch

func (c *ConfigSrv) Fetch() error

Fetch load data from config-server

func (*ConfigSrv) Get

func (c *ConfigSrv) Get(name string) (interface{}, bool)

Get get `interface{}` from the localcache of config-server

func (*ConfigSrv) GetBool

func (c *ConfigSrv) GetBool(name string) (bool, bool)

GetBool get `bool` from the localcache of config-server

func (*ConfigSrv) GetInt

func (c *ConfigSrv) GetInt(name string) (int, bool)

GetInt get `int` from the localcache of config-server

func (*ConfigSrv) GetString

func (c *ConfigSrv) GetString(name string) (string, bool)

GetString get `string` from the localcache of config-server

func (*ConfigSrv) Map

func (c *ConfigSrv) Map(set func(string, interface{}))

Map interate `set(k, v)`

type Counter

type Counter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

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 NewCounter

func NewCounter() *Counter

NewCounter create Counter from 0

func NewCounterFromN

func NewCounterFromN(n int64) *Counter

NewCounterFromN create Counter from custom number

func (*Counter) Count

func (c *Counter) Count() int64

Count increse and return the result

func (*Counter) CountN

func (c *Counter) CountN(n int64) int64

CountN increse N and return the result

func (*Counter) Get

func (c *Counter) Get() int64

Get return current counter's number

func (*Counter) GetSpeed

func (c *Counter) GetSpeed() (r float64)

GetSpeed return increasing speed from lastest invoke `GetSpeed`

func (*Counter) Set

func (c *Counter) Set(n int64)

Set overwrite the counter's number

type CtxKeyT added in v1.7.0

type CtxKeyT struct{}

CtxKeyT type of context key

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.

func (*DivideJWT) Validate added in v1.5.0

func (j *DivideJWT) Validate(user JWTUserModel, tokenStr string) (payload jwt.MapClaims, err error)

Validate validate the token and return the payload

if token is invalidate, err will not be nil.

func (*DivideJWT) VerifyAndReplaceExp added in v1.5.2

func (j *DivideJWT) VerifyAndReplaceExp(payload jwt.MapClaims) (err error)

VerifyAndReplaceExp check expires and replace expires to time.Time if validated

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) (c *GZCompressor, err error)

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) WriteFooter added in v1.6.2

func (c *GZCompressor) WriteFooter() (err error)

WriteFooter write gz footer

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
	GzLevel     int
}

GZCompressorCfg configuration for GZCompressor

type Int64CounterItf added in v1.7.8

type Int64CounterItf interface {
	Count() int64
	CountN(n int64) int64
}

Int64CounterItf counter for int64

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 NewJWT added in v1.3.3

func NewJWT(cfg *JwtCfg) (*JWT, error)

NewJWT create new JWT with JwtCfg

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

func (j *JWT) Validate(tokenStr string) (payload jwt.MapClaims, err error)

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

func (j *JWT) VerifyAndReplaceExp(payload map[string]interface{}) (err error)

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

func NewJWTCfg added in v1.3.3

func NewJWTCfg(secret []byte) *JwtCfg

NewJWTCfg create new JwtCfg with secret

type LoggerType

type LoggerType struct {
	*zap.Logger
	// contains filtered or unexported fields
}

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)
	* DebugSample(sample int, msg string, fields ...zapcore.Field)
	* InfoSample(sample int, msg string, fields ...zapcore.Field)
	* WarnSample(sample int, msg string, fields ...zapcore.Field)
	 */
	Logger *LoggerType
)

func NewLogger added in v1.8.0

func NewLogger(level string, opts ...zap.Option) (l *LoggerType, err error)

NewLogger create new logger

func NewLoggerWithName added in v1.9.0

func NewLoggerWithName(name, level string, opts ...zap.Option) (l *LoggerType, err error)

NewLoggerWithName create new logger with name

func SetDefaultLogger added in v1.9.0

func SetDefaultLogger(name, level string, opts ...zap.Option) (l *LoggerType, err error)

SetDefaultLogger set default utils.Logger

func (*LoggerType) ChangeLevel added in v1.8.0

func (l *LoggerType) ChangeLevel(level string) (err error)

ChangeLevel change logger level

func (*LoggerType) DebugSample

func (l *LoggerType) DebugSample(sample int, msg string, fields ...zapcore.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 ...zapcore.Field)

InfoSample emit info log with propability sample/SampleRateDenominator

func (*LoggerType) WarnSample

func (l *LoggerType) WarnSample(sample int, msg string, fields ...zapcore.Field)

WarnSample emit warn log with propability sample/SampleRateDenominator

func (*LoggerType) With added in v1.9.4

func (l *LoggerType) With(fields ...zapcore.Field) *LoggerType

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*LoggerType) WithOptions added in v1.9.4

func (l *LoggerType) WithOptions(opts ...zap.Option) *LoggerType

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

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:

func NewMail

func NewMail(host string, port int) *Mail

NewMail create Mail with SMTP host and port

func (*Mail) BuildMessage

func (m *Mail) BuildMessage(msg string) string

BuildMessage implement

func (*Mail) Login

func (m *Mail) Login(username, password string)

Login login to SMTP server

func (*Mail) Send

func (m *Mail) Send(frAddr, toAddr, frName, toName, subject, content string) (err error)

Send send email

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.ForceRelease()
Output:

func NewMutex added in v1.3.7

func NewMutex() *Mutex

NewMutex create new mutex

func (*Mutex) ForceRelease added in v1.7.5

func (m *Mutex) ForceRelease()

ForceRelease force release lock

func (*Mutex) IsLocked added in v1.3.7

func (m *Mutex) IsLocked() bool

IsLocked return true if is locked

func (*Mutex) SpinLock added in v1.3.7

func (m *Mutex) SpinLock(step, timeout time.Duration)

SpinLock block until succee acquired lock

func (*Mutex) TryLock added in v1.3.7

func (m *Mutex) TryLock() bool

TryLock return true if succeed locked

func (*Mutex) TryRelease added in v1.3.7

func (m *Mutex) TryRelease() bool

TryRelease return true if succeed release

type PairList

type PairList []SortItemItf

PairList array of sort items

func SortBiggest

func SortBiggest(items PairList) PairList

SortBiggest sort from biggest to smallest

func SortSmallest

func SortSmallest(items PairList) PairList

SortSmallest sort from smallest to biggest

func (PairList) Len

func (p PairList) Len() int

Len return length of sort items

func (PairList) Less

func (p PairList) Less(i, j int) bool

Less compare two items

func (PairList) Swap

func (p PairList) Swap(i, j int)

Swap change two items

type ParallelCounter added in v1.6.3

type ParallelCounter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ParallelCounter parallel count with child counter

func NewParallelCounter added in v1.6.3

func NewParallelCounter(quoteStep, rotatePoint int64) (*ParallelCounter, error)

NewParallelCounter get new parallel counter

func NewParallelCounterFromN added in v1.6.3

func NewParallelCounterFromN(n, quoteStep, rotatePoint int64) (*ParallelCounter, error)

NewParallelCounterFromN get new parallel counter

func (*ParallelCounter) GetChild added in v1.6.3

func (c *ParallelCounter) GetChild() *ChildParallelCounter

GetChild create new child

func (*ParallelCounter) GetQuote added in v1.6.3

func (c *ParallelCounter) GetQuote(step int64) (from, to int64)

GetQuote child request new quote from parent

type RequestData

type RequestData struct {
	Headers map[string]string
	Data    interface{}
}

RequestData 发起请求的结构体

type RotateCounter

type RotateCounter struct {
	Mutex
	// 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 NewRotateCounterFromNWithCtx added in v1.7.5

func NewRotateCounterFromNWithCtx(ctx context.Context, n, rotatePoint int64) (*RotateCounter, error)

NewRotateCounterFromNWithCtx create new RotateCounter with threshold from N

func NewRotateCounterWithCtx added in v1.7.5

func NewRotateCounterWithCtx(ctx context.Context, rotatePoint int64) (*RotateCounter, error)

NewRotateCounterWithCtx create new RotateCounter with threshold from 0

func (*RotateCounter) Close added in v1.7.5

func (c *RotateCounter) Close()

Close stop rorate runner

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

func (*RotateCounter) Get added in v1.7.8

func (c *RotateCounter) Get() int64

Get return current counter's number

type SettingsType

type SettingsType struct {
	sync.RWMutex
	YamlExt string
}

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
ctx := context.Background()
throttle, err := utils.NewThrottleWithCtx(ctx, &utils.ThrottleCfg{
	NPerSec: 10,
	Max:     100,
})
if err != nil {
	utils.Logger.Panic("new throttle")
}
defer throttle.Close()

inChan := make(chan int)

for msg := range inChan {
	if !throttle.Allow() {
		continue
	}

	// do something with msg
	fmt.Println(msg)
}
Output:

func NewThrottleWithCtx added in v1.9.0

func NewThrottleWithCtx(ctx context.Context, cfg *ThrottleCfg) (t *Throttle, err error)

NewThrottleWithCtx create new Throttle

func (*Throttle) Allow

func (t *Throttle) Allow() bool

Allow check whether is allowed

func (*Throttle) Close added in v1.9.0

func (t *Throttle) Close()

Close stop throttle

func (*Throttle) Stop

func (t *Throttle) Stop()

Stop stop throttle

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

Directories

Path Synopsis
Package consistenthash contains some implementation of consistent hashing.
Package consistenthash contains some implementation of consistent hashing.

Jump to

Keyboard shortcuts

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