-
Notifications
You must be signed in to change notification settings - Fork 712
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
Helper for reading & writing from binary #1600
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13269e8
Helper for reading & writing from binary
jml 81b05a3
Make ReadBinary more general and re-use in router
jml e541734
Review comments
jml 8bd8f88
Restore debugging logic
jml ce5c933
Remove unused import
jml 23faf58
Drop gob support
jml 40cbf11
Nice error on unsupported content type
jml 9e0b278
Delete test for unsupported functionality
jml 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
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,77 @@ | ||
package report | ||
|
||
import ( | ||
"compress/gzip" | ||
"io" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/ugorji/go/codec" | ||
) | ||
|
||
// WriteBinary writes a Report as a gzipped msgpack. | ||
func (rep Report) WriteBinary(w io.Writer) error { | ||
gzwriter, err := gzip.NewWriterLevel(w, gzip.BestCompression) | ||
if err != nil { | ||
return err | ||
} | ||
if err = codec.NewEncoder(gzwriter, &codec.MsgpackHandle{}).Encode(&rep); err != nil { | ||
return err | ||
} | ||
gzwriter.Close() // otherwise the content won't get flushed to the output stream | ||
return nil | ||
} | ||
|
||
type byteCounter struct { | ||
next io.Reader | ||
count *uint64 | ||
} | ||
|
||
func (c byteCounter) Read(p []byte) (n int, err error) { | ||
n, err = c.next.Read(p) | ||
*c.count += uint64(n) | ||
return n, err | ||
} | ||
|
||
// ReadBinary reads bytes into a Report. | ||
// | ||
// Will decompress the binary if gzipped is true, and will use the given | ||
// codecHandle to decode it. | ||
func (rep *Report) ReadBinary(r io.Reader, gzipped bool, codecHandle codec.Handle) error { | ||
var err error | ||
var compressedSize, uncompressedSize uint64 | ||
|
||
// We have historically had trouble with reports being too large. We are | ||
// keeping this instrumentation around to help us implement | ||
// weaveworks/scope#985. | ||
if log.GetLevel() == log.DebugLevel { | ||
r = byteCounter{next: r, count: &compressedSize} | ||
} | ||
if gzipped { | ||
r, err = gzip.NewReader(r) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if log.GetLevel() == log.DebugLevel { | ||
r = byteCounter{next: r, count: &uncompressedSize} | ||
} | ||
if err := codec.NewDecoder(r, codecHandle).Decode(&rep); err != nil { | ||
return err | ||
} | ||
log.Debugf( | ||
"Received report sizes: compressed %d bytes, uncompressed %d bytes (%.2f%%)", | ||
compressedSize, | ||
uncompressedSize, | ||
float32(compressedSize)/float32(uncompressedSize)*100, | ||
) | ||
return nil | ||
} | ||
|
||
// MakeFromBinary constructs a Report from a gzipped msgpack. | ||
func MakeFromBinary(r io.Reader) (*Report, error) { | ||
rep := MakeReport() | ||
if err := rep.ReadBinary(r, true, &codec.MsgpackHandle{}); err != nil { | ||
return nil, err | ||
} | ||
return &rep, nil | ||
} |
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.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.