File size: 1,934 Bytes
7107f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tache

import (
	"log/slog"
	"time"
)

// Options is the options for manager
type Options struct {
	Works                int
	MaxRetry             int
	Timeout              *time.Duration
	PersistPath          string
	PersistDebounce      *time.Duration
	Running              bool
	Logger               *slog.Logger
	PersistReadFunction  func() ([]byte, error)
	PersistWriteFunction func([]byte) error
}

// DefaultOptions returns default options
func DefaultOptions() *Options {
	persistDebounce := 3 * time.Second
	return &Options{
		Works: 5,
		//MaxRetry: 1,
		PersistDebounce: &persistDebounce,
		Running:         true,
		Logger:          slog.Default(),
	}
}

// Option is the option for manager
type Option func(*Options)

// WithOptions set options
func WithOptions(opts Options) Option {
	return func(o *Options) {
		*o = opts
	}
}

// WithWorks set works
func WithWorks(works int) Option {
	return func(o *Options) {
		o.Works = works
	}
}

// WithMaxRetry set retry
func WithMaxRetry(maxRetry int) Option {
	return func(o *Options) {
		o.MaxRetry = maxRetry
	}
}

// WithTimeout set timeout
func WithTimeout(timeout time.Duration) Option {
	return func(o *Options) {
		o.Timeout = &timeout
	}
}

// WithPersistPath set persist path
func WithPersistPath(path string) Option {
	return func(o *Options) {
		o.PersistPath = path
	}
}

func WithPersistFunction(r func() ([]byte, error), w func([]byte) error) Option {
	return func(o *Options) {
		o.PersistReadFunction = r
		o.PersistWriteFunction = w
	}
}

// WithPersistDebounce set persist debounce
func WithPersistDebounce(debounce time.Duration) Option {
	return func(o *Options) {
		o.PersistDebounce = &debounce
	}
}

// WithRunning set running
func WithRunning(running bool) Option {
	return func(o *Options) {
		o.Running = running
	}
}

// WithLogger set logger
func WithLogger(logger *slog.Logger) Option {
	return func(o *Options) {
		o.Logger = logger
	}
}