-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwrapper.go
113 lines (102 loc) · 2.9 KB
/
wrapper.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package appctl
import (
"context"
"fmt"
"io"
"log"
"os"
"sync/atomic"
"time"
)
type (
ServiceWrapper struct {
service Service
options ServiceOptions
initialized bool
firstTimeError time.Time
errorRepeats int64
}
ServiceOptions struct {
// RestoringThreshold sets the time for which the service should return to its operational state.
RestoringThreshold time.Duration
// MaxErrorRepeats limits the number of consecutive Ping errors that occur. If it is exceeded, it throws an error to the calling side.
MaxErrorRepeats int64
// PostInitialization allows the Init call to be deferred to a later time, applicable if the application can be started without the service.
PostInitialization bool
// InitializationThreshold limits the time for deferred initialization.
InitializationThreshold time.Duration
// Logger is used to output logs.
Logger io.Writer
}
)
// WrapService allows you to wrap a service by applying deferred initialization or a threshold of failed ping attempts.
func WrapService(service Service, options ServiceOptions) *ServiceWrapper {
return &ServiceWrapper{
service: service,
options: normalizeOptions(options),
}
}
func normalizeOptions(options ServiceOptions) ServiceOptions {
if options.Logger == nil {
options.Logger = log.New(os.Stderr, "ServiceWrapper:", log.LstdFlags).Writer()
}
return options
}
func (s *ServiceWrapper) Init(ctx context.Context) error {
err := s.service.Init(ctx)
if err == nil {
s.initialized = true
s.firstTimeError = time.Time{}
return nil
}
s.log("service initialization: %s", err)
if !s.options.PostInitialization {
return err
}
if s.firstTimeError.IsZero() {
s.firstTimeError = time.Now()
}
if time.Since(s.firstTimeError) < s.options.InitializationThreshold {
s.log("error skipped")
return nil
}
return err
}
func (s *ServiceWrapper) Ping(ctx context.Context) error {
if !s.initialized {
return s.Init(ctx)
}
err := s.service.Ping(ctx)
if err == nil {
s.firstTimeError = time.Time{}
atomic.StoreInt64(&s.errorRepeats, 0)
return nil
}
errorRepeats := atomic.AddInt64(&s.errorRepeats, 1)
if s.firstTimeError.IsZero() {
s.firstTimeError = time.Now()
}
s.log("service ping: %s", err)
timeDelayed := s.options.RestoringThreshold > 0 && time.Since(s.firstTimeError) <= s.options.RestoringThreshold
countDelayed := s.options.MaxErrorRepeats > 0 && errorRepeats <= s.options.MaxErrorRepeats
if timeDelayed || countDelayed {
s.log("error skipped")
return nil
}
return err
}
func (s *ServiceWrapper) log(format string, a ...any) {
if s.options.Logger == nil {
return
}
_, _ = s.options.Logger.Write([]byte(fmt.Sprintf("[%s] %s\n", s.Ident(), fmt.Sprintf(format, a...))))
}
func (s *ServiceWrapper) Close() error {
return s.service.Close()
}
func (s *ServiceWrapper) Ident() string {
return s.service.Ident()
}
func (s *ServiceWrapper) Health() bool {
return atomic.LoadInt64(&s.errorRepeats) == 0
}