fix golint issues in core/executors (#484)

master
Kevin Wan 4 years ago committed by GitHub
parent 72580dee38
commit 802549ac7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,8 +5,12 @@ import "time"
const defaultBulkTasks = 1000 const defaultBulkTasks = 1000
type ( type (
// BulkOption defines the method to customize a BulkExecutor.
BulkOption func(options *bulkOptions) BulkOption func(options *bulkOptions)
// A BulkExecutor is an executor that can execute tasks on either requirement meets:
// 1. up to given size of tasks
// 2. flush interval time elapsed
BulkExecutor struct { BulkExecutor struct {
executor *PeriodicalExecutor executor *PeriodicalExecutor
container *bulkContainer container *bulkContainer
@ -18,6 +22,7 @@ type (
} }
) )
// NewBulkExecutor returns a BulkExecutor.
func NewBulkExecutor(execute Execute, opts ...BulkOption) *BulkExecutor { func NewBulkExecutor(execute Execute, opts ...BulkOption) *BulkExecutor {
options := newBulkOptions() options := newBulkOptions()
for _, opt := range opts { for _, opt := range opts {
@ -36,25 +41,30 @@ func NewBulkExecutor(execute Execute, opts ...BulkOption) *BulkExecutor {
return executor return executor
} }
// Add adds task into be.
func (be *BulkExecutor) Add(task interface{}) error { func (be *BulkExecutor) Add(task interface{}) error {
be.executor.Add(task) be.executor.Add(task)
return nil return nil
} }
// Flush forces be to flush and execute tasks.
func (be *BulkExecutor) Flush() { func (be *BulkExecutor) Flush() {
be.executor.Flush() be.executor.Flush()
} }
// Wait waits be to done with the task execution.
func (be *BulkExecutor) Wait() { func (be *BulkExecutor) Wait() {
be.executor.Wait() be.executor.Wait()
} }
// WithBulkTasks customizes a BulkExecutor with given tasks limit.
func WithBulkTasks(tasks int) BulkOption { func WithBulkTasks(tasks int) BulkOption {
return func(options *bulkOptions) { return func(options *bulkOptions) {
options.cachedTasks = tasks options.cachedTasks = tasks
} }
} }
// WithBulkInterval customizes a BulkExecutor with given flush interval.
func WithBulkInterval(duration time.Duration) BulkOption { func WithBulkInterval(duration time.Duration) BulkOption {
return func(options *bulkOptions) { return func(options *bulkOptions) {
options.flushInterval = duration options.flushInterval = duration

@ -5,8 +5,12 @@ import "time"
const defaultChunkSize = 1024 * 1024 // 1M const defaultChunkSize = 1024 * 1024 // 1M
type ( type (
// ChunkOption defines the method to customize a ChunkExecutor.
ChunkOption func(options *chunkOptions) ChunkOption func(options *chunkOptions)
// A ChunkExecutor is an executor to execute tasks when either requirement meets:
// 1. up to given chunk size
// 2. flush interval elapsed
ChunkExecutor struct { ChunkExecutor struct {
executor *PeriodicalExecutor executor *PeriodicalExecutor
container *chunkContainer container *chunkContainer
@ -18,6 +22,7 @@ type (
} }
) )
// NewChunkExecutor returns a ChunkExecutor.
func NewChunkExecutor(execute Execute, opts ...ChunkOption) *ChunkExecutor { func NewChunkExecutor(execute Execute, opts ...ChunkOption) *ChunkExecutor {
options := newChunkOptions() options := newChunkOptions()
for _, opt := range opts { for _, opt := range opts {
@ -36,6 +41,7 @@ func NewChunkExecutor(execute Execute, opts ...ChunkOption) *ChunkExecutor {
return executor return executor
} }
// Add adds task with given chunk size into ce.
func (ce *ChunkExecutor) Add(task interface{}, size int) error { func (ce *ChunkExecutor) Add(task interface{}, size int) error {
ce.executor.Add(chunk{ ce.executor.Add(chunk{
val: task, val: task,
@ -44,20 +50,24 @@ func (ce *ChunkExecutor) Add(task interface{}, size int) error {
return nil return nil
} }
// Flush forces ce to flush and execute tasks.
func (ce *ChunkExecutor) Flush() { func (ce *ChunkExecutor) Flush() {
ce.executor.Flush() ce.executor.Flush()
} }
// Wait waits the execution to be done.
func (ce *ChunkExecutor) Wait() { func (ce *ChunkExecutor) Wait() {
ce.executor.Wait() ce.executor.Wait()
} }
// WithChunkBytes customizes a ChunkExecutor with the given chunk size.
func WithChunkBytes(size int) ChunkOption { func WithChunkBytes(size int) ChunkOption {
return func(options *chunkOptions) { return func(options *chunkOptions) {
options.chunkSize = size options.chunkSize = size
} }
} }
// WithFlushInterval customizes a ChunkExecutor with the given flush interval.
func WithFlushInterval(duration time.Duration) ChunkOption { func WithFlushInterval(duration time.Duration) ChunkOption {
return func(options *chunkOptions) { return func(options *chunkOptions) {
options.flushInterval = duration options.flushInterval = duration

@ -7,6 +7,7 @@ import (
"github.com/tal-tech/go-zero/core/threading" "github.com/tal-tech/go-zero/core/threading"
) )
// A DelayExecutor delays a tasks on given delay interval.
type DelayExecutor struct { type DelayExecutor struct {
fn func() fn func()
delay time.Duration delay time.Duration
@ -14,6 +15,7 @@ type DelayExecutor struct {
lock sync.Mutex lock sync.Mutex
} }
// NewDelayExecutor returns a DelayExecutor with given fn and delay.
func NewDelayExecutor(fn func(), delay time.Duration) *DelayExecutor { func NewDelayExecutor(fn func(), delay time.Duration) *DelayExecutor {
return &DelayExecutor{ return &DelayExecutor{
fn: fn, fn: fn,
@ -21,6 +23,7 @@ func NewDelayExecutor(fn func(), delay time.Duration) *DelayExecutor {
} }
} }
// Trigger triggers the task to be executed after given delay, safe to trigger more than once.
func (de *DelayExecutor) Trigger() { func (de *DelayExecutor) Trigger() {
de.lock.Lock() de.lock.Lock()
defer de.lock.Unlock() defer de.lock.Unlock()

@ -7,11 +7,13 @@ import (
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
) )
// A LessExecutor is an executor to limit execution once within given time interval.
type LessExecutor struct { type LessExecutor struct {
threshold time.Duration threshold time.Duration
lastTime *syncx.AtomicDuration lastTime *syncx.AtomicDuration
} }
// NewLessExecutor returns a LessExecutor with given threshold as time interval.
func NewLessExecutor(threshold time.Duration) *LessExecutor { func NewLessExecutor(threshold time.Duration) *LessExecutor {
return &LessExecutor{ return &LessExecutor{
threshold: threshold, threshold: threshold,
@ -19,6 +21,8 @@ func NewLessExecutor(threshold time.Duration) *LessExecutor {
} }
} }
// DoOrDiscard executes or discards the task depends on if
// another task was executed within the time interval.
func (le *LessExecutor) DoOrDiscard(execute func()) bool { func (le *LessExecutor) DoOrDiscard(execute func()) bool {
now := timex.Now() now := timex.Now()
lastTime := le.lastTime.Load() lastTime := le.lastTime.Load()

@ -28,6 +28,7 @@ type (
RemoveAll() interface{} RemoveAll() interface{}
} }
// A PeriodicalExecutor is an executor that periodically execute tasks.
PeriodicalExecutor struct { PeriodicalExecutor struct {
commander chan interface{} commander chan interface{}
interval time.Duration interval time.Duration
@ -43,6 +44,7 @@ type (
} }
) )
// NewPeriodicalExecutor returns a PeriodicalExecutor with given interval and container.
func NewPeriodicalExecutor(interval time.Duration, container TaskContainer) *PeriodicalExecutor { func NewPeriodicalExecutor(interval time.Duration, container TaskContainer) *PeriodicalExecutor {
executor := &PeriodicalExecutor{ executor := &PeriodicalExecutor{
// buffer 1 to let the caller go quickly // buffer 1 to let the caller go quickly
@ -61,6 +63,7 @@ func NewPeriodicalExecutor(interval time.Duration, container TaskContainer) *Per
return executor return executor
} }
// Add adds tasks into pe.
func (pe *PeriodicalExecutor) Add(task interface{}) { func (pe *PeriodicalExecutor) Add(task interface{}) {
if vals, ok := pe.addAndCheck(task); ok { if vals, ok := pe.addAndCheck(task); ok {
pe.commander <- vals pe.commander <- vals
@ -68,6 +71,7 @@ func (pe *PeriodicalExecutor) Add(task interface{}) {
} }
} }
// Flush forces pe to execute tasks.
func (pe *PeriodicalExecutor) Flush() bool { func (pe *PeriodicalExecutor) Flush() bool {
pe.enterExecution() pe.enterExecution()
return pe.executeTasks(func() interface{} { return pe.executeTasks(func() interface{} {
@ -77,12 +81,14 @@ func (pe *PeriodicalExecutor) Flush() bool {
}()) }())
} }
// Sync lets caller to run fn thread-safe with pe, especially for the underlying container.
func (pe *PeriodicalExecutor) Sync(fn func()) { func (pe *PeriodicalExecutor) Sync(fn func()) {
pe.lock.Lock() pe.lock.Lock()
defer pe.lock.Unlock() defer pe.lock.Unlock()
fn() fn()
} }
// Wait waits the execution to be done.
func (pe *PeriodicalExecutor) Wait() { func (pe *PeriodicalExecutor) Wait() {
pe.Flush() pe.Flush()
pe.wgBarrier.Guard(func() { pe.wgBarrier.Guard(func() {

@ -4,4 +4,5 @@ import "time"
const defaultFlushInterval = time.Second const defaultFlushInterval = time.Second
// Execute defines the method to execute tasks.
type Execute func(tasks []interface{}) type Execute func(tasks []interface{})

@ -72,6 +72,24 @@ func NewRedis(redisAddr, redisType string, redisPass ...string) *Redis {
} }
} }
// BitCount is redis bitcount command implementation.
func (s *Redis) BitCount(key string, start, end int64) (val int64, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
val, err = conn.BitCount(key, &red.BitCount{
Start: start,
End: end,
}).Result()
return err
}, acceptable)
return
}
// Blpop uses passed in redis connection to execute blocking queries. // Blpop uses passed in redis connection to execute blocking queries.
// Doesn't benefit from pooling redis connections of blocking queries // Doesn't benefit from pooling redis connections of blocking queries
func (s *Redis) Blpop(redisNode RedisNode, key string) (string, error) { func (s *Redis) Blpop(redisNode RedisNode, key string) (string, error) {
@ -108,23 +126,6 @@ func (s *Redis) BlpopEx(redisNode RedisNode, key string) (string, bool, error) {
return vals[1], true, nil return vals[1], true, nil
} }
func (s *Redis) BitCount(key string, start, end int64) (val int64, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}
val, err = conn.BitCount(key, &red.BitCount{
Start: start,
End: end,
}).Result()
return err
}, acceptable)
return
}
func (s *Redis) Del(keys ...string) (val int, err error) { func (s *Redis) Del(keys ...string) (val int, err error) {
err = s.brk.DoWithAcceptable(func() error { err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s) conn, err := getRedis(s)

Loading…
Cancel
Save