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

Start proxying metadata requests #63

Merged
merged 1 commit into from
Jan 23, 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
79 changes: 79 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,85 @@ func (s *server) findMultihashSubtree(w http.ResponseWriter, r *http.Request) {
}
}

func (s *server) findMetadataSubtree(w http.ResponseWriter, r *http.Request) {
discardBody(r)
if r.Method != http.MethodGet {
http.Error(w, "", http.StatusNotFound)
return
}

ctx := r.Context()
method := r.Method
req := r.URL

sg := &scatterGather[*url.URL, []byte]{
targets: s.servers,
tcb: s.serverCallers,
maxWait: config.Server.ResultMaxWait,
}

// TODO: wait for the first successfull response instead
ischasny marked this conversation as resolved.
Show resolved Hide resolved
if err := sg.scatter(ctx, func(cctx context.Context, b *url.URL) (*[]byte, error) {
// Copy the URL from original request and override host/schema to point
// to the server.
endpoint := *req
endpoint.Host = b.Host
endpoint.Scheme = b.Scheme
log := log.With("backend", endpoint)

req, err := http.NewRequestWithContext(cctx, method, endpoint.String(), nil)
if err != nil {
log.Warnw("Failed to construct find-metadata backend query", "err", err)
return nil, err
}
req.Header.Set("X-Forwarded-Host", req.Host)
resp, err := s.Client.Do(req)
if err != nil {
log.Warnw("Failed to query backend for metadata", "err", err)
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
log.Warnw("Failed to read find-metadata backend response", "err", err)
return nil, err
}

switch resp.StatusCode {
case http.StatusOK:
return &data, nil
case http.StatusNotFound:
return nil, nil
default:
return nil, fmt.Errorf("status %d response from backend %s", resp.StatusCode, b.String())
}
}); err != nil {
log.Errorw("Failed to scatter HTTP find metadata request", "err", err)
http.Error(w, "", http.StatusInternalServerError)
return
}

var res []byte
for md := range sg.gather(ctx) {
if len(md) == 0 {
continue
}
// It's ok to return the first encountered metadata. This is because metadata is uniquely identified
// by ValueKey (peerID + contextID). I.e. it's not possible to have different metadata records for the same ValueKey.
// In comparison to regular find requests where it's perfectly normal to have different results returned by different IPNI
// instances and hence they need to be aggregated.
res = md
// Continue to iterate to drain channels and avoid memory leak.
ischasny marked this conversation as resolved.
Show resolved Hide resolved
}

if len(res) == 0 {
http.Error(w, "", http.StatusNotFound)
return
}

httpserver.WriteJsonResponse(w, http.StatusOK, res)
}

func (s *server) find(w http.ResponseWriter, r *http.Request) {
var rb []byte
switch r.Method {
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (s *server) Serve() chan error {
mux.HandleFunc("/cid/", s.findCid)
mux.HandleFunc("/multihash", s.findMultihash)
mux.HandleFunc("/multihash/", s.findMultihashSubtree)
mux.HandleFunc("/metadata/", s.findMetadataSubtree)
mux.HandleFunc("/providers", s.providers)
mux.HandleFunc("/providers/", s.provider)
mux.HandleFunc("/health", s.health)
Expand Down