p2phttp

package
v0.1.0-rc.4 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 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

Overview

Package p2phttp implements Raminga HTTP over libp2p streams using net/http and gostream. It deliberately does not use the experimental p2p/http package.

Index

Constants

View Source
const (
	// ProtocolID is the single Raminga HTTP-over-libp2p protocol.
	ProtocolID = protocol.ID("/raminga/http/1.0.0")

	DefaultReadHeaderTimeout = 10 * time.Second
	DefaultReadTimeout       = 30 * time.Second
	DefaultIdleTimeout       = 60 * time.Second
	DefaultMaxHeaderBytes    = 16 << 10
	DefaultMaxBodyBytes      = 1 << 20
	DefaultPerPeerRate       = 5
	DefaultPerPeerBurst      = 10
	DefaultMaxTrackedPeers   = 1024
	DefaultPerPeerConcurrent = 4
	DefaultGlobalConcurrent  = 64
)

Variables

View Source
var (
	ErrResponseTooLarge = errors.New("p2phttp: response body exceeds limit")
	ErrNoDialFunc       = errors.New("p2phttp: no dial helper configured")
)

Functions

func IsProtocolUnsupported

func IsProtocolUnsupported(err error) bool

IsProtocolUnsupported detects multistream.ErrNotSupported through the net/http transport's *url.Error wrapping instead of matching error strings.

func PeerID

func PeerID(ctx context.Context) (peer.ID, bool)

PeerID returns the authenticated caller Peer ID from the request context.

func ProtocolMismatch

func ProtocolMismatch(err error, proto protocol.ID) error

ProtocolMismatch converts an unsupported-protocol dial error into the user-facing version-incompatibility message and leaves other errors alone.

func WithPeerID

func WithPeerID(ctx context.Context, pid peer.ID) context.Context

WithPeerID stores the authenticated stream Peer ID in a request context.

Types

type Client

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

func NewClient

func NewClient(h host.Host, opts ClientOptions) (*Client, error)

func (*Client) Close

func (c *Client) Close()

func (*Client) Dial

func (c *Client) Dial(ctx context.Context, pid peer.ID) error

func (*Client) Do

func (c *Client) Do(ctx context.Context, pid peer.ID, req *http.Request, maxBody int64) (*Result, error)

func (*Client) DoWithDial

func (c *Client) DoWithDial(ctx context.Context, pid peer.ID, req *http.Request, maxBody int64) (*Result, error)

func (*Client) SetDial

func (c *Client) SetDial(dial func(context.Context, peer.ID) error)

SetDial installs the address-fallback helper used by DoWithDial.

type ClientOptions

type ClientOptions struct {
	MaxResponseHeaderBytes int64
	DefaultBodyLimit       int64
	Dial                   func(context.Context, peer.ID) error
}

func DefaultClientOptions

func DefaultClientOptions() ClientOptions

type Options

type Options struct {
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	IdleTimeout       time.Duration
	MaxHeaderBytes    int
	MaxBodyBytes      int64
	PerPeerRate       float64
	PerPeerBurst      int
	MaxTrackedPeers   int
	PerPeerConcurrent int
	GlobalConcurrent  int
	// DisableRateLimit lets a route (e.g. chat with dedupe-before-rate) own
	// its per-peer budget instead of applying the generic HTTP limiter.
	DisableRateLimit bool
	// RateLimitNow supplies the clock for per-peer token-bucket decisions.
	// Nil uses time.Now. Tests set it to a frozen or manually advanced clock
	// so the production rate and burst stay covered without depending on how
	// long a request takes on a loaded machine.
	RateLimitNow func() time.Time
}

Options configures the server. Per-route long downloads should use http.NewResponseController to set their own write deadline; the server leaves WriteTimeout at zero so a global write timeout cannot kill downloads.

func DefaultOptions

func DefaultOptions() Options

type ProtocolUnsupportedError

type ProtocolUnsupportedError struct {
	Protocol protocol.ID
	Err      error
}

ProtocolUnsupportedError reports that the remote multistream muxer does not offer the Raminga HTTP protocol. Wrapping keeps the original dial error available through errors.Is/errors.As.

func (*ProtocolUnsupportedError) Error

func (e *ProtocolUnsupportedError) Error() string

func (*ProtocolUnsupportedError) Unwrap

func (e *ProtocolUnsupportedError) Unwrap() error

type Result

type Result struct {
	Response *http.Response
	Limited  bool
}

type Server

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

func NewServer

func NewServer(h host.Host, handler http.Handler, opts Options) (*Server, error)

func (*Server) Close

func (s *Server) Close(ctx context.Context) error

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the wrapped handler for tests/diagnostics.

func (*Server) RateLimit

func (s *Server) RateLimit(next http.Handler) http.Handler

RateLimit wraps one route with the server's per-peer token bucket. The server wrapper already applies the per-peer/global concurrency limits.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Jump to

Keyboard shortcuts

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