-
Notifications
You must be signed in to change notification settings - Fork 3
/
agent.go
89 lines (70 loc) · 1.46 KB
/
agent.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
package main
import (
"fmt"
"sync"
"time"
"github.com/opensourceways/community-robot-lib/config"
"github.com/opensourceways/community-robot-lib/utils"
"github.com/sirupsen/logrus"
)
type demuxConfigAgent struct {
agent *config.ConfigAgent
mut sync.RWMutex
demux map[string]eventsDemux
version string
t utils.Timer
}
func (ca *demuxConfigAgent) load() {
v, c := ca.agent.GetConfig()
if ca.version == v {
return
}
nc, ok := c.(*configuration)
if !ok {
logrus.Errorf("can't convert to configuration")
return
}
if nc == nil {
logrus.Error("empty pointer of configuration")
return
}
m := nc.Config.getDemux()
// this function runs in serially, and ca.version is accessed in it,
// so, there is no need to update it under the lock.
ca.version = v
ca.mut.Lock()
ca.demux = m
ca.mut.Unlock()
}
func (ca *demuxConfigAgent) getEndpoints(org, repo, event string) []string {
ca.mut.RLock()
v := getEventsDemux(org, repo, ca.demux)[event]
ca.mut.RUnlock()
return v
}
func getEventsDemux(org, repo string, demux map[string]eventsDemux) eventsDemux {
if demux == nil {
return eventsDemux{}
}
fullname := fmt.Sprintf("%s/%s", org, repo)
if items, ok := demux[fullname]; ok {
return items
}
if items, ok := demux[org]; ok {
return items
}
return eventsDemux{}
}
func (ca *demuxConfigAgent) start() {
ca.load()
ca.t.Start(
func() {
ca.load()
},
1*time.Minute,
0,
)
}
func (ca *demuxConfigAgent) stop() {
ca.t.Stop()
}