Skip to content

Commit 69292aa

Browse files
committed
Writer 修改:压缩时后缀变为微秒级别 + 按大小切割时考虑缓冲区中未写入的数据 + appendInt 疑似为重复代码
1 parent 59e7837 commit 69292aa

File tree

1 file changed

+8
-64
lines changed

1 file changed

+8
-64
lines changed

zutils/witer.go

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,17 @@ func (w *Writer) Write(p []byte) (n int, err error) {
103103
}
104104

105105
t := time.Now()
106-
var b []byte
107-
year, month, day := t.Date()
108-
b = appendInt(b, year, 4)
109-
b = append(b, '-')
110-
b = appendInt(b, int(month), 2)
111-
b = append(b, '-')
112-
b = appendInt(b, day, 2)
106+
b := t.AppendFormat(nil, time.RFC3339)
113107

114108
// 按天切割
115-
if !bytes.Equal(w.creates[:10], b) { //2023-04-05
109+
if !bytes.Equal(w.creates[:10], b[:10]) { //2023-04-05
116110
go w.delete() // 每天检测一次旧文件
117111
if err := w.rotate(); err != nil {
118112
return 0, err
119113
}
120114
}
121-
// 按大小切割
122-
if w.size+int64(len(p)) >= w.maxSize {
115+
// 按大小切割,需要考虑bw缓冲区中未写入的数据
116+
if w.size+int64(len(p))+int64(w.bw.Buffered()) >= w.maxSize {
123117
if err := w.rotate(); err != nil {
124118
return 0, err
125119
}
@@ -194,13 +188,15 @@ func (w *Writer) delete() {
194188
}
195189
}
196190
}
191+
197192
func (w *Writer) name2time(name string) (time.Time, error) {
198193
name = strings.TrimPrefix(name, filepath.Base(w.fname))
199194
name = strings.TrimSuffix(name, w.zipsuffix)
200-
return time.Parse(".2006-01-02-150405", name)
195+
// 改为微秒级别的文件后缀,避免1s内大量写入造成多次rotate,而覆盖丢失之前的日志文件
196+
return time.Parse(".2006-01-02-150405.000000", name)
201197
}
202198
func (w *Writer) time2name(t time.Time) string {
203-
return t.Format(".2006-01-02-150405")
199+
return t.Format(".2006-01-02-150405.000000")
204200
}
205201

206202
func (w *Writer) Close() error {
@@ -230,58 +226,6 @@ func (w *Writer) flush() error {
230226
return w.bw.Flush()
231227
}
232228

233-
// appendInt appends the decimal form of x to b and returns the result.
234-
// If the decimal form (excluding sign) is shorter than width, the result is padded with leading 0's.
235-
// Duplicates functionality in strconv, but avoids dependency.
236-
func appendInt(b []byte, x int, width int) []byte {
237-
u := uint(x)
238-
if x < 0 {
239-
b = append(b, '-')
240-
u = uint(-x)
241-
}
242-
243-
// 2-digit and 4-digit fields are the most common in time formats.
244-
utod := func(u uint) byte { return '0' + byte(u) }
245-
switch {
246-
case width == 2 && u < 1e2:
247-
return append(b, utod(u/1e1), utod(u%1e1))
248-
case width == 4 && u < 1e4:
249-
return append(b, utod(u/1e3), utod(u/1e2%1e1), utod(u/1e1%1e1), utod(u%1e1))
250-
}
251-
252-
// Compute the number of decimal digits.
253-
var n int
254-
if u == 0 {
255-
n = 1
256-
}
257-
for u2 := u; u2 > 0; u2 /= 10 {
258-
n++
259-
}
260-
261-
// Add 0-padding.
262-
for pad := width - n; pad > 0; pad-- {
263-
b = append(b, '0')
264-
}
265-
266-
// Ensure capacity.
267-
if len(b)+n <= cap(b) {
268-
b = b[:len(b)+n]
269-
} else {
270-
b = append(b, make([]byte, n)...)
271-
}
272-
273-
// Assemble decimal in reverse order.
274-
i := len(b) - 1
275-
for u >= 10 && i > 0 {
276-
q := u / 10
277-
b[i] = utod(u - q*10)
278-
u = q
279-
i--
280-
}
281-
b[i] = utod(u)
282-
return b
283-
}
284-
285229
// ZipToFile 压缩至文件
286230
// @params dst string 压缩文件目标路径
287231
// @params src string 待压缩源文件/目录路径

0 commit comments

Comments
 (0)