diff options
| author | Philipp Tanlak <philipp.tanlak@gmail.com> | 2024-02-17 21:48:14 +0100 |
|---|---|---|
| committer | Philipp Tanlak <philipp.tanlak@gmail.com> | 2024-02-17 21:48:14 +0100 |
| commit | c460e48cc920f39722bb9e404a399dbaa8d89c89 (patch) | |
| tree | 390b17279927790afd28eb8373053239e59003c4 /modules/output/ndjson | |
| parent | 0b4c723d2c310310efba41f58ee3a626510bcd3a (diff) | |
Fix race condition when writing JSON output
Diffstat (limited to 'modules/output/ndjson')
| -rw-r--r-- | modules/output/ndjson/ndjson.go | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/modules/output/ndjson/ndjson.go b/modules/output/ndjson/ndjson.go index 956b2ed..a24aaa6 100644 --- a/modules/output/ndjson/ndjson.go +++ b/modules/output/ndjson/ndjson.go @@ -9,6 +9,7 @@ import ( "io" "log" "os" + "sync" "time" "github.com/philippta/flyscrape" @@ -24,7 +25,8 @@ type Module struct { File string `json:"file"` } `json:"output"` - w io.WriteCloser + w io.WriteCloser + mu *sync.Mutex } func (Module) ModuleInfo() flyscrape.ModuleInfo { @@ -39,6 +41,8 @@ func (m *Module) Provision(ctx flyscrape.Context) { return } + m.mu = &sync.Mutex{} + if m.Output.File == "" { m.w = nopCloser{os.Stdout} return @@ -70,6 +74,9 @@ func (m *Module) ReceiveResponse(resp *flyscrape.Response) { o.Error = resp.Error.Error() } + m.mu.Lock() + defer m.mu.Unlock() + enc := json.NewEncoder(m.w) enc.SetEscapeHTML(false) enc.Encode(o) |