pricer

package
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultUsageTailBytes = 16384

DefaultUsageTailBytes is the default cap for the trailing response body bytes captured for usage reports.

View Source
const MaxUsageTailBytes = 1 << 20

MaxUsageTailBytes is the upper bound accepted for UsageTailBytes, guarding against an unbounded per-request response capture.

Variables

This section is empty.

Functions

func SerializeRequest added in v0.6.0

func SerializeRequest(r *http.Request) (string, error)

SerializeRequest serializes a request into the text form the pricer RPCs carry, leaving the request itself forwardable. Session settlement needs the request back once the response has completed, and it must be captured in exactly the shape the pricer already parses, so the serializer is shared rather than reimplemented at the call site.

Types

type AuthorizeResult added in v0.6.0

type AuthorizeResult struct {
	// Allowed is whether the request may proceed to the backend.
	Allowed bool

	// PriceSats is the price in satoshis for the fresh challenge that
	// should be minted when the request is not allowed. A value of zero
	// means the price should be looked up through GetPrice instead.
	PriceSats int64

	// Reason optionally describes why the request was denied.
	Reason string

	// ReservedEstimate is the per-request token estimate the pricer
	// reserved against the token's balance when allowing the request.
	// It is echoed back in the matching usage report so the pricer can
	// release the exact reservation it took.
	ReservedEstimate int64

	// RetryLater is set when a denied token's balance is reserved by
	// requests in flight, so the client should retry rather than pay.
	RetryLater bool

	// SettleEstimate is the token count the pricer debits when the
	// response ends without a usage object. It is echoed back in the
	// matching usage report.
	SettleEstimate int64
}

AuthorizeResult is the pricer's decision for an authenticated request to a metered service.

type Config

type Config struct {
	// Enabled indicates if the grpcPricer is to be used.
	Enabled bool `long:"enabled" description:"Set to true if a gRPC server is available to query for price data"`

	// GRPCAddress is the address that the pricer gRPC server is serving on.
	GRPCAddress string `long:"grpcaddress" description:"gRPC addr to use for price info for service resources"`

	// Insecure indicates if the connection to the gRPC server should use
	// TLS encryption or not.
	Insecure bool `long:"insecure" description:"Set to true if no TLS encryption is to be used"`

	// TLSCertPath is the path the the tls cert used by the price server.
	TLSCertPath string `long:"tlscertpath" description:"Path to the servers tls cert"`

	// Metered indicates that the pricer should also be consulted on every
	// authenticated request (AuthorizeRequest) and receive response usage
	// reports (ReportUsage), enabling draw-down of prepaid token balances.
	// A metered price server must implement the full pricesrpc.Prices
	// service, including ChallengeMinted, AuthorizeRequest and
	// ReportUsage.
	Metered bool `` /* 130-byte string literal not displayed */

	// UsageTailBytes is the maximum number of trailing response body bytes
	// captured for usage reports. Defaults to 16384 if unset.
	UsageTailBytes int `long:"usagetailbytes" description:"Maximum number of trailing response body bytes captured for usage reports"`
}

Config holds all the config values required to initialise the GRPCPricer.

func (*Config) Validate added in v0.6.0

func (c *Config) Validate() error

Validate checks the pricer configuration for consistency. It is safe to call on a disabled pricer, so a metered-but-disabled misconfiguration is caught.

type DefaultPricer

type DefaultPricer struct {
	Price int64
}

DefaultPricer provides the same price for any service path. It implements the Pricer interface.

func NewDefaultPricer

func NewDefaultPricer(price int64) *DefaultPricer

NewDefaultPricer initialises a new DefaultPricer provider where each resource for the service will have the same price.

func (*DefaultPricer) Close

func (d *DefaultPricer) Close() error

Close is part of the Pricer interface. For the DefaultPricer, the method does nothing.

func (*DefaultPricer) GetPrice

func (d *DefaultPricer) GetPrice(_ context.Context,
	_ *http.Request) (int64, error)

GetPrice returns the price charged for all resources of a service. It is part of the Pricer interface.

