utils

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2019 License: MIT Imports: 30 Imported by: 22

README

Go-Utils

Many useful golang tools

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 small tools including:

  • Clock: high performance lazy load clock
  • Settings: configuration manager that support yml and spring-cloud-config-server
  • Counter: counter and rotate counter
  • Mail: simply email sender
  • JWT: simply JWT encrypt/decrypt functions
  • RequestJSON: simply http client that send json request and unmarshal response by json
  • Logger: high performance structrued logger based by zap
  • Math: some simply math functions
    • Round: get round of float
  • Throttle: throttling to limit throughput
  • time: some useful time functions
    • UTCNow()
    • ParseTs2String
    • ParseTs2Time
  • utils: some tool functions
    • GetFuncName
    • FallBack
    • RegexNamedSubMatch
    • FlattenMap

see more examples in tests or document

Documentation

Overview

Package utils 一些常用工具

Index

Examples

Constants

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

HTTP defines

View Source
const CFG_FNAME = "settings.yml"
View Source
const SampleRateDenominator = 1000

sample rate = sample / SampleRateDenominator

Variables

View Source
var Clock = NewClock(defaultClockInterval)

Clock high performance time utils

View Source
var Settings = &SettingsType{
	SettingsConst: &SettingsConst{
		YAML_TYPE: "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 EnableCpuPprof

func EnableCpuPprof()

func EnableMemPprof

func EnableMemPprof()

func EnableTracePprof

func EnableTracePprof()

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)

func FloorDivision

func FloorDivision(val int, divisor int) int

FloorDivision 205//100 = 2

func GeneratePasswordHash added in v1.2.0

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

GeneratePasswordHash generate hashed password by origin password

func GetFuncName

func GetFuncName(f interface{}) string

GetFuncName return the name of func

func HTTPInvalidStatusError

func HTTPInvalidStatusError(statusCode int) error

HTTPInvalidStatusError return error about status code

func ParseTs2String

func ParseTs2String(ts int64, layout string) string

ParseTs2String can parse unix timestamp(int64) to string

func ParseTs2Time

func ParseTs2Time(ts int64) time.Time

ParseTs2Time can parse unix timestamp(int64) to time.Time

func RandomStringWithLength

func RandomStringWithLength(n int) string

func RegexNamedSubMatch

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

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)

func SetupClock

func SetupClock(refreshInterval time.Duration)

func SetupLogger

func SetupLogger(level string)

SetupLogger contstruct logger

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 ClockType

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

func NewClock

func NewClock(refreshInterval time.Duration) *ClockType

func (*ClockType) GetTimeInRFC3339Nano

func (c *ClockType) GetTimeInRFC3339Nano() string

func (*ClockType) GetUTCNow

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

func (*ClockType) Run

func (c *ClockType) Run()

func (*ClockType) SetupInterval

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

func (*ClockType) Stop

func (c *ClockType) Stop()

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"`
}

type ConfigServerCfg

type ConfigServerCfg struct {
	URL     string // config-server api
	Profile string // env
	Label   string // branch
	App     string // app name
}

type ConfigSource

type ConfigSource struct {
	Name   string                 `json:"name"`
	Source map[string]interface{} `json:"source"`
}

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

func (*ConfigSrv) Get

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

func (*ConfigSrv) GetBool

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

func (*ConfigSrv) GetInt

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

func (*ConfigSrv) GetString

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

func (*ConfigSrv) Map

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

type Counter

type Counter struct {
	*sync.Mutex
	// contains filtered or unexported fields
}
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

func NewCounterFromN

func NewCounterFromN(n int64) *Counter

func (*Counter) Count

func (c *Counter) Count() int64

func (*Counter) CountN

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

func (*Counter) Get

func (c *Counter) Get() int64

func (*Counter) GetSpeed

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

func (*Counter) Set

func (c *Counter) Set(n int64)

type JWT

type JWT struct {
	ExpiresAtKey string
	UserIDKey    string
	// contains filtered or unexported fields
}

JWT struct to generate and validate jwt tokens

func (*JWT) Generate deprecated

func (j *JWT) Generate(expiresAt int64, payload map[string]interface{}) (string, error)

Deprecated: Generate generate JWT token. old interface

func (*JWT) GenerateToken added in v1.2.0

func (j *JWT) GenerateToken(userId string, expiresAt time.Time, payload map[string]interface{}) (tokenStr string, err error)

GenerateToken generate JWT token. do not use `expires_at` & `uid` as keys.

func (*JWT) Setup

func (j *JWT) Setup(secret string)

Setup initialize JWT

func (*JWT) Validate

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

Validate validate the token and return the payload

type LoggerType

type LoggerType struct {
	*zap.Logger
}

LoggerType extend from zap.Logger

var (
	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)

type Mail

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

func NewMail

func NewMail(host string, port int) *Mail

func (*Mail) BuildMessage

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

func (*Mail) Login

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

func (*Mail) Send

func (m *Mail) Send(fr, to, frName, toName, subject, content string) (err error)

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)

func NewMonotonicRotateCounter

func NewMonotonicRotateCounter(rotatePoint int64) (*MonotonicRotateCounter, error)

func (*MonotonicRotateCounter) Count

func (c *MonotonicRotateCounter) Count() (n int64)

func (*MonotonicRotateCounter) CountN

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

type PairList

type PairList []SortItemItf

func SortBiggest

func SortBiggest(items PairList) PairList

func SortSmallest

func SortSmallest(items PairList) PairList

func (PairList) Len

func (p PairList) Len() int

func (PairList) Less

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

func (PairList) Swap

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

type RequestData

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

RequestData 发起请求的结构体

type RotateCounter

type RotateCounter struct {
	// contains filtered or unexported fields
}
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)

func NewRotateCounterFromN

func NewRotateCounterFromN(n, rotatePoint int64) (*RotateCounter, error)

func (*RotateCounter) Count

func (c *RotateCounter) Count() int64

func (*RotateCounter) CountN

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

type SettingsConst

type SettingsConst struct {
	YAML_TYPE string
}

type SettingsType

type SettingsType struct {
	*SettingsConst
	sync.Mutex
}

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) GetStringSlice

func (s *SettingsType) GetStringSlice(key string) []string

GetStringSlice get setting by key

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{}
}

type Throttle

type Throttle struct {
	*ThrottleCfg
	// contains filtered or unexported fields
}

func NewThrottle

func NewThrottle(cfg *ThrottleCfg) *Throttle

func (*Throttle) Allow

func (t *Throttle) Allow() bool

func (*Throttle) Run

func (t *Throttle) Run()

func (*Throttle) Stop

func (t *Throttle) Stop()

type ThrottleCfg

type ThrottleCfg struct {
	Max, NPerSec int
}

type Uint32Counter

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

func NewUint32Counter

func NewUint32Counter() *Uint32Counter

func NewUint32CounterFromN

func NewUint32CounterFromN(n uint32) *Uint32Counter

func (*Uint32Counter) Count

func (c *Uint32Counter) Count() uint32

func (*Uint32Counter) CountN

func (c *Uint32Counter) CountN(n uint32) uint32

func (*Uint32Counter) Get

func (c *Uint32Counter) Get() uint32

func (*Uint32Counter) Set

func (c *Uint32Counter) Set(n uint32)

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