utils

package module
v0.0.0-...-36e0765 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2023 License: Apache-2.0 Imports: 24 Imported by: 4

README

utils

Go utilities

Documentation

Index

Constants

View Source
const (
	LOG_DEBUG int = iota
	LOG_INFO
	LOG_WARN
	LOG_ERROR
)
View Source
const (
	MODE_PERM_RW fs.FileMode = 0600
	MODE_PERM_RO fs.FileMode = 0400
)
View Source
const (
	STATUS_UP      string = "up"
	STATUS_DOWN    string = "down"
	STATUS_PENDING string = "pending"
)
View Source
const (
	ScriptStatusWaiting = "waiting"
	ScriptStatusRunning = "running"
	ScriptStatusExited  = "exited"
	ScriptStatusKilled  = "killed"
	ScriptStatusFailed  = "failed_start"
)
View Source
const (
	DefaultThreadQueueSize int = 64
	DefaultThreadPoolSize  int = 8
)

Variables

View Source
var (
	Logger   *log.Logger = log.Default()
	LogLevel int         = LOG_DEBUG
	LogLabel []string    = []string{"DEBUG", "INFO", "WARN", "ERROR"}
)

Functions

func AllocIPFromCidr

func AllocIPFromCidr(cidr string, exist []string) (*string, error)

allocate ip from cidr

func AllocIPFromRange

func AllocIPFromRange(start string, end string, exist []string) (*string, error)

allocate ip from range

func CreateFile

func CreateFile(target string, data ...string) error

func DelFileIfExist

func DelFileIfExist(f string) error

func Equal

func Equal(a interface{}, b interface{}) bool

func ExecCommand

func ExecCommand(cmd string) (int, error)

func FileExist

func FileExist(f string) bool

func GetIPStatus

func GetIPStatus(ipaddress string, prefix *uint8, dev *string) string

func GetInterfaceOfIP

func GetInterfaceOfIP(ipaddress string) (*string, *int, error)

func GetInterfaceStatus

func GetInterfaceStatus(dev *string) string

func GetProcStatus

func GetProcStatus(pidFile string) string

func IpInNetwork

func IpInNetwork(ipstr string, cidr string) bool

cidr contai

func Ipv4ToUint32

func Ipv4ToUint32(ipv4 string) (uint32, error)

convert ipv4 to unit32

func ListContains

func ListContains(slice []string, obj string) bool

func ListDelete

func ListDelete(slice []string, obj string) (result []string)

func LogPrintf

func LogPrintf(level int, module string, format string, v ...interface{})

func MergeFiles

func MergeFiles(target string, files ...string) error

func MkdirIfNotExist

func MkdirIfNotExist(path string) error

func RandNum

func RandNum(max int) int

func RandomString

func RandomString(len int) string

func Uint32ToIpv4

func Uint32ToIpv4(ipv4Uint32 uint32) string

convert unit32 to ipv4

func WriteFile

func WriteFile(target string, flag int, mode os.FileMode, data ...string) error

Types

type Bitmap

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

func FromBytes

func FromBytes(buffer []byte, cap uint32) *Bitmap

FromBytes reads a bitmap from a byte buffer without copying the buffer.

func FromString

func FromString(hexString string, cap uint32) (*Bitmap, error)

FromString decodes the received string and loads it to bitmap object

func NewBitmap

func NewBitmap(size uint32) *Bitmap

func (*Bitmap) And

func (a *Bitmap) And(other Bitmap, extra ...Bitmap)

And computes the intersection between two bitmaps and stores the result in the current bitmap

func (*Bitmap) AndNot

func (a *Bitmap) AndNot(other Bitmap, extra ...Bitmap)

AndNot computes the difference between two bitmaps and stores the result in the current bitmap. Operation works as set subtract: a - b

func (*Bitmap) Clear

func (b *Bitmap) Clear()

Clear clears the bitmap and resizes it to zero.

func (*Bitmap) Contains

func (b *Bitmap) Contains(x uint32) bool

Contains checks whether a value is contained in the bitmap or not.

func (*Bitmap) Count

func (b *Bitmap) Count() int

Count returns the number of elements in this bitmap

func (*Bitmap) CountTo

func (b *Bitmap) CountTo(until uint32) int

CountTo counts the number of elements in the bitmap up until the specified index. If until is math.MaxUint32, it will return the count. The count is non-inclusive of the index.

func (*Bitmap) Grow

func (b *Bitmap) Grow(desiredBit uint32)

Grow grows the bitmap size until we reach the desired bit.

func (*Bitmap) MarshalJSON

func (b *Bitmap) MarshalJSON() ([]byte, error)

func (*Bitmap) Max

func (b *Bitmap) Max() (uint32, bool)

Max get the largest value stored in this bitmap, assuming the bitmap is not empty.

func (*Bitmap) MaxZero

func (b *Bitmap) MaxZero() (uint32, bool)

