-
Notifications
You must be signed in to change notification settings - Fork 4
/
work.go
85 lines (70 loc) · 1.47 KB
/
work.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
package market
import (
"context"
"github.com/zhaocong6/goUtils/goroutinepool"
"log"
"runtime/debug"
)
//manage结构体
//用于管理task任务, 和关闭task运行任务
//使用context通信
var Manage struct {
tasks map[Organize]*Worker
Ctx context.Context
Cancel context.CancelFunc
pool *goroutinepool.Worker
}
func init() {
Manage.Ctx, Manage.Cancel = context.WithCancel(context.Background())
Manage.pool = goroutinepool.NewPool(goroutinepool.Options{
Capacity: 20,
JobBuffer: 500,
})
Manage.tasks = map[Organize]*Worker{}
Manage.tasks[OkEx] = newOkEx(Manage.Ctx)
Manage.tasks[HuoBi] = newHuoBi(Manage.Ctx)
}
//运行work
func Run() {
for _, t := range Manage.tasks {
go func(t *Worker) {
defer func() {
if err := recover(); err != nil {
log.Println(err, string(debug.Stack()))
}
}()
t.RunTask()
}(t)
}
go func() {
defer func() {
if err := recover(); err != nil {
log.Println(err, string(debug.Stack()))
}
}()
subscribeHandle()
}()
}
//关闭task
//使用context 通信
func Close() {
Manage.Cancel()
}
func Find(organize string, symbol ...string) (m map[string]*Marketer) {
switch organize {
case "huobi":
m = Manage.tasks[HuoBi].List.Find(symbol...).ToMap()
case "okex":
m = Manage.tasks[OkEx].List.Find(symbol...).ToMap()
}
return m
}
//订阅请求统一处理
func subscribeHandle() {
for {
select {
case sub := <-readSubscribing:
Manage.tasks[sub.Organize].subscribeHandle(sub)
}
}
}