[go] compress/flate: move idempotent close logic to compressor

15 views
Skip to first unread message

Joseph Tsai (Gerrit)

unread,
May 2, 2022, 2:46:08 PM5/2/22
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joseph Tsai has uploaded this change for review.

View Change

compress/flate: move idempotent close logic to compressor

The compressor methods already have logic for handling a sticky error.
Merge the logic from CL 136475 into that.

This slightly changes the error message to be more sensible
in the situation where it's returned by Flush.

Updates #27741

Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
---
M src/compress/flate/deflate.go
M src/compress/flate/deflate_test.go
2 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/src/compress/flate/deflate.go b/src/compress/flate/deflate.go
index ccf03d7..4ca20b8 100644
--- a/src/compress/flate/deflate.go
+++ b/src/compress/flate/deflate.go
@@ -640,6 +640,9 @@
}

func (d *compressor) close() error {
+ if d.err == errWriterClosed {
+ return nil
+ }
if d.err != nil {
return d.err
}
@@ -652,7 +655,11 @@
return d.w.err
}
d.w.flush()
- return d.w.err
+ if d.w.err != nil {
+ return d.w.err
+ }
+ d.err = errWriterClosed
+ return nil
}

// NewWriter returns a new Writer compressing data at the given level.
@@ -700,27 +707,19 @@
return w.w.Write(b)
}

-var errWriteAfterClose = errors.New("compress/flate: write after close")
+var errWriterClosed = errors.New("flate: closed writer")

// A Writer takes data written to it and writes the compressed
// form of that data to an underlying writer (see NewWriter).
type Writer struct {
d compressor
dict []byte
- err error
}

// Write writes data to w, which will eventually write the
// compressed form of data to its underlying writer.
func (w *Writer) Write(data []byte) (n int, err error) {
- if w.err != nil {
- return 0, w.err
- }
- n, err = w.d.write(data)
- if err != nil {
- w.err = err
- }
- return n, err
+ return w.d.write(data)
}

// Flush flushes any pending data to the underlying writer.
@@ -735,37 +734,18 @@
func (w *Writer) Flush() error {
// For more about flushing:
// https://www.bolet.org/~pornin/deflate-flush.html
- if w.err != nil {
- return w.err
- }
- if err := w.d.syncFlush(); err != nil {
- w.err = err
- return err
- }
- return nil
+ return w.d.syncFlush()
}

// Close flushes and closes the writer.
func (w *Writer) Close() error {
- if w.err == errWriteAfterClose {
- return nil
- }
- if w.err != nil {
- return w.err
- }
- if err := w.d.close(); err != nil {
- w.err = err
- return err
- }
- w.err = errWriteAfterClose
- return nil
+ return w.d.close()
}

// Reset discards the writer's state and makes it equivalent to
// the result of NewWriter or NewWriterDict called with dst
// and w's level and dictionary.
func (w *Writer) Reset(dst io.Writer) {
- w.err = nil
if dw, ok := w.d.w.writer.(*dictWriter); ok {
// w was created with NewWriterDict
dw.w = dst
diff --git a/src/compress/flate/deflate_test.go b/src/compress/flate/deflate_test.go
index 8c9ee72..6d20430 100644
--- a/src/compress/flate/deflate_test.go
+++ b/src/compress/flate/deflate_test.go
@@ -143,7 +143,7 @@
afterClose := b.Len()

if c, err := zw.Write([]byte("Test")); err == nil || c != 0 {
- t.Fatalf("Write to closed writer: %s, %d", err, c)
+ t.Fatalf("Write to closed writer: %v, %d", err, c)
}

if err := zw.Flush(); err == nil {
@@ -794,7 +794,7 @@

flushErr = zw.Flush()
_, writeErr = zw.Write([]byte("Test"))
- checkErrors([]error{flushErr, writeErr}, errWriteAfterClose, t)
+ checkErrors([]error{flushErr, writeErr}, errWriterClosed, t)
}

func checkErrors(got []error, want error, t *testing.T) {

To view, visit change 403514. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
Gerrit-Change-Number: 403514
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-MessageType: newchange

Ian Lance Taylor (Gerrit)

unread,
May 2, 2022, 3:48:05 PM5/2/22
to Joseph Tsai, goph...@pubsubhelper.golang.org, Matthew Dempsky, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: Joseph Tsai.

Patch set 1:Run-TryBot +1Auto-Submit +1Code-Review +2

View Change

1 comment:

To view, visit change 403514. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
Gerrit-Change-Number: 403514
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Matthew Dempsky <mdem...@google.com>
Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
Gerrit-Comment-Date: Mon, 02 May 2022 19:48:00 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Dmitri Shuralyov (Gerrit)

unread,
May 2, 2022, 5:05:49 PM5/2/22
to Joseph Tsai, goph...@pubsubhelper.golang.org, Gopher Robot, Matthew Dempsky, golang-co...@googlegroups.com

Attention is currently required from: Joseph Tsai.

Patch set 1:Code-Review +1

View Change

    To view, visit change 403514. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
    Gerrit-Change-Number: 403514
    Gerrit-PatchSet: 1
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
    Gerrit-CC: Matthew Dempsky <mdem...@google.com>
    Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Comment-Date: Mon, 02 May 2022 21:05:45 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Gopher Robot (Gerrit)

    unread,
    May 2, 2022, 5:05:58 PM5/2/22
    to Joseph Tsai, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Dmitri Shuralyov, Matthew Dempsky, golang-co...@googlegroups.com

    Gopher Robot submitted this change.

    View Change


    Approvals: Ian Lance Taylor: Looks good to me, approved; Run TryBots; Automatically submit change Dmitri Shuralyov: Looks good to me, but someone else must approve Gopher Robot: TryBots succeeded
    compress/flate: move idempotent close logic to compressor

    The compressor methods already have logic for handling a sticky error.
    Merge the logic from CL 136475 into that.

    This slightly changes the error message to be more sensible
    in the situation where it's returned by Flush.

    Updates #27741

    Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
    Reviewed-on: https://go-review.googlesource.com/c/go/+/403514
    Reviewed-by: Dmitri Shuralyov <dmit...@google.com>
    TryBot-Result: Gopher Robot <go...@golang.org>
    Reviewed-by: Ian Lance Taylor <ia...@google.com>
    Run-TryBot: Ian Lance Taylor <ia...@google.com>
    Auto-Submit: Ian Lance Taylor <ia...@google.com>

    ---
    M src/compress/flate/deflate.go
    M src/compress/flate/deflate_test.go
    2 files changed, 37 insertions(+), 34 deletions(-)

    To view, visit change 403514. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie34cf3164d0fa6bd0811175ca467dbbcb3be1395
    Gerrit-Change-Number: 403514
    Gerrit-PatchSet: 2
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
    Gerrit-CC: Matthew Dempsky <mdem...@google.com>
    Gerrit-MessageType: merged
    Reply all
    Reply to author
    Forward
    0 new messages