-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspout.go
82 lines (69 loc) · 1.64 KB
/
spout.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
package gostream
import (
"log"
"sync"
"time"
)
type ISpout interface {
NewInstance() ISpout
Open(index int, context TopologyContext, collector IOutputCollector)
Close()
NextTuple()
}
type BaseSpout struct {
Index int
Context TopologyContext
Collector IOutputCollector
}
func NewBaseSpout() *BaseSpout {
t := &BaseSpout{}
return t
}
func (t *BaseSpout) Copy() *BaseSpout {
log.Println("BaseSpout NewInstance")
t1 := &BaseSpout{}
return t1
}
func (t *BaseSpout) Open(index int, context TopologyContext, collector IOutputCollector) {
log.Printf("BaseSpout Open,%d", index)
t.Index = index
t.Context = context
t.Collector = collector
}
func (t *BaseSpout) Close() {
log.Printf("BaseSpout Close,%d", t.Index)
}
func (t *TopologyBuilder) goSpout(wg *sync.WaitGroup, stop chan bool, id string, index int) {
defer wg.Done()
log.Printf("goSpout,%s,%d start\n", id, index)
cc := t.commons[id]
ispout := t.spouts[id].ispout.NewInstance()
task := cc.tasks[index]
ispout.Open(index, task, task)
lastTs := time.Now().Unix()
counter := int64(0) //计数器
loop:
for {
select {
case <-stop:
log.Printf("goSpout id:%s,%d receive stop signal", id, index)
break loop
default:
now := time.Now().Unix()
if t.statInterval > 0 && now > lastTs+t.statInterval {
log.Printf("goSpout id:%s,%d speed %d/s", id, index, counter/t.statInterval)
lastTs = now
counter = 0
}
ispout.NextTuple()
counter++
}
}
ispout.Close()
cc.closeDownstream()
if t.statInterval > 0 {
log.Printf("goSpout,%s,%d stopped, speed %d/s", id, index, counter/t.statInterval)
} else {
log.Printf("goSpout,%s,%d stopped", id, index)
}
}