-
Notifications
You must be signed in to change notification settings - Fork 233
/
random_input.go
65 lines (51 loc) · 1.16 KB
/
random_input.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
package input
import (
"math/rand"
"strconv"
"github.com/childe/gohangout/codec"
"github.com/childe/gohangout/topology"
"github.com/golang/glog"
)
type RandomInput struct {
config map[interface{}]interface{}
decoder codec.Decoder
from int
to int
maxMessages int
count int
}
func init() {
Register("Random", newRandomInput)
}
func newRandomInput(config map[interface{}]interface{}) topology.Input {
var codertype string = "plain"
p := &RandomInput{
config: config,
decoder: codec.NewDecoder(codertype),
count: 0,
maxMessages: -1,
}
if v, ok := config["from"]; ok {
p.from = v.(int)
} else {
glog.Fatal("from must be configured in Random Input")
}
if v, ok := config["to"]; ok {
p.to = v.(int)
} else {
glog.Fatal("to must be configured in Random Input")
}
if v, ok := config["max_messages"]; ok {
p.maxMessages = v.(int)
}
return p
}
func (p *RandomInput) ReadOneEvent() map[string]interface{} {
if p.maxMessages != -1 && p.count >= p.maxMessages {
return nil
}
n := p.from + rand.Intn(1+p.to-p.from)
p.count++
return p.decoder.Decode([]byte(strconv.Itoa(n)))
}
func (p *RandomInput) Shutdown() {}