util

package
v0.0.0-...-04cfb28 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 25 Imported by: 11

README

一、异常处理

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/util"
	"sync"
	"time"
)

func test() {
	defer util.HandlePanic()
	panic("test panic")
}

func main() {
	test()

	util.Safe(func() {
		panic("closure panic")
	})

	wg := sync.WaitGroup{}
	wg.Add(1)
	util.Go(func() {
		defer wg.Done()
		panic("goroutine closure panic")
	})
	wg.Wait()
	time.Sleep(time.Second / 10)
	fmt.Println("over")
}

二、任务池

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/util"
	"math/rand"
	"sync"
	"time"
)

func main() {
	workers := util.NewWorkers("test", 10, 5)
	wg := &sync.WaitGroup{}
	for i := 0; i < 20; i++ {
		idx := i
		wg.Add(1)
		workers.Go(func() {
			defer wg.Done()
			time.Sleep(time.Second / time.Duration(10*(1+rand.Intn(10))))
			fmt.Println(idx)
		}, 0)
	}
	wg.Wait()

	fmt.Println("-----")
	for i := 0; i < 20; i++ {
		idx := i
		go func() {
			wg.Add(1)
			defer wg.Done()
			err := workers.GoWait(func() {
				time.Sleep(time.Second / 2)
			})
			fmt.Println(idx, err)
		}()
	}
	wg.Wait()

	workers.Stop()
	fmt.Println("over")
}

三、有序任务池

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/util"
	"sync"
	"time"
)

func main() {
	wg := sync.WaitGroup{}
	cl := util.NewWorkersLink("test", 10, 20)
	for i := 1; i <= 50; i++ {
		idx := i
		wg.Add(1)
		cl.Go(func(task *util.LinkTask) {
			defer wg.Done()

			task.WaitPre()

			fmt.Println("---", idx)
			if idx%20 == 0 {
				fmt.Println("+++ sleep", idx)
				time.Sleep(time.Second)
			}

			task.Done(nil)
		})

	}
	wg.Wait()
	cl.Stop()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWorkerPoolTimeout = errors.New("timeout")
	ErrWorkerPoolStopped = errors.New("worker pool has stopped")
)
View Source
var (
	MAX_ERROR_STACK   = 128
	ERROR_LINE_PREFIX = "\n\t"
)

Functions

func B32ToNum

func B32ToNum(s string) int64

func B32ToString

func B32ToString(n int64) string

func BytesToStr

func BytesToStr(b []byte) string

func CheckSingleProc

func CheckSingleProc()

func ClearTx

func ClearTx(tx *sql.Tx)

clear transaction

func CopyFile

func CopyFile(dstName, srcName string) (written int64, err error)

func Float2String

func Float2String(v interface{}) map[string]interface{}

func GBKToUTF8

func GBKToUTF8(src string) string

func GZipCompress

func GZipCompress(data []byte) []byte

func GZipUnCompress

func GZipUnCompress(data []byte) ([]byte, error)

func GetProcName

func GetProcName() string

func GetStacks

func GetStacks(args ...interface{}) string

func Go

func Go(cb func())

func HandlePanic

func HandlePanic()

func HandleSignal

func HandleSignal(handler func(sig os.Signal))

func Hash

func Hash(str string) uint64

func IsChineseName

func IsChineseName(str string) bool

func IsEmail

func IsEmail(str string) bool

func IsMobilePhone

func IsMobilePhone(str string) bool

func IsOnlyChinese

func IsOnlyChinese(str string) bool

func JsonAToB

func JsonAToB(a, b interface{}) error

json

func MsgpackAToB

func MsgpackAToB(a, b interface{}) error

msgpack

func OnPanic

func OnPanic(h func(string))

func PathExist

func PathExist(path string) bool

func ProcExist

func ProcExist(name string) bool

func RandBytes

func RandBytes(n int) []byte

func RandCodeString

func RandCodeString(n int) string

func RandString

func RandString(n int) string

func Safe

func Safe(cb func())

func SetAnalyzerDebug

func SetAnalyzerDebug(d bool)

func SetErrorLinePrefix

func SetErrorLinePrefix(prefix string)

func SetMaxErrorStack

func SetMaxErrorStack(max int)

func ShuffleBytes

func ShuffleBytes(b []byte) []byte

func ShuffleString

func ShuffleString(s string) string

func StrToBytes

func StrToBytes(s string) []byte

