asyncreader: Make StopBuffer as well as Abandon and fix confusion in callers

This commit is contained in:
Nick Craig-Wood 2020-06-08 16:24:29 +01:00
parent 05bc19c331
commit 3c14a893fb
2 changed files with 25 additions and 4 deletions

View File

@ -128,6 +128,13 @@ func (acc *Account) GetAsyncReader() *asyncreader.AsyncReader {
// StopBuffering stops the async buffer doing any more buffering // StopBuffering stops the async buffer doing any more buffering
func (acc *Account) StopBuffering() { func (acc *Account) StopBuffering() {
if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok {
asyncIn.StopBuffering()
}
}
// Abandon stops the async buffer doing any more buffering
func (acc *Account) Abandon() {
if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok { if asyncIn, ok := acc.in.(*asyncreader.AsyncReader); ok {
asyncIn.Abandon() asyncIn.Abandon()
} }
@ -139,7 +146,7 @@ func (acc *Account) UpdateReader(in io.ReadCloser) {
acc.mu.Lock() acc.mu.Lock()
withBuf := acc.withBuf withBuf := acc.withBuf
if withBuf { if withBuf {
acc.StopBuffering() acc.Abandon()
acc.withBuf = false acc.withBuf = false
} }
acc.in = in acc.in = in

View File

@ -268,9 +268,15 @@ func (a *AsyncReader) SkipBytes(skip int) (ok bool) {
} }
} }
// Abandon will ensure that the underlying async reader is shut down. // StopBuffering will ensure that the underlying async reader is shut
// It will NOT close the input supplied on New. // down so no more is read from the input.
func (a *AsyncReader) Abandon() { //
// This does not free the memory so Abandon() or Close() need to be
// called on the input.
//
// This does not wait for Read/WriteTo to complete so can be called
// concurrently to those.
func (a *AsyncReader) StopBuffering() {
select { select {
case <-a.exit: case <-a.exit:
// Do nothing if reader routine already exited // Do nothing if reader routine already exited
@ -280,6 +286,14 @@ func (a *AsyncReader) Abandon() {
// Close and wait for go routine // Close and wait for go routine
close(a.exit) close(a.exit)
<-a.exited <-a.exited
}
// Abandon will ensure that the underlying async reader is shut down
// and memory is returned. It does everything but close the input.
//
// It will NOT close the input supplied on New.
func (a *AsyncReader) Abandon() {
a.StopBuffering()
// take the lock to wait for Read/WriteTo to complete // take the lock to wait for Read/WriteTo to complete
a.mu.Lock() a.mu.Lock()
defer a.mu.Unlock() defer a.mu.Unlock()