pool

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package pool runs and multiplexes many bots by token over a single process, lazily starting a Bot per token and garbage-collecting idle ones.

It is the multi-bot front end for github.com/gotd/botapi: a local Bot API server, or any service that serves many bots, can hold one Pool and call Do with a token to borrow a connected Bot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// AppID and AppHash are the shared MTProto app identity used for every bot
	// (https://my.telegram.org). Required.
	AppID   int
	AppHash string

	// Logger is the base structured logger (github.com/gotd/log port).
	// Defaults to a no-op logger.
	Logger log.Logger

	// StateDir, when set, is the directory holding each bot's persistent session
	// file (<id>.bbolt). When empty, bots run with in-memory storage and nothing
	// survives a restart.
	StateDir string

	// IdleTimeout shuts down a bot that has not been borrowed for this long.
	// Zero disables idle collection (RunGC then does nothing).
	IdleTimeout time.Duration
}

Options configures a Pool.

type Pool

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

Pool is a concurrency-safe set of bots keyed by token.

func New

func New(opt Options) (*Pool, error)

New constructs an empty Pool. It performs no network I/O.

func (*Pool) Close

func (p *Pool) Close()

Close stops every bot in the pool.

func (*Pool) Do

func (p *Pool) Do(ctx context.Context, token string, fn func(*botapi.Bot) error) error

Do borrows the running bot for token, starting and authorizing it on first use, and invokes fn with it. It blocks until the bot is ready, fn returns, or ctx is canceled. A failure to start the bot is returned to every concurrent caller waiting on it.

func (*Pool) Kill

func (p *Pool) Kill(token string)

Kill stops and removes the bot for the token, if present.

func (*Pool) RunGC

func (p *Pool) RunGC(ctx context.Context)

RunGC periodically reaps bots idle longer than IdleTimeout until ctx is canceled. It is a no-op when IdleTimeout is zero.

type Token

type Token struct {
	ID     int    // 123456
	Secret string // ABC-DEF1234ghIkl-zyx57W2v1u123ew11
}

Token is a parsed BotFather token, e.g. 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

func ParseToken

func ParseToken(s string) (Token, error)

ParseToken parses a BotFather token.

func (Token) String

func (t Token) String() string

String returns the token in its wire form.

Jump to

Keyboard shortcuts

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