Add a Close method to rac.Codec{Reader,Writer}

This commit is contained in:
Nigel Tao committed 2019-09-10 09:19:01 +10:00
1 parent fed2073b00
commit 8d408c7c2c
3 files changed
+83 -25

No files matched your search

+44 -11
View File
@@ -49,6 +49,9 @@ type ReaderContext struct {
//
// Instances are not required to support concurrent use.
type CodecReader interface {
// Close tells the CodecReader that no further calls will be made.
Close() error
// Accepts returns whether this CodecReader can decode a Codec.
Accepts(c Codec) bool
@@ -135,6 +138,9 @@ type Reader struct {
decompressor io.Reader
inImplicitZeroes bool
// closed is whether this Reader is closed.
closed bool
// currChunk is an io.Reader for the current chunk, used while in "State
// B". It serves zlib-compressed data, which the (non-nil) decompressor
// turns into decompressed data.
@@ -505,6 +511,8 @@ func (r *Reader) seek(offset int64, whence int, limit int64) (int64, error) {
// Close implements io.Closer.
//
// Calling Close will call Close on all of r's CodecReaders.
//
// r.ReadSeeker will not be accessed after Close returns. If r.Concurrency is
// non-zero, this may involve waiting for various goroutines to shut down,
// which may take some time. If the caller does not care about waiting until it
@@ -514,19 +522,44 @@ func (r *Reader) seek(offset int64, whence int, limit int64) (int64, error) {
// It is not safe to call Close from a separate goroutine while another method
// call like Read or Seek is in progress.
func (r *Reader) Close() error {
if err := r.initialize(); err != nil {
r.concReader.Close()
return err
}
r.err = errAlreadyClosed
return r.concReader.Close()
return r.close(false)
}
// CloseWithoutWaiting is like Close but does not wait until it is safe to
// close or otherwise release the r.ReadSeeker's resources.
func (r *Reader) CloseWithoutWaiting() error {
if err := r.initialize(); err != nil {
r.concReader.CloseWithoutWaiting()
return err
return r.close(true)
}
func (r *Reader) close(withoutWaiting bool) error {
if r.closed {
return r.err
}
r.err = errAlreadyClosed
return r.concReader.CloseWithoutWaiting()
r.closed = true
if err := r.initialize(); r.err == nil {
r.err = err
}
for _, c := range r.CodecReaders {
if err := c.Close(); r.err == nil {
r.err = err
}
}
if withoutWaiting {
if err := r.concReader.CloseWithoutWaiting(); r.err == nil {
r.err = err
}
} else {
if err := r.concReader.Close(); r.err == nil {
r.err = err
}
}
if r.err == nil {
r.err = errAlreadyClosed
return nil
}
return r.err
}
+28 -13
View File
@@ -156,6 +156,9 @@ const NoResourceUsed int = -1
//
// Instances are not required to support concurrent use.
type CodecWriter interface {
// Close tells the CodecWriter that no further calls will be made.
Close() error
// Clone duplicates this. The clone and the original can run concurrently.
Clone() CodecWriter
@@ -331,6 +334,9 @@ type Writer struct {
// uncompressed are the uncompressed bytes that have been given to this
// (via the Write method) but not yet compressed as a chunk.
uncompressed writeBuffer
// closed is whether this Writer is closed.
closed bool
}
func (w *Writer) initialize() error {
@@ -562,6 +568,8 @@ func (w *Writer) tryCChunk(targetDChunkSize uint64, force bool) error {
// Close writes the RAC index to w.Writer and marks that w accepts no further
// method calls.
//
// Calling Close will call Close on w's CodecWriter.
//
// For a one pass encoding, no further action is taken. For a two pass encoding
// (i.e. IndexLocationAtStart), it then copies w.TempFile to w.Writer. Either
// way, if this method returns nil error, the entirety of what was written to
@@ -569,21 +577,28 @@ func (w *Writer) tryCChunk(targetDChunkSize uint64, force bool) error {
//
// Closing the underlying w.Writer, w.TempFile or both is the responsibility of
// the rac.Writer caller, not the rac.Writer itself.
//
// It is not necessary to call Close, e.g. if an earlier Write call returned a
// non-nil error. Unlike an os.File, failing to call rac.Writer.Close will not
// leak resources such as file descriptors.
func (w *Writer) Close() error {
if err := w.initialize(); err != nil {
return err
if w.closed {
return w.err
}
if err := w.write(true); err != nil {
return err
}
if err := w.chunkWriter.Close(); err != nil {
w.closed = true
if err := w.initialize(); w.err == nil {
w.err = err
return err
}
w.err = errAlreadyClosed
return nil
if w.err == nil {
w.err = w.write(true)
}
if w.err == nil {
w.err = w.chunkWriter.Close()
}
if err := w.CodecWriter.Close(); w.err == nil {
w.err = err
}
if w.err == nil {
w.err = errAlreadyClosed
return nil
}
return w.err
}
+11 -1
View File
@@ -79,6 +79,11 @@ type CodecReader struct {
buf [2]byte
}
// Close implements rac.CodecReader.
func (r *CodecReader) Close() error {
return nil
}
// Accepts implements rac.CodecReader.
func (r *CodecReader) Accepts(c rac.Codec) bool {
return c == rac.CodecZlib
@@ -163,8 +168,13 @@ type CodecWriter struct {
stash []byte
}
// Close implements rac.CodecWriter.
func (w *CodecWriter) Close() error {
return nil
}
// Clone implements rac.CodecWriter.
func (r *CodecWriter) Clone() rac.CodecWriter {
func (w *CodecWriter) Clone() rac.CodecWriter {
return &CodecWriter{}
}