-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautosplitfile.go
More file actions
183 lines (154 loc) · 4.03 KB
/
autosplitfile.go
File metadata and controls
183 lines (154 loc) · 4.03 KB
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
package autosplitfile
import (
"fmt"
"path"
"os"
"strconv"
"time"
"strings"
"bytes"
)
type FileOptions struct {
PathPrefix string
BufferedLines int
MaxSize int64
MaxTime string
}
type record struct {
time time.Time
data []byte
}
type File struct {
pathPrefix string
maxSize int64
maxTime time.Duration
actualFile *os.File
occurredError error
waitWriteRecord chan *record
waitSyncRoutine chan struct{}
}
const timeLayout = "20060102-15-04"
const sequenceStart = 1
func New(options *FileOptions) (fp *File, err error) {
maxTime, err := time.ParseDuration(options.MaxTime)
if err != nil {
return
} else if maxTime < time.Minute {
// maxTime can not less than 1 minute.
maxTime = time.Minute
}
fp = &File{
path.Clean(options.PathPrefix),
options.MaxSize,
maxTime,
nil,
nil,
make(chan *record, options.BufferedLines),
make(chan struct{}),
}
// sync routine
go func() {
defer func() {
close(fp.waitSyncRoutine)
}()
for rec := range fp.waitWriteRecord {
if fp.occurredError != nil {
continue
}
err := fp.writeRecordToActualFile(rec)
if err != nil {
// todo: atomic write to the pointer
fp.occurredError = err
}
}
}()
return
}
func (fp *File) Write(p []byte) (n int, err error) {
// todo: atomic read the pointer.
if fp.occurredError != nil {
return 0, fp.occurredError
}
fp.waitWriteRecord <- &record{time.Now(), bytes.Repeat(p, 1)}
return len(p), nil
}
func (fp *File) Close() error {
close(fp.waitWriteRecord)
<-fp.waitSyncRoutine
return fp.actualFile.Close()
}
func (fp *File) refreshActualFile(currentRecordTime time.Time, increasingSize int) (err error) {
var expectedFilename string
if fp.actualFile != nil {
expectedFilename, err = fp.expectedActualFilePath(currentRecordTime, increasingSize)
if fp.currentActualFilePath() == expectedFilename {
return
} else {
err = fp.actualFile.Close()
if err != nil {
return
}
fp.actualFile = nil
}
} else {
expectedFilename = fp.buildActualFilePath(currentRecordTime, sequenceStart)
}
fp.actualFile, err = os.OpenFile(expectedFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
return
}
func (fp *File) currentActualFilePath() string {
return path.Clean(fp.actualFile.Name())
}
func (fp *File) buildActualFilePath(t time.Time, seq int) string {
// the log file name is in this format: prefix.20060102-15-04.0001 .
// Truncate() can only deal with UTC, so add the timezone offset before Truncate() and sub it after.
_, offsetSeconds := t.Local().Zone()
offsetDuration := time.Second * time.Duration(offsetSeconds)
t = t.Add(offsetDuration).Truncate(fp.maxTime).Add(-offsetDuration)
return fmt.Sprintf("%s.%s.%04d", fp.pathPrefix, t.Format(timeLayout), seq)
}
func (fp *File) expectedActualFilePath(currentRecordTime time.Time, increasingSize int) (res string, err error) {
// extract time and sequence number from current filename.
filePath := fp.currentActualFilePath()
suffix := strings.TrimPrefix(filePath, fp.pathPrefix+".")
parts := strings.Split(suffix, ".")
timePart := parts[0]
seqPart := parts[1]
fileTime, err := time.ParseInLocation(timeLayout, timePart, time.Local)
if err != nil {
return
}
fileSeq, err := strconv.Atoi(seqPart)
if err != nil {
return
}
// make expected filename.
expectedTime := fileTime
expectedSeq := fileSeq
if currentRecordTime.Sub(fileTime) > fp.maxTime {
expectedTime = currentRecordTime
expectedSeq = sequenceStart
} else {
var info os.FileInfo
info, err = fp.actualFile.Stat()
if err != nil {
return
} else if info.Size() + int64(increasingSize) >= fp.maxSize {
expectedSeq++
}
}
res = fp.buildActualFilePath(expectedTime, expectedSeq)
return
}
func (fp *File) writeRecordToActualFile(record *record) (err error) {
err = fp.refreshActualFile(record.time, len(record.data))
if err != nil {
return
}
ret, err := fp.actualFile.Write(record.data)
if err == nil && ret < len(record.data) {
// unlikely, but should care.
err = fmt.Errorf("written length less than expected")
}
return
}