package ruleEngine

import (
	"github.com/gogf/gf/os/grpool"
	"runtime"
)

const (
	APP_DISPATCHER_NAME    = "app-dispatcher"
	TENANT_DISPATCHER_NAME = "tenant-dispatcher"
	DEVICE_DISPATCHER_NAME = "device-dispatcher"
	RULE_DISPATCHER_NAME   = "rule-dispatcher"
)

// IDispatcher 分发器接口
type IDispatcher interface {
	// submit a task,is a func
	Submit(func()) error
	Destroy() error
}

// PoolDispatcher gr pool implements IDispatcher
type PoolDispatcher struct {
	pool *grpool.Pool
}

func (p *PoolDispatcher) Destroy() error {
	return nil
}

func (p *PoolDispatcher) Submit(fn func()) error {
	return p.pool.Add(fn)
}

// NewPoolDispatcher new instance with limit
func NewPoolDispatcher(limit int) *PoolDispatcher {
	if limit <= 0 {
		limit = runtime.GOMAXPROCS(-1)
	}
	pl := grpool.New(limit)
	return &PoolDispatcher{pool: pl}
}
