fix golint issues in core/fx (#486)

This commit is contained in:
Kevin Wan
2021-02-19 17:49:39 +08:00
committed by GitHub
parent c376ffc351
commit f238290dd3
6 changed files with 48 additions and 22 deletions

View File

@@ -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)
@@ -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
}