-
Notifications
You must be signed in to change notification settings - Fork 1
/
buffer.go
156 lines (128 loc) · 3.47 KB
/
buffer.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package zlog
import (
"sync"
"time"
)
const (
DEFALUT_BUFFER_SIZE int = 20000*1024
DEFAULT_BUFFER_NUM int = 20
)
type LogMsgBuffer struct {
buffer []byte
startWriteIndex int //next write index
capacity int
}
func NewLogMsgBuffer(bufferSize int) *LogMsgBuffer {
if bufferSize == 0 {
return nil
}
return &LogMsgBuffer{
buffer: make([]byte, bufferSize),
startWriteIndex: 0,
capacity: bufferSize,
}
}
func (buf *LogMsgBuffer) AppendString(logStr string) {
n := copy(buf.buffer[buf.startWriteIndex:], logStr)
buf.startWriteIndex += n;
}
func (buf *LogMsgBuffer) AppendByte(logByte []byte) {
n := copy(buf.buffer[buf.startWriteIndex:], logByte)
buf.startWriteIndex += n;
}
func (buf *LogMsgBuffer) GetLength() int {
return buf.startWriteIndex
}
func (buf *LogMsgBuffer) GetBytes() []byte {
return buf.buffer[:buf.startWriteIndex]
}
func (buf *LogMsgBuffer) GetAvailLength() int {
return buf.capacity - buf.startWriteIndex
}
func (buf *LogMsgBuffer) Clear() {
if buf.capacity > 0 {
if (cap(buf.buffer) == buf.capacity) {
buf.startWriteIndex = 0 //直接复用 之前分配的空间.
} else {
buf.buffer = make([]byte, 0, buf.capacity)
buf.startWriteIndex = 0
}
}
}
type BufferContainer struct {
buffers []*LogMsgBuffer
bufferCap int //每个buffer的容量
buffersCap int
mutex sync.Mutex
cufBufCond *TimeoutCond
}
func NewBufferContainer(bufsSize int, bufsCap int, capPerBuf int) *BufferContainer {
if capPerBuf == 0 {
return nil
}
buffers := &BufferContainer{}
buffers.bufferCap = capPerBuf
buffers.buffersCap = bufsCap
buffers.buffers = make([]*LogMsgBuffer, bufsSize, bufsCap)
buffers.cufBufCond = NewTimeoutCond(&buffers.mutex)
for i:= 0; i < bufsSize; i++ {
buffers.buffers[i] = NewLogMsgBuffer(capPerBuf)
if buffers.buffers[i] == nil {
return nil
}
}
return buffers
}
func (bufs *BufferContainer) PopBuffer() *LogMsgBuffer {
bufs.mutex.Lock()
defer bufs.mutex.Unlock()
if len(bufs.buffers) > 0 {
buffer := bufs.buffers[0]
bufs.buffers = bufs.buffers[1:] //slice 在删除某个元素时,会 自动释放 被删除元素的内存空间吗?
return buffer
} else {
return nil
}
}
func (bufs *BufferContainer) GetAllBuffersAndClear() []*LogMsgBuffer {
bufs.mutex.Lock()
defer bufs.mutex.Unlock()
retBufs := bufs.buffers
bufs.buffers = make([]*LogMsgBuffer, 0, bufs.buffersCap)
return retBufs
}
func (bufs *BufferContainer) PushBuffer(buf *LogMsgBuffer) {
bufs.mutex.Lock();
defer bufs.mutex.Unlock();
bufs.buffers = append(bufs.buffers, buf)
if bufs.cufBufCond.HasWaiters() == true {
bufs.cufBufCond.Signal()
}
}
func (bufs *BufferContainer) PushBuffers(tmpBufs []*LogMsgBuffer) {
bufs.mutex.Lock();
defer bufs.mutex.Unlock();
bufs.buffers = append(bufs.buffers, tmpBufs...)
if bufs.cufBufCond.HasWaiters() == true {
bufs.cufBufCond.Signal()
}
}
func (bufs *BufferContainer) WaitNewBuffer(sec int) {
bufs.mutex.Lock()
if len(bufs.buffers) == 0 { //unsual usage!
bufs.cufBufCond.WaitWithTimeout(time.Duration(sec) * time.Second)
}
bufs.mutex.Unlock()
}
func (bufs *BufferContainer) WaitNewBufferUnlimitedTime() {
bufs.mutex.Lock()
for ; len(bufs.buffers) == 0; {
bufs.cufBufCond.Wait()
}
bufs.mutex.Unlock()
}
func (bufs *BufferContainer) IsEmpty() bool {
bufs.mutex.Lock()
defer bufs.mutex.Unlock()
return len(bufs.buffers) == 0
}