-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshared_binding.go
More file actions
73 lines (59 loc) · 1.52 KB
/
shared_binding.go
File metadata and controls
73 lines (59 loc) · 1.52 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
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
package diecast
import (
"fmt"
"time"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/typeutil"
)
// 100ms precision seems generous for this use case, but you *can* change it.
var SharedBindingPollInterval time.Duration = 100 * time.Millisecond
type SharedBindingSet []*Binding
func (set SharedBindingSet) init(server *Server) error {
go func(s *Server) {
if SharedBindingPollInterval > 0 {
for at := range time.NewTicker(SharedBindingPollInterval).C {
if at.IsZero() {
break
}
for i, binding := range set {
if ri := typeutil.Duration(binding.Interval); ri > 0 {
if binding.lastRefreshedAt.IsZero() || time.Since(binding.lastRefreshedAt) >= ri {
if binding.Name == `` {
binding.Name = fmt.Sprintf("shared.%d", i)
}
go set.refreshAndStore(server, binding)
}
}
}
}
}
}(server)
return nil
}
func (set SharedBindingSet) refreshAndStore(server *Server, binding *Binding) {
if binding.syncing {
return
} else {
binding.syncing = true
}
defer func() {
binding.syncing = false
}()
binding.server = server
if data, err := binding.asyncEval(); err == nil {
if binding.Name != `` {
server.sharedBindingData.Store(binding.Name, data)
binding.lastRefreshedAt = time.Now()
}
} else {
log.Warningf("async binding %s: %v", binding.Name, err)
}
}
func (set SharedBindingSet) perRequestBindings() (bindings []Binding) {
for _, b := range set {
if b.Interval == `` {
bindings = append(bindings, *b)
}
}
return
}