-
Notifications
You must be signed in to change notification settings - Fork 18.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecated and moved writeflusher impl to internal pkg #47718
Open
krissetto
wants to merge
2
commits into
moby:master
Choose a base branch
from
krissetto:writeflusher-internal-impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−84
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package writeflusher | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
// Deprecated: use the internal WriteFlusher instead. | ||
// This is the old implementation that lived in ioutils. | ||
|
||
// This struct and all funcs below used to live in the pkg/ioutils package | ||
// | ||
// LegacyWriteFlusher wraps the Write and Flush operation ensuring that every write | ||
// is a flush. In addition, the Close method can be called to intercept | ||
// Read/Write calls if the targets lifecycle has already ended. | ||
// | ||
// Deprecated: use the internal writeflusher.WriteFlusher instead | ||
type LegacyWriteFlusher struct { | ||
w io.Writer | ||
flusher flusher | ||
flushed chan struct{} | ||
flushedOnce sync.Once | ||
closed chan struct{} | ||
closeLock sync.Mutex | ||
} | ||
|
||
// NewLegacyWriteFlusher returns a new LegacyWriteFlusher. | ||
// | ||
// Deprecated: use the internal writeflusher.NewWriteFlusher() instead | ||
func NewLegacyWriteFlusher(w io.Writer) *LegacyWriteFlusher { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still used in a bunch of places in API, which makes the linter complain:
we should also replace these with the new implementation. |
||
var fl flusher | ||
if f, ok := w.(flusher); ok { | ||
fl = f | ||
} else { | ||
fl = &NopFlusher{} | ||
} | ||
return &LegacyWriteFlusher{w: w, flusher: fl, closed: make(chan struct{}), flushed: make(chan struct{})} | ||
} | ||
|
||
// Deprecated: use the internal writeflusher.Write() instead | ||
func (wf *LegacyWriteFlusher) Write(b []byte) (n int, err error) { | ||
select { | ||
case <-wf.closed: | ||
return 0, errWriteFlusherClosed | ||
default: | ||
} | ||
|
||
n, err = wf.w.Write(b) | ||
wf.Flush() // every write is a flush. | ||
return n, err | ||
} | ||
|
||
// Flush the stream immediately. | ||
// | ||
// Deprecated: use the internal writeflusher.Flush() instead | ||
func (wf *LegacyWriteFlusher) Flush() { | ||
select { | ||
case <-wf.closed: | ||
return | ||
default: | ||
} | ||
|
||
wf.flushedOnce.Do(func() { | ||
close(wf.flushed) | ||
}) | ||
wf.flusher.Flush() | ||
} | ||
|
||
// Flushed returns the state of flushed. | ||
// If it's flushed, return true, or else it return false. | ||
// | ||
// Deprecated: use the internal writeflusher.WriteFlusher instead | ||
func (wf *LegacyWriteFlusher) Flushed() bool { | ||
// BUG(stevvooe): Remove this method. Its use is inherently racy. Seems to | ||
// be used to detect whether or a response code has been issued or not. | ||
// Another hook should be used instead. | ||
var flushed bool | ||
select { | ||
case <-wf.flushed: | ||
flushed = true | ||
default: | ||
} | ||
return flushed | ||
} | ||
|
||
// Close closes the write flusher, disallowing any further writes to the | ||
// target. After the flusher is closed, all calls to write or flush will | ||
// result in an error. | ||
// | ||
// Deprecated: use the internal writeflusher.Close() instead | ||
func (wf *LegacyWriteFlusher) Close() error { | ||
wf.closeLock.Lock() | ||
defer wf.closeLock.Unlock() | ||
|
||
select { | ||
case <-wf.closed: | ||
return errWriteFlusherClosed | ||
default: | ||
close(wf.closed) | ||
} | ||
return nil | ||
} |
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,89 @@ | ||
package writeflusher | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
type flusher interface { | ||
Flush() | ||
} | ||
|
||
var errWriteFlusherClosed = io.EOF | ||
|
||
// NopFlusher represents a type which flush operation is nop. | ||
type NopFlusher struct{} | ||
|
||
// Flush is a nop operation. | ||
func (f *NopFlusher) Flush() {} | ||
|
||
// WriteFlusher wraps the Write and Flush operation ensuring that every write | ||
// is a flush. In addition, the Close method can be called to intercept | ||
// Read/Write calls if the targets lifecycle has already ended. | ||
type WriteFlusher struct { | ||
w io.Writer | ||
flusher flusher | ||
closed chan struct{} | ||
closeLock sync.Mutex | ||
firstFlush sync.Once | ||
} | ||
|
||
// NewWriteFlusher returns a new WriteFlusher. | ||
func NewWriteFlusher(w io.Writer) *WriteFlusher { | ||
var fl flusher | ||
if f, ok := w.(flusher); ok { | ||
fl = f | ||
} else { | ||
fl = &NopFlusher{} | ||
} | ||
return &WriteFlusher{w: w, flusher: fl, closed: make(chan struct{})} | ||
} | ||
|
||
func (wf *WriteFlusher) Write(b []byte) (n int, err error) { | ||
select { | ||
case <-wf.closed: | ||
return 0, errWriteFlusherClosed | ||
default: | ||
} | ||
|
||
n, err = wf.w.Write(b) | ||
wf.Flush() // every write is a flush. | ||
return n, err | ||
} | ||
|
||
// Flush the stream immediately. | ||
func (wf *WriteFlusher) Flush() { | ||
select { | ||
case <-wf.closed: | ||
return | ||
default: | ||
} | ||
|
||
// Here we call WriteHeader() if the io.Writer is an http.ResponseWriter to ensure that we don't try | ||
// to send headers multiple times if the writer has already been wrapped by OTEL instrumentation | ||
// (which doesn't wrap the Flush() func. See https://github.com/moby/moby/issues/47448) | ||
wf.firstFlush.Do(func() { | ||
if rw, ok := wf.w.(http.ResponseWriter); ok { | ||
rw.WriteHeader(http.StatusOK) | ||
} | ||
}) | ||
|
||
wf.flusher.Flush() | ||
} | ||
|
||
// Close closes the write flusher, disallowing any further writes to the | ||
// target. After the flusher is closed, all calls to write or flush will | ||
// result in an error. | ||
func (wf *WriteFlusher) Close() error { | ||
wf.closeLock.Lock() | ||
defer wf.closeLock.Unlock() | ||
|
||
select { | ||
case <-wf.closed: | ||
return errWriteFlusherClosed | ||
default: | ||
close(wf.closed) | ||
} | ||
return nil | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add the deprecation and removal notice to https://github.com/docker/cli/blob/master/docs/deprecated.md? @thaJeztah
While not strictly an API, if we're bothering with the deprecation-cycle anyway, perhaps it wouldn't hurt to call it out in our docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR was created as a solution to the "superfluous call to writeheader" issue. It seemed like a good moment to go ahead and deprecate the old writeflusher implementation which was exposing some known buggy functionality (
moby/pkg/ioutils/writeflusher.go
Lines 54 to 64 in ae976b9
If we merge #47796, this PR will no longer be necessary unless we still want to deprecate the old writeflusher implementation