sentinel

package
v0.0.0-...-29d029c Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DoFunc

type DoFunc func(ctx context.Context, destPtr interface{}, args interface{}) error

DoFunc 哨兵执行的函数。

type MDofunc

type MDofunc func(ctx context.Context, destSlicePtr interface{}, argsSlice interface{}) ([]error, error)

MDofunc 哨兵批量处理时执行的函数。

type Sentinel

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

Sentinel 哨兵。一个生产者,多个消费者等待生产者完成并提交结果。 从https://github.com/wencan/cachex/blob/master/sentinel.go修改来。

func NewSentinel

func NewSentinel() *Sentinel

NewSentinel 新建哨兵

func (*Sentinel) Close

func (s *Sentinel) Close()

Close 关闭。

func (*Sentinel) Done

func (s *Sentinel) Done(result interface{}, err error)

Done 生产者提交结果。 Wait的resultPtr是指向Done的result的指针。

func (*Sentinel) Wait

func (s *Sentinel) Wait(ctx context.Context, resultPtr interface{}) error

Wait 消费者等待生产者提交结果。 Wait的resultPtr是指向Done的result的指针 resultPtr指向的内容是共享的,不可修改。

type SentinelGroup

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

SentinelGroup 哨兵组。

Example (PartialFailure)
var sg SentinelGroup

f := func(ctx context.Context, destSlicePtr interface{}, argsSlice interface{}) ([]error, error) {
	resp := destSlicePtr.(*[]string)
	req := argsSlice.([]string)

	var errs []error
	for _, r := range req {
		if r == "" {
			errs = append(errs, errors.New("skip"))
		} else {
			*resp = append(*resp, "echo: "+r)
			errs = append(errs, nil)
		}
	}
	return errs, nil
}

var keys = []string{"one", "skip", "three"}
var args = []string{"one", "", "three"}
var resp []string
errs, err := sg.MDo(context.TODO(), &resp, keys, args, f)
defer sg.Delete(keys...)

if err != nil {
	fmt.Println(err)
	return
}
for _, res := range resp {
	fmt.Println(res)
}
for _, e := range errs {
	if e != nil {
		fmt.Println("partial failure:", e)
	}
}
Output:

echo: one
echo: three
partial failure: skip

func (*SentinelGroup) Delete

func (sg *SentinelGroup) Delete(keys ...string)

Delete 删除key对应的哨兵。下次需要重新执行该key的逻辑。

func (*SentinelGroup) Do

func (sg *SentinelGroup) Do(ctx context.Context, destPtr interface{}, key string, args interface{}, f DoFunc) error

Do key删除前,不重复执行key相同的逻辑。 destPtr指向的内容是共享的,不可修改。 修改自:https://github.com/wencan/cachex/blob/master/cachex.go

Example
var sg SentinelGroup

f := func(ctx context.Context, destPtr interface{}, args interface{}) error {
	resp := destPtr.(*string)
	req := args.(string)

	*resp = "echo: " + req

	return nil
}

var key string = "hello"
var resp string
var args = "Hello"
err := sg.Do(context.TODO(), &resp, key, args, f)
defer sg.Delete(key)
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(resp)
Output:

echo: Hello

func (*SentinelGroup) MDo

func (sg *SentinelGroup) MDo(ctx context.Context, destSlicePtr interface{}, keys []string, argsSlice interface{}, f MDofunc) ([]error, error)

MDO 处理一批数据。key删除前,不重复执行key相同的逻辑。 destSlicePtr 是获取结果的切片的指针。 argsSlice 是给函数f的参数,顺序和长度等于keys的顺序和长度。 []error表示各个下标位置上的错误。如果没有错误,可以为nil。 函数f返回destSlicePtr顺序同keys/argsSlice顺序,destSlicePtr中缺失项必须在返回[]error的相同下标位置有error。 总是尽可能返回[]error,表示各个位置上的错误;除非无法组成合格的[]error,才会返回error。 destSlicePtr指向的切片内各元素的数据是共享的,不可修改。

Example
var sg SentinelGroup

f := func(ctx context.Context, destSlicePtr interface{}, argsSlice interface{}) ([]error, error) {
	resp := destSlicePtr.(*[]string)
	req := argsSlice.([]string)

	for _, r := range req {
		*resp = append(*resp, "echo: "+r)
	}
	return nil, nil
}

var keys = []string{"one", "two", "three"}
var args = []string{"one", "two", "three"}
var resp []string
errs, err := sg.MDo(context.TODO(), &resp, keys, args, f)
defer sg.Delete(keys...)

if err != nil {
	fmt.Println(err)
	return
}
for _, res := range resp {
	fmt.Println(res)
}
for _, e := range errs {
	if e != nil {
		fmt.Println("partial failure:", e)
	}
}
Output:

echo: one
echo: two
echo: three

Jump to

Keyboard shortcuts

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