Skip to content
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

fix(gateway): ensure ipfs_http_gw_get_duration_seconds gets updated #167

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions gateway/handler_unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func (i *handler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *htt
// Handling Unixfs file
if f, ok := dr.(files.File); ok {
logger.Debugw("serving unixfs file", "path", contentPath)
i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin)
return false
return i.serveFile(ctx, w, r, resolvedPath, contentPath, f, begin)
}

// Handling Unixfs directory
Expand All @@ -41,6 +40,5 @@ func (i *handler) serveUnixFS(ctx context.Context, w http.ResponseWriter, r *htt
}

logger.Debugw("serving unixfs directory", "path", contentPath)
i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger)
return true
return i.serveDirectory(ctx, w, r, resolvedPath, contentPath, dir, begin, logger)
}
26 changes: 13 additions & 13 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
// serveDirectory returns the best representation of UnixFS directory
//
// It will return index.html if present, or generate directory listing otherwise.
func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) {
func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, dir files.Directory, begin time.Time, logger *zap.SugaredLogger) bool {
ctx, span := spanTrace(ctx, "ServeDirectory", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()

Expand All @@ -35,7 +35,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
requestURI, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
webError(w, "failed to parse request path", err, http.StatusInternalServerError)
return
return false
}
originalURLPath := requestURI.Path

Expand All @@ -54,7 +54,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
redirectURL := originalURLPath + suffix
logger.Debugw("directory location moved permanently", "status", http.StatusMovedPermanently)
http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
return
return true
}
}

Expand All @@ -66,24 +66,23 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
idx, err := i.api.GetUnixFsNode(ctx, idxResolvedPath)
if err != nil {
internalWebError(w, err)
return
return false
}

f, ok := idx.(files.File)
if !ok {
internalWebError(w, files.ErrNotReader)
return
return false
}

logger.Debugw("serving index.html file", "path", idxPath)
// write to request
i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin)
return
return i.serveFile(ctx, w, r, resolvedPath, idxPath, f, begin)
case resolver.ErrNoLink:
logger.Debugw("no index.html; noop", "path", idxPath)
default:
internalWebError(w, err)
return
return false
}

// See statusResponseWriter.WriteHeader
Expand All @@ -93,7 +92,7 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
if w.Header().Get("Location") != "" {
logger.Debugw("location moved permanently", "status", http.StatusMovedPermanently)
w.WriteHeader(http.StatusMovedPermanently)
return
return true
}

// A HTML directory index will be presented, be sure to set the correct
Expand All @@ -106,20 +105,20 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *

if r.Method == http.MethodHead {
logger.Debug("return as request's HTTP method is HEAD")
return
return true
}

results, err := i.api.LsUnixFsDir(ctx, resolvedPath)
if err != nil {
internalWebError(w, err)
return
return false
}

dirListing := make([]assets.DirectoryItem, 0, len(results))
for link := range results {
if link.Err != nil {
internalWebError(w, link.Err)
return
return false
}

hash := link.Cid.String()
Expand Down Expand Up @@ -195,11 +194,12 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *

if err := assets.DirectoryTemplate.Execute(w, tplData); err != nil {
internalWebError(w, err)
return
return false
}

// Update metrics
i.unixfsGenDirGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
return true
}

func getDirListingEtag(dirCid cid.Cid) string {
Expand Down
12 changes: 7 additions & 5 deletions gateway/handler_unixfs_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// serveFile returns data behind a file along with HTTP headers based on
// the file itself, its CID and the contentPath used for accessing it.
func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) {
func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.Request, resolvedPath ipath.Resolved, contentPath ipath.Path, file files.File, begin time.Time) bool {
_, span := spanTrace(ctx, "ServeFile", trace.WithAttributes(attribute.String("path", resolvedPath.String())))
defer span.End()

Expand All @@ -33,7 +33,7 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.
size, err := file.Size()
if err != nil {
http.Error(w, "cannot serve files with unknown sizes", http.StatusBadGateway)
return
return false
}

if size == 0 {
Expand All @@ -42,7 +42,7 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.
// TODO: remove this if clause once https://github.com/golang/go/issues/54794 is fixed in two latest releases of go
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
return
return true
}

// Lazy seeker enables efficient range-requests and HTTP HEAD responses
Expand All @@ -66,14 +66,14 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.
mimeType, err := mimetype.DetectReader(content)
if err != nil {
http.Error(w, fmt.Sprintf("cannot detect content-type: %s", err.Error()), http.StatusInternalServerError)
return
return false
}

ctype = mimeType.String()
_, err = content.Seek(0, io.SeekStart)
if err != nil {
http.Error(w, "seeker can't seek", http.StatusInternalServerError)
return
return false
}
}
// Strip the encoding from the HTML Content-Type header and let the
Expand All @@ -100,4 +100,6 @@ func (i *handler) serveFile(ctx context.Context, w http.ResponseWriter, r *http.
// Update metrics
i.unixfsFileGetMetric.WithLabelValues(contentPath.Namespace()).Observe(time.Since(begin).Seconds())
}

return dataSent
}