MaxZero get the last zero bit and return its index, assuming bitmap is not empty

func (*Bitmap) Min

func (b *Bitmap) Min() (uint32, bool)

Min get the smallest value stored in this bitmap, assuming the bitmap is not empty.

func (*Bitmap) MinZero

func (b *Bitmap) MinZero() (uint32, bool)

MinZero finds the first zero bit and returns its index, assuming the bitmap is not empty.

func (*Bitmap) Ones

func (b *Bitmap) Ones()

Ones sets the entire bitmap to one.

func (*Bitmap) Or

func (a *Bitmap) Or(other Bitmap, extra ...Bitmap)

Or computes the union between two bitmaps and stores the result in the current bitmap

func (*Bitmap) Remove

func (b *Bitmap) Remove(x uint32)

Remove removes the bit x from the bitmap, but does not shrink it.

func (*Bitmap) Set

func (b *Bitmap) Set(x uint32)

Set sets the bit x in the bitmap and grows it if necessary.

func (*Bitmap) ToBytes

func (b *Bitmap) ToBytes() (out []byte)

ToBytes converts the bitmap to binary representation without copying the underlying data. The output buffer should not be modified, since it would also change the bitmap.

func (*Bitmap) ToHexBitmap

func (b *Bitmap) ToHexBitmap() *HexBitmap

func (*Bitmap) ToString

func (b *Bitmap) ToString() string

ToString returns encoded string representation for the bitmap

func (*Bitmap) UnmarshalJSON

func (b *Bitmap) UnmarshalJSON(data []byte) (err error)

func (*Bitmap) Xor

func (a *Bitmap) Xor(other Bitmap, extra ...Bitmap)

Xor computes the symmetric difference between two bitmaps and stores the result in the current bitmap

type Gossip

type Gossip struct {
	Name                    string
	BindAddr                string
	BindPort                int
	SecretKey               []byte
	ProbeInterval           int
	SyncInterval            int
	RetransmitMult          int
	Ready                   bool
	NotifyJoinHandler       func(*memberlist.Node)
	NotifyLeaveHandler      func(*memberlist.Node)
	NotifyUpdateHandler     func(*memberlist.Node)
	InvalidatesHandler      func(memberlist.Broadcast) bool
	NodeMetaHandler         func() []byte
	NotifyMsgHandler        func([]byte)
	LocalStateHandler       func() []byte
	MergeRemoteStateHandler func([]byte)
	// contains filtered or unexported fields
}

func DefaultGossip

func DefaultGossip() *Gossip

Generage a default broadcast, need to set handlers:

  • NotifyMsgHandler
  • LocalStateHandler
  • MergeRemoteStateHandler

func GossipWith

func GossipWith(name string, bindAddr string, bindPort int) *Gossip

func (*Gossip) Broadcast

func (g *Gossip) Broadcast(msg []byte)

Broadcast message

func (*Gossip) Start

func (g *Gossip) Start(members *string) error

Start gossip

type HexBitmap

type HexBitmap struct {
	Size uint32 `json:"size,omitempty"`
	Bits string `json:"bits,omitempty"`
}

func (*HexBitmap) Grow

func (h *HexBitmap) Grow(x uint32) error

func (*HexBitmap) Remove

func (h *HexBitmap) Remove(x uint32) error

func (*HexBitmap) Set

func (h *HexBitmap) Set(x uint32) error

func (*HexBitmap) ToBitmap

func (h *HexBitmap) ToBitmap() (*Bitmap, error)

type Runnable

type Runnable interface {
	Run()
}

type Script

type Script struct {
	Spec   *ScriptSpec
	Cmd    *exec.Cmd
	Status string

	Runnable
	// contains filtered or unexported fields
}

func NewScript

func NewScript(s *ScriptSpec, callback func()) (*Script, error)

func (*Script) Expect

func (s *Script) Expect(regex string, timeout time.Duration) bool

func (*Script) Input

func (s *Script) Input(line string) error

func (*Script) Run

func (s *Script) Run()

func (*Script) Wait

func (s *Script) Wait()

type ScriptSpec

type ScriptSpec struct {
	Path        string
	Dir         string
	Args        []string
	Env         []string
	SysProcAttr *syscall.SysProcAttr // &syscall.SysProcAttr{Setpgid: true}
	Out         string
}

type ThreadPool

type ThreadPool struct {
	QueueSize int
	PoolSize  int
	Queue     chan Runnable
	// contains filtered or unexported fields
}

func DefaultThreadPool

func DefaultThreadPool() *ThreadPool

func NewThreadPool

func NewThreadPool(QueueSize int, PoolSize int) *ThreadPool

func (*ThreadPool) Destroy

func (p *ThreadPool) Destroy()

func (*ThreadPool) Init

func (p *ThreadPool) Init()

func (*ThreadPool) Put

func (p *ThreadPool) Put(r Runnable) error

func (*ThreadPool) Wait

func (p *ThreadPool) Wait()

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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