bee

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: MIT Imports: 3 Imported by: 0

README

BEE

BEE is a lightweight and flexible worker pool implementation in Go, designed to handle concurrent tasks efficiently. It provides configurable options for worker numbers, queue size, and logging, making it suitable for various use cases.

Features

  • Configurable worker pool with default settings.
  • Support for custom logging.
  • Graceful handling of tasks with error logging.
  • Easy-to-use API for pushing tasks and managing workers.

Installation

To use BEE in your project, add it to your go.mod file:

go get github.com/Nexite-Cloud/bee

Usage

Basic Example
package main

import (
	"context"
	"fmt"
	"time"
	"github.com/Nexite-Cloud/bee"
)

func main() {
	ctx := context.Background()

	// Create a new Hive with default configuration
	hive := bee.NewHive[int](nil)

	// Set a handler for processing tasks
	hive.SetHandler(func(ctx context.Context, data int) error {
		fmt.Printf("Processing data: %d\n", data)
		return nil
	})

	// Start the worker pool
	hive.Start(ctx)

	// Push tasks into the hive
	for i := 0; i < 10; i++ {
		hive.Push(i)
	}

	// Wait for all tasks to complete
	hive.Wait()
}
Custom Configuration
config := &bee.HiveConfig{
	WorkerNumber: 5,
	QueueSize:    100,
	Logger:       customLogger{},
}

hive := bee.NewHive[int](config)

Configuration Options

  • WorkerNumber: Number of workers in the pool (default: 1).
  • QueueSize: Size of the task queue (default: 256).
  • Logger: Custom logger implementation.

License

This project is licensed under the MIT License. See the LICENSE.md file for details.

Documentation

Index

Constants

View Source
const DefaultQueueSize = 1 << 8
View Source
const DefaultWorkerNumber = 1

Variables

This section is empty.

Functions

func GetWorkerIndex added in v0.0.4

func GetWorkerIndex(ctx context.Context) int

GetWorkerIndex retrieves the worker index from the context. If the index is not set, it returns -1

Types

type ContextKey added in v0.0.4

type ContextKey string
const (
	CtxKeyWorkerIdx ContextKey = "_worker_idx"
)

type Handler

type Handler[T any] func(ctx context.Context, data T) error

Handler is the function type for handling data in the hive

type Hive

type Hive[T any] struct {
	// contains filtered or unexported fields
}

Hive is a worker pool that can handle data concurrently

func NewHive

func NewHive[T any](config *HiveConfig) *Hive[T]

NewHive create a worker pool with a given config, if config is nil, use NewConfig()

func (*Hive[T]) Push

func (h *Hive[T]) Push(data T)

Push data into the hive, if the hive is closed, it will panic

func (*Hive[T]) SetHandler

func (h *Hive[T]) SetHandler(handler Handler[T])

SetHandler set the handler for the hive, if not set, it will panic when push data into the hive

func (*Hive[T]) Start

func (h *Hive[T]) Start(ctx context.Context)

Start the worker pool, if already started, it will do nothing

func (*Hive[T]) Wait

func (h *Hive[T]) Wait()

Wait for all workers to finish, if already waited, it will do nothing

type HiveConfig

type HiveConfig struct {
	WorkerNumber int
	QueueSize    int
	Logger       Logger
}

HiveConfig is the config for the hive

func NewConfig

func NewConfig() *HiveConfig

NewConfig create a new config with default values

func (*HiveConfig) WithLogger

func (h *HiveConfig) WithLogger(logger Logger) *HiveConfig

WithLogger set the logger, default is NoLog

func (*HiveConfig) WithQueueSize

func (h *HiveConfig) WithQueueSize(queueSize int) *HiveConfig

WithQueueSize set the size of the queue, default is 256

func (*HiveConfig) WithWorkerNumber

func (h *HiveConfig) WithWorkerNumber(workerNumber int) *HiveConfig

WithWorkerNumber set the number of workers, default is 1

type Logger

type Logger interface {
	Info(ctx context.Context, msg string, args ...any)
	Error(ctx context.Context, msg string, args ...any)
}

Logger is an interface for logging in the hive

func NewSlogLogger

func NewSlogLogger(logger *slog.Logger) Logger

NewSlogLogger creates a new slog logger, if logger is nil, use slog.Default()

type NoLog

type NoLog struct{}

NoLog is a no-op logger

func (NoLog) Error

func (n NoLog) Error(ctx context.Context, msg string, args ...any)

func (NoLog) Info

func (n NoLog) Info(ctx context.Context, msg string, args ...any)

Jump to

Keyboard shortcuts

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