type GRPCPricer

type GRPCPricer struct {
	// contains filtered or unexported fields
}

GRPCPricer uses the pricesrpc PricesClient to query a backend server for the price of a service resource given the resource path. It implements the Pricer interface.

func NewGRPCPricer

func NewGRPCPricer(cfg *Config) (*GRPCPricer, error)

NewGRPCPricer initialises a Pricer backed by a gRPC backend server.

func (GRPCPricer) AuthorizeRequest added in v0.6.0

func (c GRPCPricer) AuthorizeRequest(ctx context.Context, r *http.Request,
	tokenID string, serviceName string) (*AuthorizeResult, error)

AuthorizeRequest asks the price server whether an authenticated request may proceed, based on the token's remaining balance. It is part of the MeteredPricer interface.

func (GRPCPricer) ChallengeMinted added in v0.6.0

func (c GRPCPricer) ChallengeMinted(ctx context.Context, r *http.Request,
	tokenID string, serviceName string, priceSats int64) error

ChallengeMinted notifies the price server that a fresh challenge macaroon with the given token ID was minted at the given price. It is part of the MeteredPricer interface.

func (GRPCPricer) Close

func (c GRPCPricer) Close() error

Close closes the gRPC connection. It is part of the Pricer interface.

func (GRPCPricer) GetPrice

func (c GRPCPricer) GetPrice(ctx context.Context,
	r *http.Request) (int64, error)

GetPrice queries the server for the price of a resource path and returns the price. GetPrice is part of the Pricer interface.

func (GRPCPricer) QuoteSession added in v0.6.0

func (c GRPCPricer) QuoteSession(ctx context.Context, r *http.Request,
	sessionID string, serviceName string) (*SessionQuote, error)

QuoteSession asks the price server what one request drawn against a prepaid session should cost. It is part of the SessionPricer interface.

func (GRPCPricer) ReportUsage added in v0.6.0

func (c GRPCPricer) ReportUsage(ctx context.Context, usage *Usage) error

ReportUsage reports the outcome of a proxied request so the price server can debit the token's balance. It is part of the MeteredPricer interface.

func (GRPCPricer) SettleSession added in v0.6.0

func (c GRPCPricer) SettleSession(ctx context.Context,
	usage *SessionUsage) (int64, error)

SettleSession asks the price server what a completed response actually cost, so aperture can reconcile it against the estimate it already deducted from the session balance. It is part of the SessionPricer interface.

type MeteredPricer added in v0.6.0

type MeteredPricer interface {
	Pricer

	// ChallengeMinted notifies the pricer that a fresh challenge macaroon
	// with the given token ID was minted at the given price.
	ChallengeMinted(ctx context.Context, req *http.Request, tokenID string,
		serviceName string, priceSats int64) error

	// AuthorizeRequest decides whether an authenticated request may
	// proceed, based on the token's remaining balance.
	AuthorizeRequest(ctx context.Context, req *http.Request,
		tokenID string, serviceName string) (*AuthorizeResult, error)

	// ReportUsage reports the outcome of a proxied request so the pricer
	// can debit the token's balance.
	ReportUsage(ctx context.Context, usage *Usage) error
}

MeteredPricer is an optional interface a Pricer can implement to support metered draw-down against prepaid L402 tokens. Aperture consults the metered pricer on every authenticated request so the pricer can decide when a token's purchased balance is exhausted and a fresh challenge is due, and reports response usage back so the pricer can perform the cost analysis and debit the balance.

type Pricer

type Pricer interface {
	// GetPrice should return the price in satoshis for the given
	// resource path.
	GetPrice(ctx context.Context, req *http.Request) (int64, error)

	// Close should clean up the Pricer implementation if needed.
	Close() error
}

Pricer is an interface used to query price data from a price provider.

type SessionPricer added in v0.6.0

