utils

package
v1.0.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHTTPListen indicates that the given address is invalid
	ErrInvalidHTTPListen = errors.New("invalid TCP4 address")
	// ErrHTTPListenMissingPort indicates that the given address lack port
	ErrHTTPListenMissingPort = errors.New("port number must be speified")
)

Functions

func BuildStats

func BuildStats(scope *stats.Scope, stats interface{}) error

BuildStats builds the stats with given scope. It will initialize the Scope, Counter, Gauge, Histogram fields.

Exmaples of struct fields and their initialized value:

  // CxTotal value will be *scope.Counter("cx_total")
	 CxTotal *stats.Counter

  // CxActive value will be *scope.Gauge("cx_active")
  CxActive *stats.Gauge

  // CxDuration value will be *scope.Histogram("cx_duration")
  CxDuration *stats.Histogram

func CheckPort

func CheckPort(port int) bool

CheckPort return true if port not bind by other process.

func CheckPortWithReusePort

func CheckPortWithReusePort(port int) bool

CheckPortWithReusePort return true if port not bind by other process.

func Copy

func Copy(src map[string]string) map[string]string

Copy returns a copy of given registry.

func DurationPtr

func DurationPtr(d time.Duration) *time.Duration

DurationPtr return *time.Duration

func FlattenKey

func FlattenKey(key string) string

FlattenKey flatten the key for formatting, removes spaces and point.

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP returns a non-loopback local IP

func GetPortByRange

func GetPortByRange(start, end int, timeout time.Duration) (int, error)

GetPortByRange return a random port in [start, end) if can not find a unused in timeout will return a error.

func IntMax

func IntMax(a, b int) int

IntMax returns the greater int

func IntMin

func IntMin(a, b int) int

IntMin returns the smaller int I can't believe that golang doesn't provide this. Yes, there is math.Min() but giving two int, you have to

math.Min(float64(a), float64(b))

Converting TWO int to FLOAT64 EVERY TIME just to get the smaller one, that's crazy! There should be something like:

a < b ? a : b

or:

a if a < b else b

But there isn't! So I wrote this stupid func along with the above, I must be crazy.

Hi, let's just blame that golang doesn't have generics.

func IsAddrInUse

func IsAddrInUse(err error) bool

IsAddrInUse returns whether the given error is known to report that the address is in use. e.g. syscall.EADDRINUSE

func LocateHAProxy

func LocateHAProxy() (string, error)

LocateHAProxy returns HAProxy location

func NewUUID

func NewUUID() (string, error)

NewUUID generates a random UUID according to RFC 4122

func PickFirstNonEmptyStr

func PickFirstNonEmptyStr(str ...string) string

PickFirstNonEmptyStr return the first no empty string.

func ReadPidFile

func ReadPidFile(pidFile string) (int, error)

ReadPidFile reads content from given file path and return as a int

func Round

func Round(f float64, digits int) (float64, error)

Round round up float to a specific precision. I'm getting a little bit used to this The implementation is dumb but safe hopefully

func RunCmd

func RunCmd(cmd string) (string, error)

RunCmd run cmd and returns the execution result

func VerifyTCP4Address

func VerifyTCP4Address(address string) error

VerifyTCP4Address returns error if the given address is not a valid TCP4 address. e.g. 1.1.1.1: -> NG

1.1.1.1:1000  -> OK

func WithTempDir

func WithTempDir(f func(d string))

WithTempDir creates a temp dir then run the given callback(tempDir) finally remove the temp dir and any children it contains

func WithTempFile

func WithTempFile(content string, f func(filename string))

WithTempFile creates a tempfile with given content then calls f()

The file will be deleted after calling f()

func WriteFileAtomically

func WriteFileAtomically(filename string, data []byte, perm os.FileMode) error

WriteFileAtomically is the same as ioutil.WriteFile but it writes atomically by using os.Rename which is atomic as required by POSIX.

Known issue: If the tempfile is write on another drive the rename will fail with EXDEV, in this case atomic write is not possible.

Types

type TestServer

type TestServer struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

TestServer is a TCP server for testing

func StartTestServer

func StartTestServer(t testing.TB, addr string, handlers ...TestServerHandler) *TestServer

StartTestServer launches a TCP server at given address

func (*TestServer) Close

func (ts *TestServer) Close() error

Close stops listening and wait for all connections to complete

func (*TestServer) Port

func (ts *TestServer) Port() int

Port returns the listening port number.

func (*TestServer) Start

func (ts *TestServer) Start() error

Start starts listening on given address

type TestServerHandler

type TestServerHandler func(*net.TCPConn)

TestServerHandler handles incomming connections.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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