README
¶
// Metrics callprocessed = promauto.NewCounter(prometheus.CounterOpts{ Namespace: "Happy", Name: "call_processed_total", Help: "counter for function call maked ", }) calltime = promauto.NewGauge(prometheus.GaugeOpts{ Namespace: "Happy", Name: "call_time", Help: "call time used", }) // Usage Example func exampleServer() { const ( service = "hello" addr = "127.0.0.1:9999" ) mesh.CheckSupportArch() mesh.SignalSetup() mesh.AddShutdownListener(func() { log.Println("hello hook") }) dis, err := mesh.PutService(service, addr, 10) pb.RegisterCoolServer(dis.Serv, &servinfo{}) dis.RunService() } func exampleClient() { const ( service = "hello" addr = "127.0.0.1:9999" ) dis, err := mesh.GetService(config.Service) service := pb.NewCoolClient(dis.Conn) resp, err := service.Ping(context.Background(), &pb.Request{}) } // Usage Funcs /* CallChain(func fn() error {},func fn2() error {}) */ /* group := NewRoutineGroup() for i := 0; i < 3; i++ { group.Run(func() { atomic.AddInt32(&count, 1) }) } group.Wait() */ /* m := make(map[string]lang.PlaceholderType) var lock sync.Mutex var wg sync.WaitGroup wg.Add(runtime.NumCPU()) group := NewWorkerGroup(func() { lock.Lock() m[fmt.Sprint(RoutineId())] = lang.Placeholder lock.Unlock() wg.Done() }, runtime.NumCPU()) go group.Start() wg.Wait() */ /* ch := make(chan lang.PlaceholderType) go RunSafe(func() { defer func() { ch <- lang.Placeholder }() panic("panic") }) <-ch */ /* Parallel(func() { time.Sleep(time.Millisecond * 100) atomic.AddInt32(&count, 1) }, func() { time.Sleep(time.Millisecond * 100) atomic.AddInt32(&count, 2) }, func() { time.Sleep(time.Millisecond * 100) atomic.AddInt32(&count, 3) }) */ /* ctx, cancel := context.WithCancel(context.Background()) go func() { time.Sleep(time.Millisecond * 10) cancel() }() err := DoWithTimeout(func() error { time.Sleep(time.Minute) return nil }, time.Second, WithContext(ctx)) */
Documentation
¶
Index ¶
- Constants
- Variables
- func AddShutdownListener(fn func()) (waitForCalled func())
- func AddWrapUpListener(fn func()) (waitForCalled func())
- func Chain(fns ...func() error) error
- func DoWithTimeout(fn func() error, timeout time.Duration, opts ...DoOption) error
- func GoSafe(fn func())
- func Hash(data []byte) uint64
- func LoadFile(file string) []byte
- func Md5(data []byte) []byte
- func Md5Hex(data []byte) string
- func NewUuid() string
- func Parallel(fns ...func())
- func PrintStack()
- func Recover(cleanups ...func())
- func RunSafe(fn func())
- func SetTimeToForceQuit(duration time.Duration)
- func SetupService()
- func Stack() []byte
- func StartMetricsAgent(addr string)
- type Discovery
- func DiscoveryService(service string) (*Discovery, error)
- func GetService(service string) (*Discovery, error)
- func MustGetService(service string) *Discovery
- func MustPutService(service, addr string, keepalivetimeout int64, register func(*grpc.Server), ...) *Discovery
- func PutService(service, addr string, keepalivetimeout int64, register func(*grpc.Server), ...) (*Discovery, error)
- func (dt *Discovery) Add(service, addr string) error
- func (dt *Discovery) AddWithLease(service, addr string, timeout int64) (<-chan *clientv3.LeaseKeepAliveResponse, error)
- func (dt *Discovery) CloseService()
- func (dt *Discovery) Del(service, addr string) error
- func (dt *Discovery) RunService()
- func (dt *Discovery) String() string
- type DoOption
- type RoutineGroup
- type WorkerGroup
Constants ¶
const ( MeshName = "service.discovery" MeshMeta = "" )
Variables ¶
var ( // ErrCanceled is the error returned when the context is canceled. ErrCanceled = context.Canceled // ErrTimeout is the error returned when the context's deadline passes. ErrTimeout = context.DeadlineExceeded )
------------------------------------------------------------------------------------------ DoWithTimeout
var Logger *logx.Logger
Functions ¶
func AddShutdownListener ¶
func AddShutdownListener(fn func()) (waitForCalled func())
----------------------------------- Add Graceful Shutdown handlers AddShutdownListener adds fn as a shutdown listener. The returned func can be used to wait for fn getting called.
func AddWrapUpListener ¶
func AddWrapUpListener(fn func()) (waitForCalled func())
AddWrapUpListener adds fn as a wrap up listener. The returned func can be used to wait for fn getting called.
func Chain ¶
------------------------------------------------------------------------------------------ Chain runs funs one by one until an error occurred.
func DoWithTimeout ¶
DoWithTimeout runs fn with timeout control.
func GoSafe ¶
func GoSafe(fn func())
GoSafe runs the given fn using another goroutine, recovers if fn panics.
func PrintStack ¶
func PrintStack()
PrintStack prints to standard error the stack trace returned by runtime.Stack.
func SetTimeToForceQuit ¶
SetTimeToForceQuit sets the waiting time before force quitting.
func SetupService ¶
func SetupService()
Types ¶
type Discovery ¶
type Discovery struct { //etcd client Cli *clientv3.Client // grpc server/client Serv *grpc.Server Conn *grpc.ClientConn //endpoints EndMgr endpoints.Manager SrvKey string Addr string // contains filtered or unexported fields }
func DiscoveryService ¶
------------------- Discovery Service Put / Get Service -------------------
func GetService ¶
func MustGetService ¶
func MustPutService ¶
func PutService ¶
func (*Discovery) AddWithLease ¶
func (*Discovery) CloseService ¶
func (dt *Discovery) CloseService()
func (*Discovery) RunService ¶
func (dt *Discovery) RunService()
type DoOption ¶
DoOption defines the method to customize a DoWithTimeout call.
func WithContext ¶
WithContext customizes a DoWithTimeout call with given ctx.
type RoutineGroup ¶
type RoutineGroup struct {
// contains filtered or unexported fields
}
------------------------------------------------------------------------------------------ RoutineGroup A RoutineGroup is used to group goroutines together and all wait all goroutines to be done.
func (*RoutineGroup) Run ¶
func (g *RoutineGroup) Run(fn func())
Run runs the given fn in RoutineGroup. Don't reference the variables from outside, because outside variables can be changed by other goroutines
func (*RoutineGroup) RunSafe ¶
func (g *RoutineGroup) RunSafe(fn func())
RunSafe runs the given fn in RoutineGroup, and avoid panics. Don't reference the variables from outside, because outside variables can be changed by other goroutines
func (*RoutineGroup) Wait ¶
func (g *RoutineGroup) Wait()
Wait waits all running functions to be done.
type WorkerGroup ¶
type WorkerGroup struct {
// contains filtered or unexported fields
}
------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------ WorkerGroup A WorkerGroup is used to run given number of workers to process jobs.
func NewWorkerGroup ¶
func NewWorkerGroup(job func(), workers int) WorkerGroup
NewWorkerGroup returns a WorkerGroup with given job and workers.