-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgochan.go
49 lines (42 loc) · 881 Bytes
/
gochan.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
package gochan
// TaskFunc task
type TaskFunc func() error
type gochan struct {
uuid int
tasksChan chan TaskFunc
dieChan chan struct{}
}
// newGochan return gochan with bufferNum tasks
func newGochan(bufferNum int) *gochan {
gc := &gochan{
uuid: defualtUUID(),
tasksChan: make(chan TaskFunc, bufferNum),
dieChan: make(chan struct{}),
}
return gc
}
func (gc *gochan) setUUID(uuid int) {
gc.uuid = uuid
}
// run make gochan running
func (gc *gochan) run() {
go gc.start()
}
// start gochan's goroutine
func (gc *gochan) start() {
defer func() {
logger.Infof("gochan %d ending...", gc.uuid)
}()
logger.Debugf("gochan %d starting...", gc.uuid)
for {
select {
case task := <-gc.tasksChan:
err := task()
if err != nil {
logger.Errorf("task in gochan %d error: %s", gc.uuid, err.Error())
}
case <-gc.dieChan:
return
}
}
}