Skip to content

Commit

Permalink
Fix delegated find path (#166)
Browse files Browse the repository at this point in the history
* Fix delegated translator path
* update version
  • Loading branch information
gammazero authored Sep 20, 2023
1 parent 13bbd7d commit 03785ff
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ecr-publisher.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ on:
- 'docs/**'
branches:
- main
# See https://github.com/ipni/indexstar/pull/105
- ivan/add-sharding-key

jobs:
publisher:
if: ${{ github.event.pusher.name != 'sti-bot' }}
Expand Down
8 changes: 5 additions & 3 deletions delegated_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ func (dt *delegatedTranslator) provide(w http.ResponseWriter, r *http.Request) {

h := w.Header()
h.Add("Access-Control-Allow-Origin", "*")
h.Add("Access-Control-Allow-Methods", "GET, PUT, OPTIONS")
h.Add("Access-Control-Allow-Methods", "GET, OPTIONS")
switch r.Method {
case http.MethodOptions:
w.WriteHeader(http.StatusOK)
case http.MethodPut:
http.Error(w, "", http.StatusNotImplemented)
default:
http.Error(w, "", http.StatusNotFound)
h.Add("Allow", http.MethodGet)
h.Add("Allow", http.MethodOptions)
http.Error(w, "", http.StatusMethodNotAllowed)
}
}

Expand All @@ -80,7 +82,7 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request, encr
cidUrlParam := path.Base(r.URL.Path)

// Translate URL by mapping `/providers/{CID}` to `/cid/{CID}`.
uri := r.URL.JoinPath("../cid", cidUrlParam)
uri := r.URL.JoinPath("../../cid", cidUrlParam)
rcode, resp := dt.be(r.Context(), http.MethodGet, findMethodDelegated, uri, encrypted)
if rcode != http.StatusOK {
http.Error(w, "", rcode)
Expand Down
43 changes: 23 additions & 20 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/url"
"path"
"strings"
"sync/atomic"
"time"

Expand All @@ -32,7 +31,7 @@ func (s *server) findCid(w http.ResponseWriter, r *http.Request, encrypted bool)
case http.MethodOptions:
handleIPNIOptions(w, false)
case http.MethodGet:
sc := strings.TrimPrefix(path.Base(r.URL.Path), "cid/")
sc := path.Base(r.URL.Path)
c, err := cid.Decode(sc)
if err != nil {
http.Error(w, "invalid cid: "+err.Error(), http.StatusBadRequest)
Expand Down Expand Up @@ -139,10 +138,14 @@ func (s *server) findMetadataSubtree(w http.ResponseWriter, r *http.Request) {
}

for md := range sg.gather(ctx) {
// 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.
// It is ok to return the first encountered metadata. This is because
// metadata is uniquely identified by ValueKey (peerID + contextID).
// I.e. it is not possible to have different metadata records for the
// same ValueKey.
//
// Whereas in regular find requests it is perfectly normal to have
// different results returned by different IPNI instances and hence
// they need to be aggregated.
if len(md) > 0 {
writeJsonResponse(w, http.StatusOK, md)
return
Expand Down Expand Up @@ -182,7 +185,7 @@ func (s *server) find(w http.ResponseWriter, r *http.Request, mh multihash.Multi
}
}

func (s *server) doFind(ctx context.Context, method, source string, req *url.URL, encrypted bool) (int, []byte) {
func (s *server) doFind(ctx context.Context, method, source string, reqURL *url.URL, encrypted bool) (int, []byte) {
start := time.Now()
latencyTags := []tag.Mutator{tag.Insert(metrics.Method, method)}
loadTags := []tag.Mutator{tag.Insert(metrics.Method, source)}
Expand Down Expand Up @@ -220,7 +223,7 @@ func (s *server) doFind(ctx context.Context, method, source string, req *url.URL

// Copy the URL from original request and override host/schema to point
// to the server.
endpoint := *req
endpoint := *reqURL
endpoint.Host = b.URL().Host
endpoint.Scheme = b.URL().Scheme
log := log.With("backend", endpoint.Host)
Expand Down Expand Up @@ -299,7 +302,7 @@ outer:
} else {
if !bytes.Equal(resp.MultihashResults[0].Multihash, r.rsp.MultihashResults[0].Multihash) {
// weird / invalid.
log.Warnw("conflicting results", "q", req, "first", resp.MultihashResults[0].Multihash, "second", r.rsp.MultihashResults[0].Multihash)
log.Warnw("conflicting results", "q", reqURL, "first", resp.MultihashResults[0].Multihash, "second", r.rsp.MultihashResults[0].Multihash)
return http.StatusInternalServerError, nil
}
for _, pr := range r.rsp.MultihashResults[0].ProviderResults {
Expand All @@ -320,7 +323,7 @@ outer:
updateFoundFlags(r.bknd)
} else {
if !bytes.Equal(resp.EncryptedMultihashResults[0].Multihash, r.rsp.EncryptedMultihashResults[0].Multihash) {
log.Warnw("conflicting encrypted results", "q", req, "first", resp.EncryptedMultihashResults[0].Multihash, "second", r.rsp.EncryptedMultihashResults[0].Multihash)
log.Warnw("conflicting encrypted results", "q", reqURL, "first", resp.EncryptedMultihashResults[0].Multihash, "second", r.rsp.EncryptedMultihashResults[0].Multihash)
return http.StatusInternalServerError, nil
}
updateFoundFlags(r.bknd)
Expand All @@ -335,19 +338,19 @@ outer:
if len(resp.MultihashResults) == 0 && len(resp.EncryptedMultihashResults) == 0 {
latencyTags = append(latencyTags, tag.Insert(metrics.Found, "no"))
return http.StatusNotFound, nil
} else {
latencyTags = append(latencyTags, tag.Insert(metrics.Found, "yes"))
yesno := func(yn bool) string {
if yn {
return "yes"
}
return "no"
}
}

latencyTags = append(latencyTags, tag.Insert(metrics.FoundCaskade, yesno(foundCaskade)))
latencyTags = append(latencyTags, tag.Insert(metrics.FoundRegular, yesno(foundRegular)))
latencyTags = append(latencyTags, tag.Insert(metrics.Found, "yes"))
yesno := func(yn bool) string {
if yn {
return "yes"
}
return "no"
}

latencyTags = append(latencyTags, tag.Insert(metrics.FoundCaskade, yesno(foundCaskade)))
latencyTags = append(latencyTags, tag.Insert(metrics.FoundRegular, yesno(foundRegular)))

rs.observeFindResponse(&resp)
rs.reportMetrics(source)

Expand Down
4 changes: 2 additions & 2 deletions find_ndjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (rs *resultStats) reportMetrics(method string) {
}
}

func (s *server) doFindNDJson(ctx context.Context, w http.ResponseWriter, source string, req *url.URL, translateNonStreaming bool, mh multihash.Multihash, encrypted bool) {
func (s *server) doFindNDJson(ctx context.Context, w http.ResponseWriter, source string, reqURL *url.URL, translateNonStreaming bool, mh multihash.Multihash, encrypted bool) {
start := time.Now()
latencyTags := []tag.Mutator{tag.Insert(metrics.Method, http.MethodGet)}
loadTags := []tag.Mutator{tag.Insert(metrics.Method, source)}
Expand Down Expand Up @@ -179,7 +179,7 @@ func (s *server) doFindNDJson(ctx context.Context, w http.ResponseWriter, source

// Copy the URL from original request and override host/schema to point
// to the server.
endpoint := *req
endpoint := *reqURL
endpoint.Host = b.URL().Host
endpoint.Scheme = b.URL().Scheme
log := log.With("backend", endpoint.Host)
Expand Down
1 change: 0 additions & 1 deletion providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
"path"

//"github.com/ipni/go-libipni/find/model"
"github.com/libp2p/go-libp2p/core/peer"
)

Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.1.1"
"version": "v0.1.2"
}

0 comments on commit 03785ff

Please sign in to comment.