Skip to content

Commit

Permalink
refactor: GC has supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
uri-99 committed Oct 15, 2024
1 parent 7391495 commit ef8e5d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 11 additions & 0 deletions aggregator/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"time"

"github.com/urfave/cli/v2"
"github.com/yetanotherco/aligned_layer/aggregator/internal/pkg"
Expand Down Expand Up @@ -38,6 +39,9 @@ func main() {
}
}

const garbageCollectorPeriod = time.Second * 150 //TODO change to time.Day * 1
const garbageCollectorTasksAge = uint64(10) //TODO change to 2592000, 1 month of blocks

func aggregatorMain(ctx *cli.Context) error {

configFilePath := ctx.String(config.ConfigFileFlag.Name)
Expand All @@ -49,6 +53,13 @@ func aggregatorMain(ctx *cli.Context) error {
return err
}

// Supervisor revives garbage collector
go func() {
for {
aggregator.ClearTasksFromMaps(garbageCollectorPeriod, garbageCollectorTasksAge)
}
}()

// Listen for new task created in the ServiceManager contract in a separate goroutine, both V1 and V2 subscriptions:
go func() {
listenErr := aggregator.SubscribeToNewTasks()
Expand Down
14 changes: 9 additions & 5 deletions aggregator/internal/pkg/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func NewAggregator(aggregatorConfig config.AggregatorConfig) (*Aggregator, error
telemetry: aggregatorTelemetry,
}

go aggregator.clearTasksFromMaps(garbageCollectorPeriod, garbageCollectorTasksAge)

return &aggregator, nil
}
Expand Down Expand Up @@ -217,8 +216,6 @@ func (agg *Aggregator) Start(ctx context.Context) error {

const MaxSentTxRetries = 5

const garbageCollectorPeriod = time.Second * 150 //TODO change to time.Day * 1
const garbageCollectorTasksAge = uint64(10) //TODO change to 2592000, 1 month of blocks

const BLS_AGG_SERVICE_TIMEOUT = 100 * time.Second

Expand Down Expand Up @@ -392,8 +389,15 @@ func (agg *Aggregator) AddNewTask(batchMerkleRoot [32]byte, senderAddress [20]by
}

// long-lived gorouting that periodically checks and removes old Tasks from stored Maps
func (agg *Aggregator) clearTasksFromMaps(period time.Duration, blocksOld uint64) {
agg.AggregatorConfig.BaseConfig.Logger.Info("- Removing finalized Task Infos from Maps every %d seconds", period)
func (agg *Aggregator) ClearTasksFromMaps(period time.Duration, blocksOld uint64) {
defer func() {
err := recover() //stops panics
if err != nil {
agg.logger.Error(err.(string))
}
}()

agg.AggregatorConfig.BaseConfig.Logger.Info(fmt.Sprintf("- Removing finalized Task Infos from Maps every %v", period))
lastIdxDeleted := uint32(0)

for {
Expand Down

0 comments on commit ef8e5d6

Please sign in to comment.