Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/base/fgo

fgo

go
import "gitlab.soludian.com/soludian/fountain/libs/base/fgo"

Index

Constants

DefaultPoolSize giới hạn tổng số background task chạy đồng thời cho default pool. Đủ lớn để không tạo back-pressure ở tải bình thường, đủ nhỏ để chặn goroutine explosion ở tải đột biến (1 service phát sinh hàng nghìn fire-and-forget tasks).

go
const DefaultPoolSize = 256

func InFlight

go
func InFlight() int64

InFlight trả về số task đang chạy trên default pool.

func Run

go
func Run(name string, fn func())

Run sử dụng default pool. Xem Pool.Run.

type Pool

Pool là một bounded worker pool dựa trên semaphore.

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

func NewPool

go
func NewPool(capacity int) *Pool

NewPool tạo Pool mới với capacity cho trước. Có thể sử dụng "github.com/alitto/pond/v2" để có thêm tính năng như timeout, retry, v.v. nhưng ở đây implement đơn giản để tránh phụ thuộc ngoài.

func (*Pool) InFlight

go
func (p *Pool) InFlight() int64

InFlight trả về số task đang chạy. Hữu ích cho monitoring/metrics.

func (*Pool) Run

go
func (p *Pool) Run(name string, fn func())

Run chạy fn trong goroutine có giới hạn concurrency.

  • Nếu pool còn slot: acquire ngay, spawn goroutine, return ngay.
  • Nếu pool đầy: block caller cho tới khi có slot trống (back-pressure).
  • Recover panic để 1 task lỗi không crash service.

Tham số `name` chỉ dùng cho logging khi panic xảy ra.

Generated by gomarkdoc