Fixed race conditions on exporters.
MaxIdleConns limit disabled to support unlimited requests to all hosts. MaxIdleConnsPerHost limit increased to speed up requests to same host.
This commit is contained in:
@ -15,7 +15,7 @@ type CSVExporter struct {
|
||||
FileName string
|
||||
|
||||
once sync.Once
|
||||
file *os.File
|
||||
mut sync.Mutex
|
||||
writer *csv.Writer
|
||||
}
|
||||
|
||||
@ -33,8 +33,7 @@ func (e *CSVExporter) Export(response *geziyor.Response) {
|
||||
fmt.Fprintf(os.Stderr, "output file creation error: %v", err)
|
||||
return
|
||||
}
|
||||
e.file = newFile
|
||||
e.writer = csv.NewWriter(e.file)
|
||||
e.writer = csv.NewWriter(newFile)
|
||||
})
|
||||
|
||||
// Export data as responses came
|
||||
@ -59,9 +58,14 @@ func (e *CSVExporter) Export(response *geziyor.Response) {
|
||||
}
|
||||
|
||||
// Write to file
|
||||
e.mut.Lock()
|
||||
if err := e.writer.Write(values); err != nil {
|
||||
log.Printf("CSV writing error on exporter: %v\n", err)
|
||||
}
|
||||
e.writer.Flush()
|
||||
e.mut.Unlock()
|
||||
}
|
||||
|
||||
e.mut.Lock()
|
||||
e.writer.Flush()
|
||||
e.mut.Unlock()
|
||||
}
|
||||
|
@ -14,8 +14,9 @@ type JSONExporter struct {
|
||||
FileName string
|
||||
EscapeHTML bool
|
||||
|
||||
once sync.Once
|
||||
file *os.File
|
||||
once sync.Once
|
||||
mut sync.Mutex
|
||||
encoder *json.Encoder
|
||||
}
|
||||
|
||||
// Export exports response data as JSON streaming file
|
||||
@ -33,15 +34,16 @@ func (e *JSONExporter) Export(response *geziyor.Response) {
|
||||
fmt.Fprintf(os.Stderr, "output file creation error: %v", err)
|
||||
return
|
||||
}
|
||||
e.file = newFile
|
||||
e.encoder = json.NewEncoder(newFile)
|
||||
e.encoder.SetEscapeHTML(e.EscapeHTML)
|
||||
})
|
||||
|
||||
// Export data as responses came
|
||||
for res := range response.Exports {
|
||||
encoder := json.NewEncoder(e.file)
|
||||
encoder.SetEscapeHTML(e.EscapeHTML)
|
||||
if err := encoder.Encode(res); err != nil {
|
||||
e.mut.Lock()
|
||||
if err := e.encoder.Encode(res); err != nil {
|
||||
log.Printf("JSON encoding error on exporter: %v\n", err)
|
||||
}
|
||||
e.mut.Unlock()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user