Skip to content

Commit

Permalink
fix golint issues in core/fx (zeromicro#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan authored Feb 19, 2021
1 parent c376ffc commit f238290
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
1 change: 1 addition & 0 deletions core/fs/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"syscall"
)

// CloseOnExec makes sure closing the file on process forking.
func CloseOnExec(file *os.File) {
if file != nil {
syscall.CloseOnExec(int(file.Fd()))
Expand Down
34 changes: 24 additions & 10 deletions core/fx/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,30 @@ type (
workers int
}

FilterFunc func(item interface{}) bool
ForAllFunc func(pipe <-chan interface{})
ForEachFunc func(item interface{})
// FilterFunc defines the method to filter a Stream.
FilterFunc func(item interface{}) bool
// ForAllFunc defines the method to handle all elements in a Stream.
ForAllFunc func(pipe <-chan interface{})
// ForEachFunc defines the method to handle each element in a Stream.
ForEachFunc func(item interface{})
// GenerateFunc defines the method to send elements into a Stream.
GenerateFunc func(source chan<- interface{})
KeyFunc func(item interface{}) interface{}
LessFunc func(a, b interface{}) bool
MapFunc func(item interface{}) interface{}
Option func(opts *rxOptions)
// KeyFunc defines the method to generate keys for the elements in a Stream.
KeyFunc func(item interface{}) interface{}
// LessFunc defines the method to compare the elements in a Stream.
LessFunc func(a, b interface{}) bool
// MapFunc defines the method to map each element to another object in a Stream.
MapFunc func(item interface{}) interface{}
// Option defines the method to customize a Stream.
Option func(opts *rxOptions)
// ParallelFunc defines the method to handle elements parallelly.
ParallelFunc func(item interface{})
ReduceFunc func(pipe <-chan interface{}) (interface{}, error)
WalkFunc func(item interface{}, pipe chan<- interface{})
// ReduceFunc defines the method to reduce all the elements in a Stream.
ReduceFunc func(pipe <-chan interface{}) (interface{}, error)
// WalkFunc defines the method to walk through all the elements in a Stream.
WalkFunc func(item interface{}, pipe chan<- interface{})

// A Stream is a stream that can be used to do stream processing.
Stream struct {
source <-chan interface{}
}
Expand Down Expand Up @@ -159,6 +171,7 @@ func (p Stream) Group(fn KeyFunc) Stream {
return Range(source)
}

// Head returns the first n elements in p.
func (p Stream) Head(n int64) Stream {
if n < 1 {
panic("n must be greater than 0")
Expand Down Expand Up @@ -187,7 +200,7 @@ func (p Stream) Head(n int64) Stream {
return Range(source)
}

// Maps converts each item to another corresponding item, which means it's a 1:1 model.
// Map converts each item to another corresponding item, which means it's a 1:1 model.
func (p Stream) Map(fn MapFunc, opts ...Option) Stream {
return p.Walk(func(item interface{}, pipe chan<- interface{}) {
pipe <- fn(item)
Expand Down Expand Up @@ -274,6 +287,7 @@ func (p Stream) Split(n int) Stream {
return Range(source)
}

// Tail returns the last n elements in p.
func (p Stream) Tail(n int64) Stream {
if n < 1 {
panic("n should be greater than 0")
Expand Down
1 change: 1 addition & 0 deletions core/fx/parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fx

import "github.com/tal-tech/go-zero/core/threading"

// Parallel runs fns parallelly and waits for done.
func Parallel(fns ...func()) {
group := threading.NewRoutineGroup()
for _, fn := range fns {
Expand Down
7 changes: 5 additions & 2 deletions core/fx/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import "github.com/tal-tech/go-zero/core/errorx"
const defaultRetryTimes = 3

type (
// RetryOption defines the method to customize DoWithRetry.
RetryOption func(*retryOptions)

retryOptions struct {
times int
}
)

func DoWithRetries(fn func() error, opts ...RetryOption) error {
// DoWithRetry runs fn, and retries if failed. Default to retry 3 times.
func DoWithRetry(fn func() error, opts ...RetryOption) error {
var options = newRetryOptions()
for _, opt := range opts {
opt(options)
Expand All @@ -30,7 +32,8 @@ func DoWithRetries(fn func() error, opts ...RetryOption) error {
return berr.Err()
}

func WithRetries(times int) RetryOption {
// WithRetry customize a DoWithRetry call with given retry times.
func WithRetry(times int) RetryOption {
return func(options *retryOptions) {
options.times = times
}
Expand Down
10 changes: 5 additions & 5 deletions core/fx/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

func TestRetry(t *testing.T) {
assert.NotNil(t, DoWithRetries(func() error {
assert.NotNil(t, DoWithRetry(func() error {
return errors.New("any")
}))

var times int
assert.Nil(t, DoWithRetries(func() error {
assert.Nil(t, DoWithRetry(func() error {
times++
if times == defaultRetryTimes {
return nil
Expand All @@ -22,7 +22,7 @@ func TestRetry(t *testing.T) {
}))

times = 0
assert.NotNil(t, DoWithRetries(func() error {
assert.NotNil(t, DoWithRetry(func() error {
times++
if times == defaultRetryTimes+1 {
return nil
Expand All @@ -32,11 +32,11 @@ func TestRetry(t *testing.T) {

var total = 2 * defaultRetryTimes
times = 0
assert.Nil(t, DoWithRetries(func() error {
assert.Nil(t, DoWithRetry(func() error {
times++
if times == total {
return nil
}
return errors.New("any")
}, WithRetries(total)))
}, WithRetry(total)))
}
17 changes: 12 additions & 5 deletions core/fx/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ package fx
import (
"context"
"time"

"github.com/tal-tech/go-zero/core/contextx"
)

var (
// ErrCanceled is the error returned when the context is canceled.
ErrCanceled = context.Canceled
ErrTimeout = context.DeadlineExceeded
// ErrTimeout is the error returned when the context's deadline passes.
ErrTimeout = context.DeadlineExceeded
)

type FxOption func() context.Context
// DoOption defines the method to customize a DoWithTimeout call.
type DoOption func() context.Context

func DoWithTimeout(fn func() error, timeout time.Duration, opts ...FxOption) error {
// DoWithTimeout runs fn with timeout control.
func DoWithTimeout(fn func() error, timeout time.Duration, opts ...DoOption) error {
parentCtx := context.Background()
for _, opt := range opts {
parentCtx = opt()
}
ctx, cancel := context.WithTimeout(parentCtx, timeout)
ctx, cancel := contextx.ShrinkDeadline(parentCtx, timeout)
defer cancel()

done := make(chan error)
Expand All @@ -42,7 +48,8 @@ func DoWithTimeout(fn func() error, timeout time.Duration, opts ...FxOption) err
}
}

func WithContext(ctx context.Context) FxOption {
// WithContext customizes a DoWithTimeout call with given ctx.
func WithContext(ctx context.Context) DoOption {
return func() context.Context {
return ctx
}
Expand Down

0 comments on commit f238290

Please sign in to comment.