proxy+pricer: pass in the entire HTTP requests for GetPrice

In this commit, we modify the `GetPrice` method and interface to accept
the full request instead of _just_ the path. For backwards compat, we
leave the path in place, but also include the full serialized HTTP
request.
This commit is contained in:
Olaoluwa Osuntokun
2023-06-06 18:50:57 -07:00
parent 3a5dd96a95
commit 40df8f8339
4 changed files with 25 additions and 11 deletions

View File

@@ -1,6 +1,9 @@
package pricer
import "context"
import (
"context"
"net/http"
)
// DefaultPricer provides the same price for any service path. It implements
// the Pricer interface.
@@ -16,8 +19,8 @@ func NewDefaultPricer(price int64) *DefaultPricer {
// GetPrice returns the price charged for all resources of a service.
// It is part of the Pricer interface.
func (d *DefaultPricer) GetPrice(_ context.Context, _ string) (int64,
error) {
func (d *DefaultPricer) GetPrice(_ context.Context,
_ *http.Request) (int64, error) {
return d.Price, nil
}

View File

@@ -1,8 +1,10 @@
package pricer
import (
"bytes"
"context"
"fmt"
"net/http"
"github.com/lightninglabs/aperture/pricesrpc"
"google.golang.org/grpc"
@@ -68,9 +70,17 @@ func NewGRPCPricer(cfg *Config) (*GRPCPricer, error) {
// GetPrice queries the server for the price of a resource path and returns the
// price. GetPrice is part of the Pricer interface.
func (c GRPCPricer) GetPrice(ctx context.Context, path string) (int64, error) {
func (c GRPCPricer) GetPrice(ctx context.Context,
r *http.Request) (int64, error) {
var b bytes.Buffer
if err := r.Write(&b); err != nil {
return 0, nil
}
resp, err := c.rpcClient.GetPrice(ctx, &pricesrpc.GetPriceRequest{
Path: path,
Path: r.URL.Path,
HttpRequestText: b.String(),
})
if err != nil {
return 0, err

View File

@@ -1,12 +1,15 @@
package pricer
import "context"
import (
"context"
"net/http"
)
// Pricer is an interface used to query price data from a price provider.
type Pricer interface {
// GetPrice should return the price in satoshis for the given
// resource path.
GetPrice(ctx context.Context, path string) (int64, error)
GetPrice(ctx context.Context, req *http.Request) (int64, error)
// Close should clean up the Pricer implementation if needed.
Close() error

View File

@@ -155,9 +155,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// resources.
acceptAuth := p.authenticator.Accept(&r.Header, resourceName)
if !acceptAuth {
price, err := target.pricer.GetPrice(
r.Context(), r.URL.Path,
)
price, err := target.pricer.GetPrice(r.Context(), r)
if err != nil {
prefixLog.Errorf("error getting "+
"resource price: %v", err)
@@ -197,7 +195,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if !ok {
price, err := target.pricer.GetPrice(
r.Context(), r.URL.Path,
r.Context(), r,
)
if err != nil {
prefixLog.Errorf("error getting "+