cloud

package
v0.0.0-...-7ef3eb6 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: GPL-3.0 Imports: 25 Imported by: 0

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

View Source
const (
	MeshName = "service.discovery"
	MeshMeta = ""
)

Variables

View Source
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

View Source
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

func Chain(fns ...func() error) error

------------------------------------------------------------------------------------------ Chain runs funs one by one until an error occurred.

func DoWithTimeout

func DoWithTimeout(fn func() error, timeout time.Duration, opts ...DoOption) error

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 Hash

func Hash(data []byte) uint64

Hash returns the hash value of data.

func LoadFile

func LoadFile(file string) []byte

Loading Config

func Md5

func Md5(data []byte) []byte

Md5 returns the md5 bytes of data.

func Md5Hex

func Md5Hex(data []byte) string

Md5Hex returns the md5 hex string of data.

func NewUuid

func NewUuid() string

func Parallel

func Parallel(fns ...func())

func PrintStack

func PrintStack()

PrintStack prints to standard error the stack trace returned by runtime.Stack.

func Recover

func Recover(cleanups ...func())

func RunSafe

func RunSafe(fn func())

RunSafe runs the given fn, recovers if fn panics.

func SetTimeToForceQuit

func SetTimeToForceQuit(duration time.Duration)

SetTimeToForceQuit sets the waiting time before force quitting.

func SetupService

func SetupService()

func Stack

func Stack() []byte

Stack returns a formatted stack trace of the goroutine that calls it. It calls runtime.Stack with a large enough buffer to capture the entire trace.

func StartMetricsAgent

func StartMetricsAgent(addr string)

StartAgent starts a prometheus agent.

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

func DiscoveryService(service string) (*Discovery, error)

------------------- Discovery Service Put / Get Service -------------------

func GetService

func GetService(service string) (*Discovery, error)

func MustGetService

func MustGetService(service string) *Discovery

func MustPutService

func MustPutService(service, addr string, keepalivetimeout int64, register func(*grpc.Server), options ...grpc.ServerOption) *Discovery

func PutService

func PutService(service, addr string, keepalivetimeout int64, register func(*grpc.Server), options ...grpc.ServerOption) (*Discovery, error)

func (*Discovery) Add

func (dt *Discovery) Add(service, addr string) error

func (*Discovery) AddWithLease

func (dt *Discovery) AddWithLease(service, addr string, timeout int64) (<-chan *clientv3.LeaseKeepAliveResponse, error)

func (*Discovery) CloseService

func (dt *Discovery) CloseService()

func (*Discovery) Del

func (dt *Discovery) Del(service, addr string) error

func (*Discovery) RunService

func (dt *Discovery) RunService()

func (*Discovery) String

func (dt *Discovery) String() string

type DoOption

type DoOption func() context.Context

DoOption defines the method to customize a DoWithTimeout call.

func WithContext

func WithContext(ctx context.Context) DoOption

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 NewRoutineGroup

func NewRoutineGroup() *RoutineGroup

NewRoutineGroup returns a RoutineGroup.

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.

func (WorkerGroup) Start

func (wg WorkerGroup) Start()

Start starts a WorkerGroup.

Jump to

Keyboard shortcuts

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