From af8753cb5b3476a8be2e3884d12764a7309edf94 Mon Sep 17 00:00:00 2001 From: Hailey Date: Tue, 19 Nov 2024 15:16:54 -0800 Subject: [PATCH] configurable circuit breakers --- automod/engine/effects.go | 12 ------------ automod/engine/engine.go | 8 ++++++++ automod/engine/persisthelpers.go | 10 +++++----- cmd/hepa/main.go | 28 ++++++++++++++++++++++++++++ cmd/hepa/server.go | 11 +++++++++++ 5 files changed, 52 insertions(+), 17 deletions(-) diff --git a/automod/engine/effects.go b/automod/engine/effects.go index a318615cc..10bbc1fb0 100644 --- a/automod/engine/effects.go +++ b/automod/engine/effects.go @@ -2,18 +2,6 @@ package engine import ( "sync" - "time" -) - -var ( - // time period within which automod will not re-report an account for the same reasonType - ReportDupePeriod = 1 * 24 * time.Hour - // number of reports automod can file per day, for all subjects and types combined (circuit breaker) - QuotaModReportDay = 2000 - // number of takedowns automod can action per day, for all subjects combined (circuit breaker) - QuotaModTakedownDay = 200 - // number of misc actions automod can do per day, for all subjects combined (circuit breaker) - QuotaModActionDay = 1000 ) type CounterRef struct { diff --git a/automod/engine/engine.go b/automod/engine/engine.go index b313a4492..933987072 100644 --- a/automod/engine/engine.go +++ b/automod/engine/engine.go @@ -52,6 +52,14 @@ type Engine struct { type EngineConfig struct { // if enabled, account metadata is not hydrated for every event by default SkipAccountMeta bool + // time period within which automod will not re-report an account for the same reasonType + ReportDupePeriod time.Duration + // number of reports automod can file per day, for all subjects and types combined (circuit breaker) + QuotaModReportDay int + // number of takedowns automod can action per day, for all subjects combined (circuit breaker) + QuotaModTakedownDay int + // number of misc actions automod can do per day, for all subjects combined (circuit breaker) + QuotaModActionDay int } // Entrypoint for external code pushing #identity events in to the engine. diff --git a/automod/engine/persisthelpers.go b/automod/engine/persisthelpers.go index 42ba8934e..b08322223 100644 --- a/automod/engine/persisthelpers.go +++ b/automod/engine/persisthelpers.go @@ -98,7 +98,7 @@ func (eng *Engine) circuitBreakReports(ctx context.Context, reports []ModReport) if err != nil { return nil, fmt.Errorf("checking report action quota: %w", err) } - if c >= QuotaModReportDay { + if c >= eng.Config.QuotaModReportDay { eng.Logger.Warn("CIRCUIT BREAKER: automod reports") return []ModReport{}, nil } @@ -117,7 +117,7 @@ func (eng *Engine) circuitBreakTakedown(ctx context.Context, takedown bool) (boo if err != nil { return false, fmt.Errorf("checking takedown action quota: %w", err) } - if c >= QuotaModTakedownDay { + if c >= eng.Config.QuotaModTakedownDay { eng.Logger.Warn("CIRCUIT BREAKER: automod takedowns") return false, nil } @@ -137,7 +137,7 @@ func (eng *Engine) circuitBreakModAction(ctx context.Context, action bool) (bool if err != nil { return false, fmt.Errorf("checking mod action quota: %w", err) } - if c >= QuotaModActionDay { + if c >= eng.Config.QuotaModActionDay { eng.Logger.Warn("CIRCUIT BREAKER: automod action") return false, nil } @@ -191,7 +191,7 @@ func (eng *Engine) createReportIfFresh(ctx context.Context, xrpcc *xrpc.Client, if err != nil { return false, err } - if time.Since(created.Time()) > ReportDupePeriod { + if time.Since(created.Time()) > eng.Config.ReportDupePeriod { continue } @@ -267,7 +267,7 @@ func (eng *Engine) createRecordReportIfFresh(ctx context.Context, xrpcc *xrpc.Cl if err != nil { return false, err } - if time.Since(created.Time()) > ReportDupePeriod { + if time.Since(created.Time()) > eng.Config.ReportDupePeriod { continue } diff --git a/cmd/hepa/main.go b/cmd/hepa/main.go index 7883d9c29..9e3029b6c 100644 --- a/cmd/hepa/main.go +++ b/cmd/hepa/main.go @@ -149,6 +149,30 @@ func run(args []string) error { Usage: "secret token for prescreen server", EnvVars: []string{"HEPA_PRESCREEN_TOKEN"}, }, + &cli.DurationFlag{ + Name: "report-dupe-period", + Usage: "time period within which automod will not re-report an account for the same reasonType", + EnvVars: []string{"HEPA_REPORT_DUPE_PERIOD"}, + Value: 1 * 24 * time.Hour, + }, + &cli.IntFlag{ + Name: "quota-mod-report-day", + Usage: "number of reports automod can file per day, for all subjects and types combined (circuit breaker)", + EnvVars: []string{"HEPA_QUOTA_MOD_REPORT_DAY"}, + Value: 10000, + }, + &cli.IntFlag{ + Name: "quota-mod-takedown-day", + Usage: "number of takedowns automod can action per day, for all subjects combined (circuit breaker)", + EnvVars: []string{"HEPA_QUOTA_MOD_TAKEDOWN_DAY"}, + Value: 200, + }, + &cli.IntFlag{ + Name: "quota-mod-action-day", + Usage: "number of misc actions automod can do per day, for all subjects combined (circuit breaker)", + EnvVars: []string{"HEPA_QUOTA_MOD_ACTION_DAY"}, + Value: 2000, + }, } app.Commands = []*cli.Command{ @@ -255,6 +279,10 @@ var runCmd = &cli.Command{ FirehoseParallelism: cctx.Int("firehose-parallelism"), // DEPRECATED PreScreenHost: cctx.String("prescreen-host"), PreScreenToken: cctx.String("prescreen-token"), + ReportDupePeriod: cctx.Duration("report-dupe-period"), + QuotaModReportDay: cctx.Int("quota-mod-report-day"), + QuotaModTakedownDay: cctx.Int("quota-mod-takedown-day"), + QuotaModActionDay: cctx.Int("quota-mod-action-day"), }, ) if err != nil { diff --git a/cmd/hepa/server.go b/cmd/hepa/server.go index 9fe08f98e..a4ca252d1 100644 --- a/cmd/hepa/server.go +++ b/cmd/hepa/server.go @@ -14,6 +14,7 @@ import ( "github.com/bluesky-social/indigo/automod" "github.com/bluesky-social/indigo/automod/cachestore" "github.com/bluesky-social/indigo/automod/countstore" + "github.com/bluesky-social/indigo/automod/engine" "github.com/bluesky-social/indigo/automod/flagstore" "github.com/bluesky-social/indigo/automod/rules" "github.com/bluesky-social/indigo/automod/setstore" @@ -54,6 +55,10 @@ type Config struct { FirehoseParallelism int // DEPRECATED PreScreenHost string PreScreenToken string + ReportDupePeriod time.Duration + QuotaModReportDay int + QuotaModTakedownDay int + QuotaModActionDay int } func NewServer(dir identity.Directory, config Config) (*Server, error) { @@ -219,6 +224,12 @@ func NewServer(dir identity.Directory, config Config) (*Server, error) { OzoneClient: ozoneClient, AdminClient: adminClient, BlobClient: blobClient, + Config: engine.EngineConfig{ + ReportDupePeriod: config.ReportDupePeriod, + QuotaModReportDay: config.QuotaModReportDay, + QuotaModTakedownDay: config.QuotaModTakedownDay, + QuotaModActionDay: config.QuotaModActionDay, + }, } s := &Server{