Skip to content

Commit

Permalink
fixup! fixup! common/util: ReadAtMost n bytes from an io.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jailuthra committed Apr 8, 2021
1 parent 5df4ed8 commit 979451e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var HTTPTimeout = 8 * time.Second
var MaxDuration = (5 * time.Minute)

// Max Segment Size in bytes (cap reading HTTP response body at this size)
var MaxSegSize = int(MaxDuration.Seconds()) * 1024 * 1024 // 1080p30fps video @ 8000Kb/s or 1MiB/s
// 1080p30fps video @ 8000kbps will have roughly 1M (1024*1024) bytes per second
var MaxSegSize = int(MaxDuration.Seconds()) * 1024 * 1024

const maxInt64 = int64(math.MaxInt64)

Expand Down Expand Up @@ -397,13 +398,13 @@ func JoinURL(url, path string) string {
}

// Read at most n bytes from an io.Reader
// error out if the io.Reader had bytes remaining
func ReadAtMost(r io.Reader, n int) ([]byte, error) {
// Reading one extra byte to check if input Reader
// had more than n bytes
limitedReader := io.LimitReader(r, int64(n)+1)
b, err := ioutil.ReadAll(limitedReader)
if err == nil && len(b) > n {
return nil, errors.New("input bigger than max buffer size")
} else {
return b, err
}
return b, err
}

0 comments on commit 979451e

Please sign in to comment.