otasyncgo

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2021 License: MIT Imports: 9 Imported by: 0

README

otasyncgo

OpenTracing plugin for spawns new goroutine.

Usage

package main

import (
	"context"
	"fmt"
	"github.com/yuewokeji/opentracing-go-plugins/module/otasyncgo"
	"time"
)

func main() {
	fn := func(ctx context.Context) error {
		fmt.Println("from another goroutine")
		return nil
	}

	// create a goroutine and call the function fn
	otasyncgo.GoWithRecoverOnce(context.Background(), "another-goroutine", fn)

	time.Sleep(time.Second * 3)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoWithRecover

func GoWithRecover(traceCtx context.Context, serviceName string, fn Executor, options ...Option)

创建一个goroutine,执行fn() 如果发生panic,在retry次数内,会sleep一个interval周期后,重新开启新的goroutine

traceCtx:与链路追踪相关的上下文,这个上下文仅用来链接到caller,随后会被销毁

如果你需要使用context的特性,请使用WithContext()
当然,你可以传递traceCtx,即WithContext(traceCtx)

serviceName:服务名,对应链路追踪的span name

func GoWithRecoverOnce

func GoWithRecoverOnce(traceCtx context.Context, serviceName string, fn Executor, options ...Option)

创建一个goroutine,执行fn(),只执行一次

func GoWithRecoverResident

func GoWithRecoverResident(traceCtx context.Context, serviceName string, fn Executor, options ...Option)

创建一个goroutine,执行fn() 如果发生panic,会sleep一个interval周期后,重新开启新的goroutine,没有次数限制 任何一次创建goroutine,链路追踪都会生成一个新的traceID

Types

type Executor

type Executor func(ctx context.Context) error

Executor是goroutine的真正执行者 ctx:一个自定义的上下文,并附加链路追踪的信息 span:你可以通过span记录更多的信息

注意:ctx与下面的traceCtx是不同的

type Option

type Option func(*config)

func WithContext

func WithContext(ctx context.Context) Option

指定一个上下文

func WithInterval

func WithInterval(interval time.Duration) Option

设置时间间隔

func WithRetry

func WithRetry(retry int) Option

设置重试次数

func WithTracer

func WithTracer(tracer opentracing.Tracer) Option

Jump to

Keyboard shortcuts

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