Skip to content

Commit

Permalink
Merge pull request #974 from lins05/fix-file-output-size-limit
Browse files Browse the repository at this point in the history
This should fix #973 . Tested with the following command:

```bash
gor --input-raw 127.0.0.1:8080 \
      --output-file '%Y-%m-%d.log' \
       --output-file-queue-limit 0 \
      --output-file-size-limit 1mb
```
  • Loading branch information
buger authored Jul 27, 2021
2 parents 214edb4 + debc3b4 commit 0f2f93a
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions output_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ type FileOutputConfig struct {
// FileOutput output plugin
type FileOutput struct {
sync.RWMutex
pathTemplate string
currentName string
file *os.File
QueueLength int
chunkSize int
writer io.Writer
requestPerFile bool
currentID []byte
payloadType []byte
closed bool
totalFileSize size.Size
pathTemplate string
currentName string
file *os.File
QueueLength int
writer io.Writer
requestPerFile bool
currentID []byte
payloadType []byte
closed bool
currentFileSize int
totalFileSize size.Size

config *FileOutputConfig
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (o *FileOutput) filename() string {

if o.currentName == "" ||
((o.config.QueueLimit > 0 && o.QueueLength >= o.config.QueueLimit) ||
(o.config.SizeLimit > 0 && o.chunkSize >= int(o.config.SizeLimit))) {
(o.config.SizeLimit > 0 && o.currentFileSize >= int(o.config.SizeLimit))) {
nextChunk = true
}

Expand All @@ -195,6 +195,7 @@ func (o *FileOutput) filename() string {

if nextChunk {
fileIndex++
o.currentFileSize = 0
}
}

Expand Down Expand Up @@ -253,6 +254,7 @@ func (o *FileOutput) PluginWrite(msg *Message) (n int, err error) {
n += nn

o.totalFileSize += size.Size(n)
o.currentFileSize += n
o.QueueLength++

if Settings.OutputFileConfig.OutputFileMaxSize > 0 && o.totalFileSize >= Settings.OutputFileConfig.OutputFileMaxSize {
Expand Down Expand Up @@ -281,7 +283,7 @@ func (o *FileOutput) flush() {
}

if stat, err := o.file.Stat(); err == nil {
o.chunkSize = int(stat.Size())
o.currentFileSize = int(stat.Size())
} else {
Debug(0, "[OUTPUT-HTTP] error accessing file size", err)
}
Expand Down

0 comments on commit 0f2f93a

Please sign in to comment.