Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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修改来。
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
Click to show internal directories.
Click to hide internal directories.