aboutsummaryrefslogtreecommitdiff
path: root/rutina.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-18 17:58:00 +0300
committerAlexander Kiryukhin <a.kiryukhin@mail.ru>2021-03-18 17:58:00 +0300
commit14fcf184ae40729e270821115e2ba39c475badb9 (patch)
tree81f6d915e6ed6de5ead922b9bdb1ab1be41a5c01 /rutina.go
parentcb67839e3c2acb257d8b67bd28ca3dcea258b66a (diff)
Diffstat (limited to 'rutina.go')
-rwxr-xr-xrutina.go36
1 files changed, 14 insertions, 22 deletions
diff --git a/rutina.go b/rutina.go
index cd5748e..3cf140c 100755
--- a/rutina.go
+++ b/rutina.go
@@ -22,7 +22,7 @@ type logger func(format string, v ...interface{})
var nopLogger = func(format string, v ...interface{}) {}
-//Rutina is routine manager
+// Rutina is routine manager
type Rutina struct {
ctx context.Context // State of application (started/stopped)
Cancel func() // Cancel func that stops all routines
@@ -39,19 +39,13 @@ type Rutina struct {
}
// New instance with builtin context
-func New(opts *Options) *Rutina {
+func New(opts ...Options) *Rutina {
if opts == nil {
- opts = Opt
+ opts = []Options{}
}
- ctx, cancel := context.WithCancel(opts.ParentContext)
+ options := composeOptions(opts)
+ ctx, cancel := context.WithCancel(options.ParentContext)
var counter uint64
- if opts.Logger == nil {
- opts.Logger = nopLogger
- }
- var signals []os.Signal
- if opts.ListenOsSignals {
- signals = []os.Signal{os.Kill, os.Interrupt}
- }
return &Rutina{
ctx: ctx,
Cancel: cancel,
@@ -59,20 +53,18 @@ func New(opts *Options) *Rutina {
onceErr: sync.Once{},
onceWait: sync.Once{},
err: nil,
- logger: opts.Logger,
+ logger: options.Logger,
counter: &counter,
- errCh: opts.Errors,
- autoListenSignals: signals,
+ errCh: options.Errors,
+ autoListenSignals: options.ListenOsSignals,
processes: map[uint64]*process{},
mu: sync.Mutex{},
}
}
// Go routine
-func (r *Rutina) Go(doer func(ctx context.Context) error, opts *RunOptions) uint64 {
- if opts == nil {
- opts = RunOpt
- }
+func (r *Rutina) Go(doer func(ctx context.Context) error, opts ...RunOptions) uint64 {
+ options := composeRunOptions(opts)
// Check that context is not canceled yet
if r.ctx.Err() != nil {
return 0
@@ -83,11 +75,11 @@ func (r *Rutina) Go(doer func(ctx context.Context) error, opts *RunOptions) uint
process := process{
id: id,
doer: doer,
- onDone: opts.OnDone,
- onError: opts.OnError,
- restartLimit: opts.MaxCount,
+ onDone: options.OnDone,
+ onError: options.OnError,
+ restartLimit: options.MaxCount,
restartCount: 0,
- timeout: opts.Timeout,
+ timeout: options.Timeout,
}
r.processes[id] = &process
r.mu.Unlock()