forked from megaease/easeprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
170 lines (149 loc) · 4.57 KB
/
channel.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package channel
import (
"sync"
"sync/atomic"
"github.com/megaease/easeprobe/notify"
"github.com/megaease/easeprobe/probe"
log "github.com/sirupsen/logrus"
)
const kind = "channel"
// Channel implements a config for Channel
type Channel struct {
Name string `yaml:"name"` // unique name
Probers map[string]probe.Prober `yaml:"probers"` // probers
Notifiers map[string]notify.Notify `yaml:"notifiers"` // notifiers
isWatch int32 `yaml:"-"` // is watch
done chan bool `yaml:"-"` // done channel
channel chan probe.Result `yaml:"-"` // notify channel
}
// NewEmpty creates a new empty Channel object with nil channel
// After setup the probers, You have to call Config() to create the channel
func NewEmpty(name string) *Channel {
return &Channel{
Name: name,
Probers: map[string]probe.Prober{},
Notifiers: map[string]notify.Notify{},
isWatch: 0,
done: nil,
channel: nil,
}
}
// Config configures the channel
func (c *Channel) Config() {
c.done = make(chan bool)
c.channel = make(chan probe.Result, len(c.Probers))
}
// Done returns the done channel
func (c *Channel) Done() chan bool {
return c.done
}
// Channel returns the notification channel
func (c *Channel) Channel() chan probe.Result {
return c.channel
}
// Send sends the result to the channel
func (c *Channel) Send(result probe.Result) {
c.channel <- result
}
// GetProber returns the Notify object
func (c *Channel) GetProber(name string) probe.Prober {
return c.Probers[name]
}
// SetProbers sets the Notify objects
func (c *Channel) SetProbers(probers []probe.Prober) {
for _, p := range probers {
c.SetProber(p)
}
}
// SetProber sets the Notify object
func (c *Channel) SetProber(p probe.Prober) {
if p == nil {
return
}
if _, ok := c.Probers[p.Name()]; ok {
log.Errorf("Prober [%s - %s] name is duplicated, ignored!", p.Kind(), p.Name())
return
}
c.Probers[p.Name()] = p
}
// GetNotify returns the Notify object
func (c *Channel) GetNotify(name string) notify.Notify {
return c.Notifiers[name]
}
// SetNotifiers sets the Notify objects
func (c *Channel) SetNotifiers(notifiers []notify.Notify) {
for _, n := range notifiers {
c.SetNotify(n)
}
}
// SetNotify sets the Notify object
func (c *Channel) SetNotify(n notify.Notify) {
if n == nil {
return
}
if _, ok := c.Notifiers[n.Name()]; ok {
log.Errorf("Notifier [%s - %s] name is duplicated, ignored!", n.Kind(), n.Name())
return
}
c.Notifiers[n.Name()] = n
}
// WatchEvent watches the notification event
// Go through all of notification to notify the result.
func (c *Channel) WatchEvent(wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
// check if the channel is watching
if atomic.CompareAndSwapInt32(&(c.isWatch), 0, 1) == false {
log.Warnf("[%s/ %s]: Channel is already watching!", kind, c.Name)
return
}
// set the channel is not watching
defer func() {
atomic.StoreInt32(&(c.isWatch), 0)
}()
// Watching the Probe Event...
for {
select {
case <-c.done:
log.Infof("[%s / %s]: Received the done signal, channel exiting...", kind, c.Name)
return
case result := <-c.channel:
// if the status has no change, no need notify
if result.PreStatus == result.Status {
log.Debugf("[%s / %s]: %s (%s) - Status no change [%s] == [%s], no notification.",
kind, c.Name, result.Name, result.Endpoint, result.PreStatus, result.Status)
continue
}
if result.PreStatus == probe.StatusInit && result.Status == probe.StatusUp {
log.Debugf("[%s / %s]: %s (%s) - Initial Status [%s] == [%s], no notification.",
kind, c.Name, result.Name, result.Endpoint, result.PreStatus, result.Status)
continue
}
log.Infof("[%s / %s]: %s (%s) - Status changed [%s] ==> [%s]",
kind, c.Name, result.Name, result.Endpoint, result.PreStatus, result.Status)
for _, n := range c.Notifiers {
if dryNotify {
n.DryNotify(result)
} else {
go n.Notify(result)
}
}
}
}
}