2021-01-28 19:43:32 +00:00
|
|
|
package settlement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-01 16:18:34 +00:00
|
|
|
"sync"
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/settlement/basic"
|
|
|
|
nodeutil "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-01-28 19:43:32 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-02-01 16:18:34 +00:00
|
|
|
type (
|
2022-10-17 12:03:55 +00:00
|
|
|
// AlphabetState is a callback interface for inner ring global state.
|
2021-03-23 15:20:44 +00:00
|
|
|
AlphabetState interface {
|
|
|
|
IsAlphabet() bool
|
2021-02-01 16:18:34 +00:00
|
|
|
}
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2021-02-01 16:18:34 +00:00
|
|
|
// Processor is an event handler for payments in the system.
|
|
|
|
Processor struct {
|
|
|
|
log *logger.Logger
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2021-03-23 15:20:44 +00:00
|
|
|
state AlphabetState
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2021-02-01 16:18:34 +00:00
|
|
|
pool nodeutil.WorkerPool
|
|
|
|
|
|
|
|
auditProc AuditProcessor
|
|
|
|
|
|
|
|
basicIncome BasicIncomeInitializer
|
|
|
|
|
|
|
|
contextMu sync.Mutex
|
|
|
|
incomeContexts map[uint64]*basic.IncomeSettlementContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prm groups the required parameters of Processor's constructor.
|
|
|
|
Prm struct {
|
|
|
|
AuditProcessor AuditProcessor
|
|
|
|
BasicIncome BasicIncomeInitializer
|
2021-03-23 15:20:44 +00:00
|
|
|
State AlphabetState
|
2021-02-01 16:18:34 +00:00
|
|
|
}
|
|
|
|
)
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func panicOnPrmValue(n string, v any) {
|
2021-01-28 19:43:32 +00:00
|
|
|
panic(fmt.Sprintf("invalid parameter %s (%T):%v", n, v, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates and returns a new Processor instance.
|
|
|
|
func New(prm Prm, opts ...Option) *Processor {
|
|
|
|
switch {
|
|
|
|
case prm.AuditProcessor == nil:
|
|
|
|
panicOnPrmValue("AuditProcessor", prm.AuditProcessor)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := defaultOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](o)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(o.poolSize, ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
panic(fmt.Errorf("could not create worker pool: %w", err))
|
2021-01-28 19:43:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
o.log.Debug(logs.SettlementWorkerPoolForSettlementProcessorSuccessfullyInitialized,
|
2021-01-28 19:43:32 +00:00
|
|
|
zap.Int("capacity", o.poolSize),
|
|
|
|
)
|
|
|
|
|
|
|
|
return &Processor{
|
2021-02-01 16:18:34 +00:00
|
|
|
log: o.log,
|
|
|
|
state: prm.State,
|
|
|
|
pool: pool,
|
|
|
|
auditProc: prm.AuditProcessor,
|
|
|
|
basicIncome: prm.BasicIncome,
|
|
|
|
incomeContexts: make(map[uint64]*basic.IncomeSettlementContext),
|
2021-01-28 19:43:32 +00:00
|
|
|
}
|
|
|
|
}
|