Skip to content

Commit

Permalink
routing/http: feat: add streaming support
Browse files Browse the repository at this point in the history
This adds streaming support to the routing/v1 client and server by
changing the interfaces to use iterators instead of slices, and adding
content type negotation to the client and server.
  • Loading branch information
guseggert committed Jan 16, 2023
1 parent 9bec693 commit b24943a
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 207 deletions.
102 changes: 83 additions & 19 deletions routing/http/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"encoding/json"
"errors"
"fmt"
"mime"
"net/http"
"strings"
"time"

"github.com/benbjohnson/clock"
Expand All @@ -16,21 +18,34 @@ import (
"github.com/ipfs/go-libipfs/routing/http/internal/drjson"
"github.com/ipfs/go-libipfs/routing/http/server"
"github.com/ipfs/go-libipfs/routing/http/types"
"github.com/ipfs/go-libipfs/routing/http/types/iter"
jsontypes "github.com/ipfs/go-libipfs/routing/http/types/json"
"github.com/ipfs/go-libipfs/routing/http/types/ndjson"
logging "github.com/ipfs/go-log/v2"
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)

var logger = logging.Logger("service/delegatedrouting")
var (
_ contentrouter.Client = &client{}
logger = logging.Logger("service/delegatedrouting")
)

const (
mediaTypeJSON = "application/json"
mediaTypeNDJSON = "application/x-ndjson"
)

type client struct {
baseURL string
httpClient httpClient
validator record.Validator
clock clock.Clock

accepts string

peerID peer.ID
addrs []types.Multiaddr
identity crypto.PrivKey
Expand All @@ -40,8 +55,6 @@ type client struct {
afterSignCallback func(req *types.WriteBitswapProviderRecord)
}

var _ contentrouter.Client = &client{}

type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
Expand Down Expand Up @@ -69,6 +82,12 @@ func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr) option {
}
}

func WithStreamResultsRequired() option {
return func(c *client) {
c.accepts = mediaTypeNDJSON
}
}

