-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid recorder for io.ReadSeeker in anyio.NewReaderWithOpts (#4790)
NewReaderWithOpts always creates a Recorder, which buffers its input and can cause format detection to fail if the buffer is too small. Avoid that problem when the passed reader implements io.ReadSeeker by modifying Track to create a Recorder only when the reader cannot seek. Closes #4586.
- Loading branch information
Showing
4 changed files
with
64 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
package anyio | ||
|
||
import "io" | ||
|
||
const TrackSize = InitBufferSize | ||
|
||
type Track struct { | ||
rs io.ReadSeeker | ||
initial int64 | ||
|
||
recorder *Recorder | ||
off int | ||
} | ||
|
||
func NewTrack(r *Recorder) *Track { | ||
func NewTrack(r io.Reader) *Track { | ||
if rs, ok := r.(io.ReadSeeker); ok { | ||
if n, err := rs.Seek(0, io.SeekCurrent); err == nil { | ||
return &Track{rs: rs, initial: n} | ||
} | ||
} | ||
return &Track{ | ||
recorder: r, | ||
recorder: NewRecorder(r), | ||
} | ||
} | ||
|
||
func (t *Track) Reset() { | ||
if t.rs != nil { | ||
// We ignore errors here under the assumption that a subsequent | ||
// call to Read will also fail. | ||
t.rs.Seek(t.initial, io.SeekStart) | ||
return | ||
} | ||
t.off = 0 | ||
} | ||
|
||
func (t *Track) Read(b []byte) (int, error) { | ||
if t.rs != nil { | ||
return t.rs.Read(b) | ||
} | ||
n, err := t.recorder.ReadAt(t.off, b) | ||
t.off += n | ||
return n, err | ||
} | ||
|
||
func (t *Track) Reader() io.Reader { | ||
if t.rs != nil { | ||
t.Reset() | ||
return t.rs | ||
} | ||
return t.recorder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
script: | | ||
! yes ' ' | head -c $((11 * 1024 * 1024)) > huge.zson | ||
echo 0 >> huge.zson | ||
zq -z huge.zson | ||
! cat huge.zson | zq -z - | ||
outputs: | ||
- name: stdout | ||
data: | | ||
0 | ||
- name: stderr | ||
data: | | ||
stdio:stdin: format detection error | ||
arrows: schema message length exceeds 1 MiB | ||
csv: line 1: no comma found | ||
json: buffer exceeded max size trying to infer input format | ||
line: auto-detection not supported | ||
parquet: auto-detection requires seekable input | ||
vng: auto-detection requires seekable input | ||
zeek: line 1: bad types/fields definition in zeek header | ||
zjson: line 1: malformed ZJSON: bad type object: "": unpacker error parsing JSON: unexpected end of JSON input | ||
zng: buffer exceeded max size trying to infer input format | ||
zson: buffer exceeded max size trying to infer input format |