-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
90 lines (78 loc) · 1.95 KB
/
client.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
// Package acc provides an accumulator mechanism to accumulate, process, and manage data.
package acc
import (
"context"
"fmt"
"time"
"github.com/9ssi7/nanoid"
)
// accumulator represents the core data structure that manages the accumulation process.
type accumulator[T any] struct {
processor ProcessorFunc[T]
storage DataStorage[Data[T]]
interval time.Duration
startTime time.Time
}
// Add adds a new item to the accumulator and returns its ID.
func (a *accumulator[T]) Add(ctx context.Context, data T) (string, error) {
id, err := nanoid.New()
if err != nil {
return "", err
}
if err := a.storage.Save(ctx, []Data[T]{{Original: data, ID: id}}); err != nil {
return "", err
}
return id, nil
}
// Cancel removes an item from the accumulator based on the provided ID.
func (a *accumulator[T]) Cancel(ctx context.Context, id string) error {
data, err := a.storage.Load(ctx)
if err != nil {
return err
}
var newData []Data[T]
var found bool
for _, d := range data {
if d.ID != id {
newData = append(newData, d)
} else {
found = true
}
}
if !found {
return fmt.Errorf("data not found with given ID %s", id)
}
if err := a.storage.Save(ctx, newData); err != nil {
return err
}
return nil
}
// Start initiates the accumulator to process accumulated data based on the set interval.
// If a start time is set, it waits until that time to begin processing.
func (a *accumulator[T]) Start(ctx context.Context) error {
if !a.startTime.IsZero() {
for {
now := time.Now().UTC()
if now.Hour() == a.startTime.Hour() && now.Minute() == a.startTime.Minute() {
break
}
time.Sleep(time.Minute)
}
}
ticker := time.NewTicker(a.interval)
defer ticker.Stop()
for range ticker.C {
data, err := a.storage.Load(ctx)
if err != nil {
return err
}
if len(data) > 0 {
var originalData []T
for _, d := range data {
originalData = append(originalData, d.Original)
}
a.processor(ctx, originalData)
}
}
return nil
}