// New creates a content routing API client.
// The Provider and identity parameters are option. If they are nil, the `Provide` method will not function.
func New(baseURL string, opts ...option) (*client, error) {
Expand All @@ -83,6 +102,7 @@ func New(baseURL string, opts ...option) (*client, error) {
httpClient: defaultHTTPClient,
validator: ipns.Validator{},
clock: clock.New(),
accepts: strings.Join([]string{mediaTypeNDJSON, mediaTypeJSON}, ","),
}

for _, opt := range opts {
Expand All @@ -96,43 +116,87 @@ func New(baseURL string, opts ...option) (*client, error) {
return client, nil
}

func (c *client) FindProviders(ctx context.Context, key cid.Cid) (provs []types.ProviderResponse, err error) {
measurement := newMeasurement("FindProviders")
defer func() {
measurement.length = len(provs)
measurement.record(ctx)
}()
// measuringIter measures the length of the iter and then publishes metrics about the whole req once the iter is closed.
// Of course, if the caller forgets to close the iter, this won't publish anything.
type measuringIter[T any] struct {
iter.Iter[T]
ctx context.Context
m *measurement
}

func (c *measuringIter[T]) Next() (T, bool, error) {
c.m.length++
return c.Iter.Next()
}

func (c *measuringIter[T]) Close() error {
c.m.record(c.ctx)
return c.Iter.Close()
}

func (c *client) FindProviders(ctx context.Context, key cid.Cid) (provs iter.Iter[types.ProviderResponse], err error) {
// TODO test measurements
m := newMeasurement("FindProviders")

url := c.baseURL + server.ProvidePath + key.String()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
measurement.host = req.Host

m.host = req.Host

start := c.clock.Now()
resp, err := c.httpClient.Do(req)

measurement.err = err
measurement.latency = c.clock.Since(start)
m.err = err
m.latency = c.clock.Since(start)

if err != nil {
m.record(ctx)
return nil, err
}
defer resp.Body.Close()

measurement.statusCode = resp.StatusCode
m.statusCode = resp.StatusCode
if resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
m.record(ctx)
return nil, nil
}

if resp.StatusCode != http.StatusOK {
resp.Body.Close()
m.record(ctx)
return nil, httpError(resp.StatusCode, resp.Body)
}

parsedResp := &types.ReadProvidersResponse{}
err = json.NewDecoder(resp.Body).Decode(parsedResp)
return parsedResp.Providers, err
respContentType := resp.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(respContentType)
if err != nil {
resp.Body.Close()
m.err = err
m.record(ctx)
return nil, fmt.Errorf("parsing Content-Type: %w", err)
}

m.mediaType = mediaType

var it iter.Iter[types.ProviderResponse]
switch mediaType {
case mediaTypeJSON:
defer resp.Body.Close()
parsedResp := &jsontypes.ReadProvidersResponse{}
err = json.NewDecoder(resp.Body).Decode(parsedResp)
it = iter.FromSlice(parsedResp.Providers)
case mediaTypeNDJSON:
it = ndjson.NewReadProvidersResponseIter(resp.Body)
default:
defer resp.Body.Close()
logger.Errorw("unknown media type", "MediaType", mediaType, "ContentType", respContentType)
return nil, errors.New("unknown content type")
}

return &measuringIter[types.ProviderResponse]{Iter: it, ctx: ctx, m: m}, nil
}

func (c *client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Duration) (time.Duration, error) {
Expand Down Expand Up @@ -180,7 +244,7 @@ func (c *client) ProvideBitswap(ctx context.Context, keys []cid.Cid, ttl time.Du

// ProvideAsync makes a provide request to a delegated router
func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *types.WriteBitswapProviderRecord) (time.Duration, error) {
req := types.WriteProvidersRequest{Providers: []types.WriteProviderRecord{bswp}}
req := jsontypes.WriteProvidersRequest{Providers: []types.WriteProviderRecord{bswp}}

url := c.baseURL + server.ProvidePath

Expand All @@ -203,7 +267,7 @@ func (c *client) provideSignedBitswapRecord(ctx context.Context, bswp *types.Wri
if resp.StatusCode != http.StatusOK {
return 0, httpError(resp.StatusCode, resp.Body)
}
var provideResult types.WriteProvidersResponse
var provideResult jsontypes.WriteProvidersResponse
err = json.NewDecoder(resp.Body).Decode(&provideResult)
if err != nil {
return 0, err
Expand Down
18 changes: 13 additions & 5 deletions routing/http/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-libipfs/routing/http/server"
"github.com/ipfs/go-libipfs/routing/http/types"
"github.com/ipfs/go-libipfs/routing/http/types/iter"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
Expand All @@ -25,9 +26,9 @@ import (

type mockContentRouter struct{ mock.Mock }

func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid) ([]types.ProviderResponse, error) {
func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid) (iter.Iter[types.ProviderResponse], error) {
args := m.Called(ctx, key)
return args.Get(0).([]types.ProviderResponse), args.Error(1)
return args.Get(0).(iter.Iter[types.ProviderResponse]), args.Error(1)
}
func (m *mockContentRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) {
args := m.Called(ctx, req)
Expand Down Expand Up @@ -164,6 +165,9 @@ func TestClient_FindProviders(t *testing.T) {
client := deps.client
router := deps.router

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

if c.httpStatusCode != 0 {
deps.server.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(c.httpStatusCode)
Expand All @@ -175,10 +179,12 @@ func TestClient_FindProviders(t *testing.T) {
}
cid := makeCID()

findProvsIter := iter.FromSlice(c.routerProvs)

router.On("FindProviders", mock.Anything, cid).
Return(c.routerProvs, c.routerErr)
Return(findProvsIter, c.routerErr)

provs, err := client.FindProviders(context.Background(), cid)
provsIter, err := client.FindProviders(ctx, cid)

var errList []string
if runtime.GOOS == "windows" && len(c.expWinErrContains) != 0 {
Expand All @@ -194,6 +200,9 @@ func TestClient_FindProviders(t *testing.T) {
require.NoError(t, err)
}

provs, err := iter.ReadAll(provsIter)
require.NoError(t, err)

assert.Equal(t, c.expProvs, provs)
})
}
Expand Down Expand Up @@ -258,7 +267,6 @@ func TestClient_Provide(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// deps := makeTestDeps(t)
deps := makeTestDeps(t)
client := deps.client
router := deps.router
Expand Down
34 changes: 13 additions & 21 deletions routing/http/client/measures.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
keyHost = tag.MustNewKey("host")
keyStatusCode = tag.MustNewKey("code")
keyError = tag.MustNewKey("error")
keyMediaType = tag.MustNewKey("mediatype")

ViewLatency = &view.View{
Measure: measureLatency,
Expand All @@ -42,6 +43,7 @@ var (
)

type measurement struct {
mediaType string
operation string
err error
latency time.Duration
Expand All @@ -51,32 +53,22 @@ type measurement struct {
}

func (m measurement) record(ctx context.Context) {
stats.RecordWithTags(
ctx,
[]tag.Mutator{
tag.Upsert(keyHost, m.host),
tag.Upsert(keyOperation, m.operation),
tag.Upsert(keyStatusCode, strconv.Itoa(m.statusCode)),
tag.Upsert(keyError, metricsErrStr(m.err)),
},
measureLatency.M(m.latency.Milliseconds()),
)
if m.err == nil {
stats.RecordWithTags(
ctx,
[]tag.Mutator{
tag.Upsert(keyHost, m.host),
tag.Upsert(keyOperation, m.operation),
},
measureLength.M(int64(m.length)),
)
muts := []tag.Mutator{
tag.Upsert(keyHost, m.host),
tag.Upsert(keyOperation, m.operation),
tag.Upsert(keyStatusCode, strconv.Itoa(m.statusCode)),
tag.Upsert(keyError, metricsErrStr(m.err)),
tag.Upsert(keyMediaType, m.mediaType),
}
stats.RecordWithTags(ctx, muts, measureLatency.M(m.latency.Milliseconds()))
stats.RecordWithTags(ctx, muts, measureLength.M(int64(m.length)))
}

func newMeasurement(operation string) measurement {
return measurement{
func newMeasurement(operation string) *measurement {
return &measurement{
operation: operation,
host: "None",
mediaType: "None",
}
}

Expand Down
Loading

0 comments on commit b24943a

Please sign in to comment.