-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathdefault_checker.go
More file actions
43 lines (36 loc) · 1.05 KB
/
Copy pathdefault_checker.go
File metadata and controls
43 lines (36 loc) · 1.05 KB
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
package tcp
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"
)
var (
defaultChecker *Checker
once sync.Once
)
// DefaultChecker returns a shared singleton instance of the Checker.
//
// It starts the Checker's CheckingLoop in a goroutine with a context that
// listens for system signals (SIGINT, SIGTERM) to stop gracefully. This function
// blocks until the Checker is ready for use.
func DefaultChecker() *Checker {
once.Do(func() {
defaultChecker = NewChecker()
go func() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
// If the checking loop stops with an error, we log it.
// We use the standard log package to avoid external dependencies.
if err := defaultChecker.CheckingLoop(ctx); err != nil {
log.Printf("tcpshaker: TCP checking loop stopped with an error: %v", err)
}
}()
// Wait for the checker to be ready to ensure initialization is complete
// before returning the instance.
<-defaultChecker.WaitReady()
})
return defaultChecker
}