Skip to content

Commit

Permalink
Improve debug
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 23, 2023
1 parent 9952980 commit 973d3d4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
36 changes: 21 additions & 15 deletions http-range.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -77,20 +78,6 @@ func remoteHTTPFileAsIoReaderAt(ctx context.Context, url string) (ReaderAtCloser
rc.StartCacheGC(ctx, 1*time.Minute)
rr.ca = rc

// try prefetching the first n MiB:
{
MiB := int64(1024 * 1024)
prefetchSize := MiB
if prefetchSize > contentLength {
prefetchSize = contentLength
}
prefetchBuf := make([]byte, prefetchSize)
_, err := rr.ReadAt(prefetchBuf, 0)
if err != nil {
return nil, err
}
}

return rr, nil
}

Expand Down Expand Up @@ -188,12 +175,31 @@ func (r *readCloserWrapper) ReadAt(p []byte, off int64) (n int, err error) {
startedAt := time.Now()
defer func() {
if DebugMode {
fmt.Fprintf(os.Stderr, "read %s:%d-%d (%s)\n", filepath.Base(r.name), off, len(p), time.Since(startedAt))
prefix := "[READ-UNKNOWN]"
// if has suffix .index, then it's an index file
if strings.HasSuffix(r.name, ".index") {
prefix = azureBG("[READ-INDEX]")
}
// if has suffix .car, then it's a car file
if strings.HasSuffix(r.name, ".car") {
prefix = purpleBG("[READ-CAR]")
}
fmt.Fprintf(os.Stderr, prefix+" %s:%d-%d (%s)\n", filepath.Base(r.name), off, len(p), time.Since(startedAt))
}
}()
return r.rac.ReadAt(p, off)
}

func purpleBG(s string) string {
// blue bg, black fg
return "\033[48;5;4m\033[38;5;0m" + s + "\033[0m"
}

func azureBG(s string) string {
// azure bg, black fg
return "\033[48;5;6m\033[38;5;0m" + s + "\033[0m"
}

// when closing print a newline
func (r *readCloserWrapper) Close() error {
return r.rac.Close()
Expand Down
4 changes: 2 additions & 2 deletions range-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (rc *RangeCache) GetRange(ctx context.Context, start, ln int64) ([]byte, er
end := start + ln
got, err := rc.getRange(ctx, start, end, func() ([]byte, error) {
v := make([]byte, end-start)
debugLn(orange("cache MISS; reading from reader"), start, end, end-start)
debugLn(orange("[cache-MISS] going to read from original reader"), start, end, end-start)
_, err := rc.remoteFetcher(v, start)
if err == nil {
cloned := clone(v)
Expand Down Expand Up @@ -212,7 +212,7 @@ func (rc *RangeCache) getRangeFromCache(ctx context.Context, start, end int64) (
return nil, false, ctx.Err()
}
if r.contains(Range{start, end}) {
debugLn(lime("cache HIT for a superset of this range"), start, end, end-start)
debugLn(lime("[cache-HIT] for a superset of this range"), start, end, end-start)
return clone(rc.cache[r].Value[start-r[0] : end-r[0]]), true, nil
}
}
Expand Down

0 comments on commit 973d3d4

Please sign in to comment.