/
channel.go
executable file
·130 lines (110 loc) · 3.12 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
/*
Copyright [2018] [jc3wish]
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://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 server
import (
"sync"
"github.com/brokercap/Bifrost/Bristol/mysql"
"github.com/brokercap/Bifrost/server/count"
"github.com/brokercap/Bifrost/config"
"log"
)
type Channel struct {
sync.Mutex
Name string
chanName chan mysql.EventReslut
MaxThreadNum int // 消费通道的最大线程数
CurrentThreadNum int
Status string //stop ,starting,running,wait
db *db
countChan chan *count.FlowCount
}
func NewChannel(MaxThreadNum int,Name string, db *db) *Channel {
return &Channel{
Name: Name,
chanName: make(chan mysql.EventReslut, MaxThreadNum*config.ChannelQueueSize),
MaxThreadNum: MaxThreadNum,
CurrentThreadNum: 0,
Status: "stop",
db: db,
}
}
func GetChannel(name string,channelID int) *Channel{
if _,ok:=DbList[name];!ok{
return nil
}
DbList[name].Lock()
defer DbList[name].Unlock()
if _,ok:=DbList[name].channelMap[channelID];!ok{
return nil
}
return DbList[name].channelMap[channelID]
}
func DelChannel(name string,channelID int) bool{
if _,ok:=DbList[name];!ok{
return false
}
if _,ok:=DbList[name].channelMap[channelID];!ok{
return false
}
log.Println(DbList[name].Name,"Channel:",DbList[name].channelMap[channelID].Name,"delete")
delete(DbList[name].channelMap,channelID)
return true
}
func (Channel *Channel) SetFlowCountChan(flowChan chan *count.FlowCount) {
Channel.countChan = flowChan
}
func (Channel *Channel) GetCountChan() chan *count.FlowCount {
return Channel.countChan
}
func (Channel *Channel) Start() chan mysql.EventReslut {
log.Println(Channel.db.Name,"Channel:",Channel.Name,"start")
if Channel.Status == "running"{
return Channel.chanName
}
Channel.Status = "running"
for i := 0; i < Channel.MaxThreadNum; i++ {
go Channel.channelConsume()
}
return Channel.chanName
}
func (Channel *Channel) GetChannel() chan mysql.EventReslut {
return Channel.chanName
}
func (Channel *Channel) Stop() {
log.Println(Channel.db.Name,"Channel:",Channel.Name,"stop")
Channel.Status = "stop"
}
func (Channel *Channel) Close() {
log.Println(Channel.db.Name,"Channel:",Channel.Name,"close")
Channel.Status = "close"
}
func (This *Channel) SetChannelMaxThreadNum(n int) {
This.MaxThreadNum = n
}
func (This *Channel) GetChannelMaxThreadNum() int {
return This.MaxThreadNum
}
func (c *Channel) channelConsume() {
c.Lock()
c.CurrentThreadNum++
c.Unlock()
defer func() {
if err := recover(); err != nil {
log.Println("channelConsume err:",err)
c.Lock()
c.CurrentThreadNum--
c.Unlock()
}
}()
NewConsumeChannel(c).consume_channel()
}