func TestTimeID

func TestTimeID()

func TimeID

func TimeID(n int) (string, error)

func TrimComment

func TrimComment(data []byte) []byte

func TrimJson

func TrimJson(data []byte, args ...interface{}) ([]byte, error)

func TrimJsonComma

func TrimJsonComma(data []byte, args ...interface{}) ([]byte, error)

func UTF8ToGBK

func UTF8ToGBK(src string) string

func ZlibCompress

func ZlibCompress(data []byte) []byte

func ZlibUnCompress

func ZlibUnCompress(data []byte) ([]byte, error)

Types

type Analyzer

type Analyzer struct {
	Tag          string
	Parent       *Analyzer `json:"-"`
	Children     []*Analyzer
	Limit        time.Duration `json:"-"`
	SLimit       string        `json:"Limit"`
	TBegin       time.Time
	TEnd         time.Time
	TUsed        string
	StackBegin   string
	StackEnd     string
	Data         interface{}
	Expired      bool
	ChildExpired bool
}

func NewAnalyzer

func NewAnalyzer(tag string, limit time.Duration) *Analyzer

func (*Analyzer) Begin

func (a *Analyzer) Begin()

func (*Analyzer) Done

func (a *Analyzer) Done(v ...interface{})

func (*Analyzer) Fork

func (a *Analyzer) Fork(tag string, limit time.Duration) *Analyzer

func (*Analyzer) Info

func (a *Analyzer) Info() string

func (*Analyzer) Report

func (a *Analyzer) Report(v ...interface{})

type ConfigLoader

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

func NewConfigLoader

func NewConfigLoader(redisCli *redis.Client, pubsubKey string, updateInterval time.Duration) *ConfigLoader

func (*ConfigLoader) Add

func (loader *ConfigLoader) Add(configKey string, configFieled string, onUpdate func() error)

func (*ConfigLoader) AddAuto

func (loader *ConfigLoader) AddAuto(configKey string, configFieled string, onUpdate func(string, string) error)

type Empty

type Empty struct{}

type Error

type Error struct {
	Deep  int
	Text  string
	Stack string
	Child *Error
}

func ErrorWrap

func ErrorWrap(text string, v interface{}) *Error

func Errorf

func Errorf(format string, v ...interface{}) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Root

func (e *Error) Root() *Error

func (*Error) ToError

func (e *Error) ToError() error

type LinkTask

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

func (*LinkTask) Done

func (task *LinkTask) Done(data interface{})

func (*LinkTask) WaitPre

func (task *LinkTask) WaitPre() interface{}

type Qps

type Qps struct {
	Tag   string
	Born  time.Time
	Begin time.Time
	Count int64
	Total int64
	// contains filtered or unexported fields
}

func NewQps

func NewQps(tag string) *Qps

func (*Qps) Add

func (q *Qps) Add(n int64)

func (*Qps) Run

func (q *Qps) Run(args ...interface{}) *Qps

func (*Qps) Stop

func (q *Qps) Stop()

type TimeCOunt

type TimeCOunt struct {
	Tag string
	B   time.Time
	E   time.Time
	// contains filtered or unexported fields
}

func NewTimeCount

func NewTimeCount(tag string) *TimeCOunt

func (*TimeCOunt) Output

func (tc *TimeCOunt) Output()

func (*TimeCOunt) String

func (tc *TimeCOunt) String() string

func (*TimeCOunt) TimeUsed

func (tc *TimeCOunt) TimeUsed(tag string) time.Duration

func (*TimeCOunt) Warn

func (tc *TimeCOunt) Warn(maxTimeUsed time.Duration)

type WorkerPool

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

func NewWorkerPool

func NewWorkerPool(tag string, qCap int, corNum int) *WorkerPool

func (*WorkerPool) Go

func (c *WorkerPool) Go(h func(), to time.Duration) error

func (*WorkerPool) Stop

func (c *WorkerPool) Stop()
type WorkerPoolLink struct {
	sync.RWMutex
	sync.WaitGroup
	// contains filtered or unexported fields
}
func NewWorkerPoolLink(tag string, qCap int, corNum int) *WorkerPoolLink

func (*WorkerPoolLink) Go

func (c *WorkerPoolLink) Go(h func(task *LinkTask)) error

func (*WorkerPoolLink) Stop

func (c *WorkerPoolLink) Stop()

Jump to

Keyboard shortcuts

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