forked from scottkiss/grtm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grmanager.go
88 lines (78 loc) · 2.04 KB
/
grmanager.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
package grtm
import (
"fmt"
"strconv"
"strings"
)
type GrManager struct {
grchannelMap *GoroutineChannelMap
}
func NewGrManager() *GrManager {
gm := &GoroutineChannelMap{}
return &GrManager{grchannelMap: gm}
}
func (gm *GrManager) StopLoopGoroutine(name string) error {
stopChannel, ok := gm.grchannelMap.grchannels[name]
if !ok {
return fmt.Errorf("not found goroutine name :" + name)
}
gm.grchannelMap.grchannels[name].msg <- STOP + strconv.Itoa(int(stopChannel.gid))
return nil
}
func (gm *GrManager) UpdateLoopGoroutine(name string, fc interface{}) error {
gm.grchannelMap.grchannels[name].fcmsg <- fc
return nil
}
func (gm *GrManager) NewLoopGoroutine(name string, fc interface{}, args ...interface{}) {
go func(this *GrManager, n string, fc interface{}, args ...interface{}) {
//register channel
err := this.grchannelMap.register(n)
if err != nil {
return
}
for {
select {
case info := <-this.grchannelMap.grchannels[name].msg:
taskInfo := strings.Split(info, ":")
signal, gid := taskInfo[0], taskInfo[1]
if gid == strconv.Itoa(int(this.grchannelMap.grchannels[name].gid)) {
if signal == "__P" {
fmt.Println("gid[" + gid + "] quit")
this.grchannelMap.unregister(name)
return
} else {
fmt.Println("unknown signal")
}
}
case fcmsg := <- this.grchannelMap.grchannels[name].fcmsg:
fc = fcmsg
default:
//fmt.Println("no signal")
}
if len(args) > 1 {
fc.(func(...interface{}))(args)
} else if len(args) == 1 {
fc.(func(interface{}))(args[0])
} else {
fc.(func())()
}
}
}(gm, name, fc, args...)
}
func (gm *GrManager) NewGoroutine(name string, fc interface{}, args ...interface{}) {
go func(n string, fc interface{}, args ...interface{}) {
//register channel
err := gm.grchannelMap.register(n)
if err != nil {
return
}
if len(args) > 1 {
fc.(func(...interface{}))(args)
} else if len(args) == 1 {
fc.(func(interface{}))(args[0])
} else {
fc.(func())()
}
gm.grchannelMap.unregister(name)
}(name, fc, args...)
}