-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
135 lines (105 loc) · 2.91 KB
/
hub.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package poh
import (
"context"
"sync"
"github.com/myfantasy/mfctx"
)
type PointsKeysListFunc[K comparable] func(ctx context.Context) (keys []K, err error)
type PointDestroyFunc[K comparable, T any] func(ctx context.Context, key K, point T) (err error)
type PointRefreshFunc[K comparable, T any] func(ctx context.Context, key K, point T) (err error)
type PointGenerateFunc[K comparable, T any] func(ctx context.Context, key K) (point T, err error)
type Hub[K comparable, T any] struct {
points map[K]T
ctxBase context.Context
pointsKeysList PointsKeysListFunc[K]
pointDestroy PointDestroyFunc[K, T]
pointGenerate PointGenerateFunc[K, T]
pointRefresh PointRefreshFunc[K, T]
mx sync.Mutex
}
func MakeHub[K comparable, T any](
ctxBase context.Context,
pointsKeysList PointsKeysListFunc[K],
pointDestroy PointDestroyFunc[K, T],
pointGenerate PointGenerateFunc[K, T],
pointRefresh PointRefreshFunc[K, T],
) *Hub[K, T] {
return &Hub[K, T]{
points: make(map[K]T),
ctxBase: ctxBase,
pointsKeysList: pointsKeysList,
pointDestroy: pointDestroy,
pointGenerate: pointGenerate,
pointRefresh: pointRefresh,
}
}
func (hub *Hub[K, T]) Get(key K) (point T, ok bool) {
hub.mx.Lock()
defer hub.mx.Unlock()
return hub.GetInternal(key)
}
func (hub *Hub[K, T]) GetInternal(key K) (point T, ok bool) {
point, ok = hub.points[key]
return point, ok
}
func (hub *Hub[K, T]) Refresh() (err error) {
hub.mx.Lock()
defer hub.mx.Unlock()
return hub.refreshLoadInternal()
}
func (hub *Hub[K, T]) refreshLoadInternal() (err error) {
ctx := mfctx.FromCtx(hub.ctxBase).Start("Hub.refreshInternal")
defer func() { ctx.Complete(err) }()
keys, err := hub.pointsKeysList(hub.ctxBase)
if err != nil {
return err
}
mkey := make(map[K]bool, len(keys))
for _, key := range keys {
mkey[key] = true
go hub.refreshPoint(ctx.Copy(), key)
}
for key := range hub.points {
if !mkey[key] {
go hub.destroy(ctx.Copy(), key)
}
}
return nil
}
func (hub *Hub[K, T]) refreshPoint(ctx *mfctx.Crumps, key K) (err error) {
hub.mx.Lock()
defer hub.mx.Unlock()
ctx = ctx.With("key", key).Start("hub.refreshPointInternal")
defer func() { ctx.Complete(err) }()
point, ok := hub.points[key]
if !ok {
point, err := hub.pointGenerate(ctx, key)
if err != nil {
return err
}
hub.points[key] = point
return nil
}
err = hub.pointRefresh(ctx, key, point)
if err != nil {
return err
}
return nil
}
func (hub *Hub[K, T]) destroy(ctx *mfctx.Crumps, key K) {
hub.mx.Lock()
point, ok := hub.points[key]
if ok {
delete(hub.points, key)
}
hub.mx.Unlock()
if ok {
go hub.destroyJob(ctx.Copy(), key, point)
}
}
func (hub *Hub[K, T]) destroyJob(ctx *mfctx.Crumps, key K, point T) {
err := hub.pointDestroy(ctx, key, point)
for err != nil {
err = hub.pointDestroy(ctx, key, point)
}
}