added payment orchestrator optimizer tweaks

This commit is contained in:
Stephan D
2026-03-11 15:58:18 +01:00
parent 27b4ece6c6
commit 9f998b8134
19 changed files with 1164 additions and 157 deletions

View File

@@ -3,7 +3,9 @@ package serverimp
import (
"strings"
"github.com/tech/sendico/payments/orchestrator/internal/service/orchestrationv2/psvc"
"github.com/tech/sendico/payments/orchestrator/internal/service/orchestrator"
"github.com/tech/sendico/payments/storage/model"
"github.com/tech/sendico/pkg/discovery"
"github.com/tech/sendico/pkg/mlogger"
)
@@ -42,3 +44,52 @@ func buildGatewayRegistry(logger mlogger.Logger, src []gatewayInstanceConfig, re
}
return orchestrator.NewDiscoveryGatewayRegistry(logger, registry)
}
func buildBatchOptimizationPolicy(cfg optimizerConfig) psvc.BatchOptimizationPolicy {
rules := make([]psvc.BatchOptimizationRule, 0, len(cfg.Aggregation.Rules))
for i := range cfg.Aggregation.Rules {
ruleCfg := cfg.Aggregation.Rules[i]
rule := psvc.BatchOptimizationRule{
ID: strings.TrimSpace(ruleCfg.ID),
Enabled: ruleCfg.Enabled,
Priority: ruleCfg.Priority,
Mode: psvc.BatchOptimizationMode(strings.TrimSpace(ruleCfg.Mode)),
Match: psvc.BatchOptimizationMatch{
Rail: model.ParseRail(ruleCfg.Match.Rail),
Providers: cloneTrimmedSlice(ruleCfg.Match.Providers),
Networks: cloneTrimmedSlice(ruleCfg.Match.Networks),
Currencies: cloneTrimmedSlice(ruleCfg.Match.Currencies),
},
}
if ruleCfg.Match.Amount != nil {
rule.Match.Amount = &psvc.BatchOptimizationAmountRange{
Min: strings.TrimSpace(ruleCfg.Match.Amount.Min),
Max: strings.TrimSpace(ruleCfg.Match.Amount.Max),
Currency: strings.TrimSpace(ruleCfg.Match.Amount.Currency),
}
}
rules = append(rules, rule)
}
return psvc.BatchOptimizationPolicy{
DefaultMode: psvc.BatchOptimizationMode(strings.TrimSpace(cfg.Aggregation.DefaultMode)),
Rules: rules,
}
}
func cloneTrimmedSlice(values []string) []string {
if len(values) == 0 {
return nil
}
out := make([]string, 0, len(values))
for i := range values {
token := strings.TrimSpace(values[i])
if token == "" {
continue
}
out = append(out, token)
}
if len(out) == 0 {
return nil
}
return out
}

View File

@@ -15,9 +15,41 @@ type config struct {
*grpcapp.Config `yaml:",inline"`
CardGateways map[string]cardGatewayRouteConfig `yaml:"card_gateways"`
GatewayInstances []gatewayInstanceConfig `yaml:"gateway_instances"`
Optimizer optimizerConfig `yaml:"optimizer"`
QuoteRetentionHrs int `yaml:"quote_retention_hours"`
}
type optimizerConfig struct {
Aggregation aggregationConfig `yaml:"aggregation"`
}
type aggregationConfig struct {
DefaultMode string `yaml:"default_mode"`
Rules []aggregationRuleConfig `yaml:"rules"`
}
type aggregationRuleConfig struct {
ID string `yaml:"id"`
Enabled *bool `yaml:"enabled"`
Priority int `yaml:"priority"`
Mode string `yaml:"mode"`
Match aggregationMatchConfig `yaml:"match"`
}
type aggregationMatchConfig struct {
Rail string `yaml:"rail"`
Providers []string `yaml:"providers"`
Networks []string `yaml:"networks"`
Currencies []string `yaml:"currencies"`
Amount *aggregationAmountConfig `yaml:"amount"`
}
type aggregationAmountConfig struct {
Min string `yaml:"min"`
Max string `yaml:"max"`
Currency string `yaml:"currency"`
}
type cardGatewayRouteConfig struct {
FundingAddress string `yaml:"funding_address"`
FeeAddress string `yaml:"fee_address"`

View File

@@ -40,6 +40,7 @@ func (i *Imp) buildServiceOptions(cfg *config, deps *orchestratorDeps) []orchest
if routes := buildCardGatewayRoutes(cfg.CardGateways); len(routes) > 0 {
opts = append(opts, orchestrator.WithCardGatewayRoutes(routes))
}
opts = append(opts, orchestrator.WithBatchOptimizationPolicy(buildBatchOptimizationPolicy(cfg.Optimizer)))
if registry := buildGatewayRegistry(i.logger, cfg.GatewayInstances, i.discoveryReg); registry != nil {
opts = append(opts, orchestrator.WithGatewayRegistry(registry))
}