-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (42 loc) · 853 Bytes
/
main.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
package main
import (
"context"
"fmt"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"github.com/alextanhongpin/core/sync/expire"
)
func main() {
worker := expire.New(expire.Option{
Threshold: 10,
Expiry: 10 * time.Second,
Interval: 5 * time.Second,
})
ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGTERM, os.Interrupt)
defer cancel()
stop := worker.HandleFunc(ctx, func(ctx context.Context) error {
fmt.Println("executing ...", time.Now())
return nil
})
defer stop()
go func() {
for {
select {
case <-ctx.Done():
return
default:
sleep := time.Duration(rand.Intn(1_000)) * time.Millisecond
time.Sleep(sleep)
fmt.Println("added 1s")
worker.Add(ctx, rand.Intn(5))
}
}
}()
fmt.Println("running...")
<-ctx.Done()
fmt.Println("terminating...")
}