protocol

package
v0.1.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 16 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 protocol owns the cross-node chat contract: strict JSON over the Raminga HTTP-over-libp2p transport. The authenticated stream Peer ID is the only sender identity; payload sender fields are accepted but ignored.

Index

Constants

View Source
const (
	// HTTPProtocol is the libp2p protocol carrying net/http streams.
	HTTPProtocol = "/raminga/http/1.0.0"
	// ChatPath is the chat route on HTTPProtocol.
	ChatPath = "/chat"

	// DefaultMaxBodyBytes bounds one JSON request body. It is deliberately
	// smaller than the generic p2phttp 1 MiB default.
	DefaultMaxBodyBytes = 64 << 10
	// DefaultMaxChatTextBytes bounds UTF-8 chat text.
	DefaultMaxChatTextBytes = 4096
	// DefaultDedupeTTL is how long an accepted message ID is remembered.
	DefaultDedupeTTL = 10 * time.Minute
	// DefaultDedupeSize bounds the dedupe cache.
	DefaultDedupeSize = 4096
	// DefaultSendTimeout bounds one HTTP request/response.
	DefaultSendTimeout = 20 * time.Second
	// DefaultRatePerSecond is the per-peer inbound chat rate.
	DefaultRatePerSecond = 5
	// DefaultRateBurst is the per-peer inbound chat burst.
	DefaultRateBurst = 10
	// DefaultRateTrackedPeers bounds the per-peer limiter cache.
	DefaultRateTrackedPeers = 1024
	// MaxMessageIDBytes bounds the client-supplied message ID.
	MaxMessageIDBytes = 128
	// MaxResponseBytes bounds an ACK response.
	MaxResponseBytes = 16 << 10
)

Variables

View Source
var ErrTooLarge = errors.New("protocol: request body exceeds configured maximum size")

ErrTooLarge reports a request body larger than the configured limit.

Functions

func NewMessageID

func NewMessageID() string

NewMessageID returns a random 128-bit hex identifier.

func ValidateMessage

func ValidateMessage(m *Message, maxTextBytes int) error

Types

type Ack

type Ack struct {
	MessageID     string `json:"message_id"`
	AckID         string `json:"ack_id"`
	Accepted      bool   `json:"accepted"`
	Error         string `json:"error,omitempty"`
	AckedAtUnixMs int64  `json:"acked_at_unix_ms"`
}

Ack is the JSON response for a durably stored (or deduplicated) message.

func DecodeAck

func DecodeAck(data []byte, wantMessageID string) (*Ack, error)

DecodeAck strictly decodes and validates an ACK response.

type Handler

type Handler func(remote peer.ID, msg *Message) (accepted bool, reason string, status int)

Handler processes an inbound message. remote is authenticated by the transport. status is 200 for accepted, or an HTTP error status.

type Message

type Message struct {
	MessageID    string `json:"message_id"`
	SenderPeerID string `json:"sender_peer_id,omitempty"`
	SenderNodeID string `json:"sender_node_id,omitempty"`
	Text         string `json:"text"`
	SentAtUnixMs int64  `json:"sent_at_unix_ms"`
}

Message is the JSON request body. SenderPeerID/SenderNodeID are accepted for old-payload compatibility but are never used as identity.

func DecodeMessage

func DecodeMessage(data []byte, maxTextBytes int) (*Message, error)

DecodeMessage strictly decodes one JSON request. Unknown fields, trailing JSON values, invalid UTF-8 and oversized text are rejected.

type Options

type Options struct {
	MaxBodyBytes     int
	MaxChatTextBytes int
	DedupeTTL        time.Duration
	DedupeSize       int
	RatePerSecond    float64
	RateBurst        int
	RateTrackedPeers int
	Handler          Handler
	Logger           *slog.Logger
}

type Service

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

func New

func New(opts Options) (*Service, error)

func (*Service) HandleHTTP

func (s *Service) HandleHTTP(w http.ResponseWriter, r *http.Request, remote peer.ID)

type StatusError

type StatusError struct {
	Status int
	Reason string
}

StatusError reports a non-200 HTTP chat response. It is never delivered.

func (*StatusError) Error

func (e *StatusError) Error() string

func (*StatusError) Permanent

func (e *StatusError) Permanent() bool

Permanent reports whether retrying this status cannot help.

Jump to

Keyboard shortcuts

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