util

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package util provides some utility tools for other components. Such as net-transporting, file-operating, rate-limiter.

Index

Constants

View Source
const DefaultLogTimeFormat = "2006-01-02 15:04:05.000"

DefaultLogTimeFormat defines the timestamp format.

Variables

View Source
var (
	// Printer is global StdPrinter.
	Printer = &StdPrinter{Out: os.Stdout}
)

Functions

func AddConsoleLog

func AddConsoleLog(logger *log.Logger)

AddConsoleLog will add a ConsoleLog into logger's hooks. It will output logs to console when logger's outputting logs.

func CreateLogger

func CreateLogger(logPath string, logName string, logLevel string, sign string) *log.Logger

CreateLogger creates a logger.

func IsEmptyStr

func IsEmptyStr(s string) bool

IsEmptyStr returns whether the string x is empty.

func IsNil

func IsNil(value interface{}) (result bool)

IsNil returns whether the value is nil.

func Max

func Max(x, y int32) int32

Max returns the larger of x or y.

func Min

func Min(x, y int32) int32

Min returns the smaller of x or y.

func PanicIfError

func PanicIfError(err error, msg string)

PanicIfError panic if an error happened.

func PanicIfNil

func PanicIfNil(obj interface{}, msg string)

PanicIfNil panic if the obj is nil.

Types

type ConsoleHook

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

ConsoleHook shows logs on console.

func (*ConsoleHook) Fire

func (ch *ConsoleHook) Fire(entry *log.Entry) error

Fire implements Hook#Fire.

func (*ConsoleHook) Levels

func (ch *ConsoleHook) Levels() []log.Level

Levels implements Hook#Levels().

type DragonflyFormatter

type DragonflyFormatter struct {
	// TimestampFormat sets the format used for marshaling timestamps.
	TimestampFormat string
	Sign            string
}

DragonflyFormatter customizes the dragonfly log format.

func (*DragonflyFormatter) Format

func (f *DragonflyFormatter) Format(entry *log.Entry) ([]byte, error)

Format implements Formatter#Format.

type RateLimiter

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

RateLimiter is used for limiting the rate of transporting.

func NewRateLimiter

func NewRateLimiter(rate int32, window int64) *RateLimiter

NewRateLimiter creates a RateLimiter instance. rate: how many tokens are generated per second. 0 represents that don't limit the rate. window: generating tokens interval (millisecond, [1,1000]). The production of rate and window should be division by 1000.

func (*RateLimiter) AcquireBlocking

func (rl *RateLimiter) AcquireBlocking(token int32) int32

AcquireBlocking acquires tokens. It will be blocking unit the bucket has enough required number of tokens.

func (*RateLimiter) AcquireNonBlocking

func (rl *RateLimiter) AcquireNonBlocking(token int32) int32

AcquireNonBlocking acquires tokens. It will return -1 immediately when there is no enough number of tokens.

func (*RateLimiter) SetRate

func (rl *RateLimiter) SetRate(rate int32)

SetRate sets rate of RateLimiter.

type StdPrinter

type StdPrinter struct {
	Out io.Writer
}

StdPrinter output info to console directly.

func (*StdPrinter) Printf

func (sp *StdPrinter) Printf(format string, a ...interface{})

Printf formats according to a format specifier.

func (*StdPrinter) Println

func (sp *StdPrinter) Println(msg string)

Println output info to console directly.

Jump to

Keyboard shortcuts

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