-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added delayed reader and blob head check
- Loading branch information
Showing
9 changed files
with
80 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package regclient | ||
|
||
import "io" | ||
|
||
// delayedReader sets up a reader that only fetches a blob | ||
// upon explicit reading request, otherwise, it stores the | ||
// way of getting the reader. | ||
type delayedReader struct { | ||
open func() (io.ReadCloser, error) | ||
rc io.ReadCloser | ||
closed bool | ||
} | ||
|
||
func newDelayedReader(open func() (io.ReadCloser, error)) (io.ReadCloser, error) { | ||
return &delayedReader{ | ||
open: open, | ||
}, nil | ||
} | ||
|
||
func (d *delayedReader) Read(p []byte) (n int, err error) { | ||
if d.closed { | ||
return 0, io.EOF | ||
} | ||
|
||
reader, err := d.reader() | ||
if err != nil { | ||
return 0, err | ||
|
||
} | ||
|
||
return reader.Read(p) | ||
} | ||
|
||
func (d *delayedReader) reader() (io.ReadCloser, error) { | ||
if d.rc != nil { | ||
return d.rc, nil | ||
} | ||
|
||
rc, err := d.open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
d.rc = rc | ||
return rc, nil | ||
} | ||
|
||
func (d *delayedReader) Close() error { | ||
if d.closed { | ||
return nil | ||
} | ||
|
||
// we close regardless of an error | ||
d.closed = true | ||
return d.rc.Close() | ||
} |
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 |
---|---|---|
|
@@ -4,4 +4,4 @@ set -e | |
|
||
echo "> Generate" | ||
|
||
GO111MODULE=on go generate -v -mod=mod $@ | ||
GO111MODULE=on go generate -mod=mod $@ |