streampool

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const CName = "common.net.streampool"

Variables

This section is empty.

Functions

func CtxStreamId added in v0.13.0

func CtxStreamId(ctx context.Context) (streamId uint32, ok bool)

CtxStreamId returns the id of the stream that delivered the current message. It is set on the context passed to StreamHandler.HandleMessage, letting a handler key per-stream state (e.g. subscription interest) so that two streams from the same peer stay independent.

Types

type ExecPool added in v0.1.2

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

ExecPool needed for parallel execution of the incoming send tasks

func NewExecPool added in v0.1.2

func NewExecPool(workers, maxSize int) *ExecPool

NewExecPool creates new ExecPool workers - how many processes will execute tasks maxSize - limit for queue size

func (*ExecPool) Add added in v0.1.2

func (ss *ExecPool) Add(ctx context.Context, f ...func()) (err error)

func (*ExecPool) Close added in v0.1.2

func (ss *ExecPool) Close() (err error)

func (*ExecPool) Run added in v0.1.2

func (ss *ExecPool) Run()

func (*ExecPool) TryAdd added in v0.1.2

func (ss *ExecPool) TryAdd(f ...func()) (err error)

type MessageQueueId

type MessageQueueId interface {
	MessageQueueId() string
	DrpcMessage() drpc.Message
}

type Option added in v0.13.0

type Option func(*streamPool)

Option configures a standalone pool created with NewStreamPool.

func WithMetric added in v0.13.0

func WithMetric(m metric.Metric, prefix string) Option

WithMetric registers the standalone pool's prometheus gauges (stream_count, tag_count, dial_queue) under the given namespace prefix, so a service that owns a private pool (e.g. pubsub) is observable even though it never runs the app-component Init.

It deliberately does NOT call RegisterStreamPoolSyncMetric: that feeds a single shared slot on the metric component meant for the one app-level sync streampool, and registering a second pool there would overwrite (and, on Close, null) the sync pool's OutgoingMsg telemetry. The namespaced gauges below give the private pool independent observability without touching that slot.

func WithStreamCloseHook added in v0.13.0

func WithStreamCloseHook(hook func(streamId uint32, peerId string, tags []string)) Option

WithStreamCloseHook registers a callback invoked after a stream is removed from the pool, outside the pool lock, with the closed stream's id, peerId and the tags it carried. The streamId lets a handler clean up per-stream state keyed on CtxStreamId even when several streams share a peerId.

type PeerGetter

type PeerGetter func(ctx context.Context) (peers []peer.Peer, err error)

PeerGetter should dial or return cached peers

type SizeableMessage added in v0.3.9

type SizeableMessage interface {
	Size() int
}

type StreamConfig

type StreamConfig struct {
	// SendQueueSize size of the queue for write per peer
	SendQueueSize int
	// DialQueueWorkers how many workers will dial to peers
	DialQueueWorkers int
	// DialQueueSize size of the dial queue
	DialQueueSize int
}

type StreamPool

type StreamPool interface {
	app.ComponentRunnable
	// AddStream adds new outgoing stream into the pool
	AddStream(stream drpc.Stream, queueSize int, tags ...string) (err error)
	// ReadStream adds new incoming stream and synchronously reads it
	ReadStream(stream drpc.Stream, queueSize int, tags ...string) (err error)
	// Send sends a message to given peers. A stream will be opened if it is not cached before. Works async.
	Send(ctx context.Context, msg drpc.Message, target PeerGetter) (err error)
	// SendById sends a message to given peerIds. Works only if stream exists
	SendById(ctx context.Context, msg drpc.Message, peerIds ...string) (err error)
	// Broadcast sends a message to all peers with given tags. Works async.
	Broadcast(ctx context.Context, msg drpc.Message, tags ...string) (err error)
	// AddTagsCtx adds tags to stream, stream will be extracted from ctx
	AddTagsCtx(ctx context.Context, tags ...string) error
	// RemoveTagsCtx removes tags from stream, stream will be extracted from ctx
	RemoveTagsCtx(ctx context.Context, tags ...string) error
	// RemoveTagsById removes tags from a specific stream by id, for callers that
	// track streamId out of band. Missing streams and tags are ignored.
	RemoveTagsById(streamId uint32, tags ...string) error
	// Streams gets all streams for specific tags
	Streams(tags ...string) (streams []drpc.Stream)
}

StreamPool stores opened streams, and it processes them using StreamHandler It opens a stream using StreamHandler.OpenStream method, if needed. Also, it could send requests to tagged streams or just to specific peers. For example, a stream could be tagged with a specific spaceId.

func New

func New() StreamPool

func NewStreamPool added in v0.13.0

func NewStreamPool(handler streamhandler.StreamHandler, cfg StreamConfig, opts ...Option) StreamPool

NewStreamPool creates a standalone pool with explicit dependencies, for services that own a private pool (e.g. pubsub) instead of sharing the app-level component. The caller must not register it in the app and is responsible for calling Run(ctx) and Close(ctx); Init must not be called.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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