-
Notifications
You must be signed in to change notification settings - Fork 5
/
output.go
65 lines (54 loc) · 1 KB
/
output.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"
)
// Outputer 数据输入接口, 实现了标准io库中的ReadCloser接口
type Outputer interface {
Init(Configer) error
Write(p []byte) (n int, err error)
Start() error
Close() error
Version() string
}
// Output output
type Output struct {
Name string
cnt uint64
*sync.Mutex
Outputer
}
// NewOutput new output
func NewOutput(name string, out Outputer) *Output {
o := new(Output)
o.Name = name
o.cnt = 0
o.Outputer = out
o.Mutex = new(sync.Mutex)
return o
}
// Set set
func (o *Output) Set(out Outputer) error {
if err := o.Outputer.Close(); err != nil {
return err
}
o.Outputer = out
return nil
}
// Count count
func (o *Output) Count() uint64 {
return o.cnt
}
func (o *Output) Write(p []byte) (int, error) {
n, err := o.Outputer.Write(p)
atomic.AddUint64(&o.cnt, 1)
return n, err
}
// Start start
func (o *Output) Start() error {
return o.Outputer.Start()
}
// Close close
func (o *Output) Close() error {
return o.Outputer.Close()
}