This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
224 lines (202 loc) · 7.4 KB
/
pool.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// MIT License
//
// Copyright (c) 2020 Nobody Night
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package santa
import "sync"
// StructMessagePool is a structure that contains instances of
// cached structured messages.
//
// The structure message pool allows the allocated structure message
// instance to be cached in the pool after use and reused in multiple
// hyper-threading contexts, which will significantly reduce the number
// of heap memory allocations.
type StructMessagePool struct {
pool *sync.Pool
}
// New gets and returns a reusable message instance from the buffer pool.
// If not, then allocate and return a new message instance.
func (p *StructMessagePool) New(text string, fields []Field) *StructMessage {
message := p.pool.Get().(*StructMessage)
message.Text = text
message.Fields = fields
return message
}
// Free returns the given message instance to the buffer pool. After the
// refund, the message instance is not allowed to be used again, otherwise
// the behavior is undefined.
func (p *StructMessagePool) Free(message *StructMessage) {
p.pool.Put(message)
}
// NewStructMessagePool creates and returns a structured message buffer
// pool instance.
func NewStructMessagePool() *StructMessagePool {
return &StructMessagePool {
pool: &sync.Pool {
New: func() interface { } {
return &StructMessage { }
},
},
}
}
// TemplateMessagePool is a structure that contains instances of
// cached template messages.
//
// The template message pool allows the allocated template message
// instance to be cached in the pool after use and reused in multiple
// hyper-threading contexts, which will significantly reduce the number
// of heap memory allocations.
type TemplateMessagePool struct {
pool *sync.Pool
}
// New gets and returns a reusable message instance from the buffer pool.
// If not, then allocate and return a new message instance.
func (p *TemplateMessagePool) New(template string, args []interface { }) *TemplateMessage {
message := p.pool.Get().(*TemplateMessage)
message.Template = template
message.Args = args
return message
}
// Free returns the given message instance to the buffer pool. After the
// refund, the message instance is not allowed to be used again, otherwise
// the behavior is undefined.
func (p *TemplateMessagePool) Free(message *TemplateMessage) {
p.pool.Put(message)
}
// NewTemplateMessagePool creates and returns a template message buffer
// pool instance.
func NewTemplateMessagePool() *TemplateMessagePool {
return &TemplateMessagePool {
pool: &sync.Pool {
New: func() interface { } {
return &TemplateMessage { }
},
},
}
}
// EntryPool is a structure that contains instances of cached log entries.
//
// The log entry pool allows the allocated and used log entry instances to
// be cached in the pool for use by other hyper-threading contexts, which
// will significantly reduce the number of heap memory allocations.
//
// Note that any instance of log entry should use this pool allocation.
type EntryPool struct {
pool *sync.Pool
}
// New gets and returns a reusable log entry instance from the buffer pool.
// If not, then allocate and return a new log entry instance.
//
// Please note that the log entry instance obtained and returned may be dirty,
// and the pool is not responsible for cleaning it.
func (p *EntryPool) New() *Entry {
return p.pool.Get().(*Entry)
}
// Free returns the given log entry instance to the buffer pool. After the
// refund, the log entry instance is not allowed to be used again, otherwise
// the behavior is undefined.
func (p *EntryPool) Free(entry *Entry) {
p.pool.Put(entry)
}
// NewEntryPool creates and returns a log entry buffer pool instance.
func NewEntryPool() *EntryPool {
return &EntryPool {
pool: &sync.Pool {
New: func() interface { } {
return &Entry { }
},
},
}
}
// ExporterBufferPool is a structure that contains instances of cached
// exporter buffer.
//
// The exporter buffer pool allows the allocated and used exporter buffer
// instances to be cached in the pool for use by other hyper-threading
// contexts, which will significantly reduce the number of heap memory
// allocations.
//
// Note that any instance of exporter buffer should use this pool
// allocation.
type ExporterBufferPool struct {
pool *sync.Pool
}
// New gets and returns a reusable exporter buffer instance from the
// buffer pool. If not, then allocate and return a new exporter buffer
// instance.
//
// Please note that the exporter buffer instance obtained and returned
// may be dirty, and the pool is not responsible for cleaning it.
func (p *ExporterBufferPool) New() *[]byte {
return p.pool.Get().(*[]byte)
}
// Free returns the given exporter buffer instance to the buffer pool.
// After the refund, the exporter buffer instance is not allowed to be
// used again, otherwise the behavior is undefined.
func (p *ExporterBufferPool) Free(buffer *[]byte) {
p.pool.Put(buffer)
}
// NewExporterBufferPool creates and returns a log entry buffer pool
// instance.
func NewExporterBufferPool(capacity int) *ExporterBufferPool {
return &ExporterBufferPool {
pool: &sync.Pool {
New: func() interface { } {
buffer := make([]byte, 0, capacity)
return &buffer
},
},
}
}
// GlobalPool is a structure that contains default instances of various
// pools. By using the global pool, some objects that need to be frequently
// instantiated will be cached in the global pool after use to facilitate
// reuse, which will significantly reduce the number of heap memory
// allocations.
type GlobalPool struct {
Entry *EntryPool
Message struct {
Structure *StructMessagePool
Template *TemplateMessagePool
}
Buffer struct {
Exporter *ExporterBufferPool
}
}
// NewGlobalPool creates instances of various pools and returns the value
// of the global pool. Unless necessary, applications should use
// GetGlobalPool to obtain the default global pool.
func NewGlobalPool() GlobalPool {
instance := GlobalPool {
Entry: NewEntryPool(),
}
instance.Message.Template = NewTemplateMessagePool()
instance.Message.Structure = NewStructMessagePool()
instance.Buffer.Exporter = NewExporterBufferPool(2048)
return instance
}
// pool is a structural variable that contains default instances of
// various pools. These pool instances are automatically created when
// the application is initialized and shared globally.
var pool GlobalPool = NewGlobalPool()
// GetGlobalPool returns the value of the default global pool.
func GetGlobalPool() GlobalPool {
return pool
}