-
Notifications
You must be signed in to change notification settings - Fork 5
/
input.go
65 lines (55 loc) · 1.04 KB
/
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 transport
import (
"sync"
"sync/atomic"
"github.com/luopengift/log"
)
// Inputer 数据输入接口
type Inputer interface {
Init(Configer) error
Start() error
Read(p []byte) (n int, err error)
Close() error
Version() string
}
// Input input
type Input struct {
Name string
cnt uint64 //count numbers of input message
*sync.Mutex
Inputer
}
func NewInput(name string, in Inputer) *Input {
i := new(Input)
i.Name = name
i.Inputer = in
i.Mutex = new(sync.Mutex)
return i
}
func (i *Input) Set(in Inputer) error {
i.Mutex.Lock()
defer i.Mutex.Unlock()
if err := i.Inputer.Close(); err != nil {
return err
}
i.Inputer = in
return nil
}
func (i *Input) Count() uint64 {
return i.cnt
}
func (i *Input) Read(p []byte) (int, error) {
n, err := i.Inputer.Read(p)
atomic.AddUint64(&i.cnt, 1)
return n, err
}
func (i *Input) Start() error {
log.Info("Input Plugin[%v] starting...", i.Name)
return i.Inputer.Start()
}
func (i *Input) Close() error {
return i.Inputer.Close()
}
func (i *Input) Version() string {
return ""
}