init repo
This commit is contained in:
71
options.go
Normal file
71
options.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package actor
|
||||
|
||||
const (
|
||||
defaultFixedCapacity = 64
|
||||
defaultBatchSize = 64
|
||||
)
|
||||
|
||||
type configuration[T any] struct {
|
||||
fixedCapacity int
|
||||
batchSize int
|
||||
onPanic func(T, any, []byte)
|
||||
dispatcher Dispatcher
|
||||
}
|
||||
|
||||
// Option configures an Actor during construction.
|
||||
type Option[T any] func(*configuration[T])
|
||||
|
||||
// WithFixedCapacity sets the number of retained mailbox nodes. The default is
|
||||
// 64. This is not a mailbox limit: further messages use dynamic nodes.
|
||||
func WithFixedCapacity[T any](capacity int) Option[T] {
|
||||
return func(c *configuration[T]) { c.fixedCapacity = capacity }
|
||||
}
|
||||
|
||||
// WithBatchSize sets how many messages are consumed between calls to
|
||||
// runtime.Gosched. Long pending-batch reversals also yield at this interval.
|
||||
// The default is 64, independent of the dispatcher.
|
||||
func WithBatchSize[T any](size int) Option[T] {
|
||||
return func(c *configuration[T]) { c.batchSize = size }
|
||||
}
|
||||
|
||||
// WithPanicHandler sets an optional callback for handler panics. It receives the
|
||||
// message, the recovered value, and a stack trace. Its own panics are recovered.
|
||||
// A nil callback disables panic reporting; messages still continue to drain.
|
||||
func WithPanicHandler[T any](handler func(message T, value any, stack []byte)) Option[T] {
|
||||
return func(c *configuration[T]) { c.onPanic = handler }
|
||||
}
|
||||
|
||||
// WithDispatcher chooses the construction-time scheduling policy. The default
|
||||
// dispatcher runs inline. New rejects a nil interface or nil DispatcherFunc.
|
||||
// Custom typed-nil implementations must themselves satisfy the Dispatcher
|
||||
// contract; their validity is the caller's responsibility.
|
||||
func WithDispatcher[T any](dispatcher Dispatcher) Option[T] {
|
||||
return func(c *configuration[T]) { c.dispatcher = dispatcher }
|
||||
}
|
||||
|
||||
func configure[T any](opts []Option[T]) configuration[T] {
|
||||
c := configuration[T]{
|
||||
fixedCapacity: defaultFixedCapacity,
|
||||
batchSize: defaultBatchSize,
|
||||
dispatcher: inlineDispatcher{},
|
||||
}
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
panic("actor: nil option")
|
||||
}
|
||||
opt(&c)
|
||||
}
|
||||
if c.fixedCapacity <= 0 {
|
||||
panic("actor: fixed capacity must be positive")
|
||||
}
|
||||
if c.batchSize <= 0 {
|
||||
panic("actor: batch size must be positive")
|
||||
}
|
||||
if c.dispatcher == nil {
|
||||
panic("actor: nil dispatcher")
|
||||
}
|
||||
if f, ok := c.dispatcher.(DispatcherFunc); ok && f == nil {
|
||||
panic("actor: nil dispatcher function")
|
||||
}
|
||||
return c
|
||||
}
|
||||
Reference in New Issue
Block a user