type SessionPricer interface {
	Pricer

	// QuoteSession returns the estimated cost of serving the given request
	// against a session. The session ID is empty when the quote is for a
	// fresh challenge, which is minted before any session exists.
	QuoteSession(ctx context.Context, req *http.Request, sessionID string,
		serviceName string) (*SessionQuote, error)

	// SettleSession costs a completed response and returns what the
	// request should actually have cost in satoshis. Aperture reconciles
	// that against the estimate it already deducted.
	SettleSession(ctx context.Context, usage *SessionUsage) (int64, error)
}

SessionPricer is an optional interface a Pricer can implement to price individual requests drawn against a prepaid MPP session balance.

It is deliberately separate from MeteredPricer even though both cost the same responses, because the two answer different questions about different money. MeteredPricer sells a token bundle and holds the buyer's balance itself, so its AuthorizeRequest is an admission decision and its ReportUsage is a debit. A session's balance lives in aperture's own session store, because the deposit is a Lightning payment aperture received and will refund on close. So here the pricer only quotes and costs: aperture holds the funds and moves them.

type SessionQuote added in v0.6.0

type SessionQuote struct {
	// UnitPriceSats is the estimated cost in satoshis of serving one
	// request of this shape. It is the per-unit amount a session challenge
	// advertises and the amount deducted from the session balance before
	// the request is proxied.
	UnitPriceSats int64

	// DepositSats is the deposit in satoshis to ask for when opening a
	// fresh session. Zero leaves the deposit to aperture's configured
	// deposit multiplier.
	DepositSats int64
}

SessionQuote is the pricer's answer for a request that will be, or was, served against a prepaid MPP session balance.

type SessionUsage added in v0.6.0

type SessionUsage struct {
	// SessionID is the session the request drew against.
	SessionID string

	// Path is the URL path of the request.
	Path string

	// ServiceName is the name of the aperture service.
	ServiceName string

	// HTTPStatus is the status code the backend responded with.
	HTTPStatus int

	// ContentType is the Content-Type of the backend response.
	ContentType string

	// ContentEncoding is the Content-Encoding the backend response
	// declared. A non-identity value means the captured tail is compressed
	// and could not be costed.
	ContentEncoding string

	// Complete is whether the response body was read to completion.
	Complete bool

	// ResponseTail is a capped tail of the response body. For SSE streams
	// this contains the trailing chunks, including any final usage object.
	ResponseTail []byte

	// EstimateSats is the amount already deducted from the session balance
	// for this request, so the pricer can see what it is reconciling
	// against.
	EstimateSats int64

	// RequestText is the serialized HTTP request the response answered. A
	// session books no model the way an L402 token bundle does, so the
	// pricer needs the request back to know which model's rates to cost
	// the response at.
	RequestText string
}

SessionUsage describes a response that was served against a session balance, as reported to the pricer once the response has completed.

type Usage added in v0.6.0

type Usage struct {
	// TokenID is the hex-encoded L402 token ID the request authenticated
	// with.
	TokenID string

	// Path is the URL path of the request.
	Path string

	// ServiceName is the name of the aperture service.
	ServiceName string

	// HTTPStatus is the status code the backend responded with.
	HTTPStatus int

	// ContentType is the Content-Type of the backend response.
	ContentType string

	// ContentEncoding is the Content-Encoding the backend response
	// declared. Metered requests strip the client's Accept-Encoding so the
	// observed tail is plaintext; a non-identity value here signals that
	// the observer captured bytes it could not decode.
	ContentEncoding string

	// Complete is whether the response body was read to completion.
	Complete bool

	// ResponseTail is a capped tail of the response body. For SSE streams
	// this contains the trailing chunks, including any final usage object.
	ResponseTail []byte

	// ReservedEstimate is the ReservedEstimate value of the authorization
	// that admitted this request, echoed back verbatim so the pricer can
	// release the exact reservation it took. Zero when the authorization
	// carried none.
	ReservedEstimate int64

	// SettleEstimate is the SettleEstimate value of the authorization that
	// admitted this request, echoed back verbatim. Zero when the
	// authorization carried none.
	SettleEstimate int64
}

Usage describes the outcome of a proxied request to a metered service, as reported to the pricer once the response has completed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL