From 0bf011ffa20a2f68ff2e3a1e509e0e4b1cced4a2 Mon Sep 17 00:00:00 2001 From: kaanaktas Date: Mon, 19 Sep 2022 17:18:56 +0100 Subject: [PATCH] introduce executor interface --- datafilter/datafilter.go | 11 ++++++++--- datafilter/datafilter_test.go | 6 +++++- executor/executor.go | 24 ++++++++++++++++++------ schedule/schedule.go | 30 +++++++++++++++++------------- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/datafilter/datafilter.go b/datafilter/datafilter.go index 74a1341..6bb0612 100644 --- a/datafilter/datafilter.go +++ b/datafilter/datafilter.go @@ -8,13 +8,18 @@ import ( "sync" ) -func Apply(actions []policy.Action, data *string) { +type Executor struct { + Actions []policy.Action + Data *string +} + +func (e *Executor) Apply() { breaker := make(chan string) in := make(chan Validate) closeCh := make(chan struct{}) - go processor(actions, in, breaker) - go validator(data, in, closeCh, breaker) + go processor(e.Actions, in, breaker) + go validator(e.Data, in, closeCh, breaker) select { case v := <-breaker: diff --git a/datafilter/datafilter_test.go b/datafilter/datafilter_test.go index 7323616..3412022 100644 --- a/datafilter/datafilter_test.go +++ b/datafilter/datafilter_test.go @@ -65,7 +65,11 @@ func TestExecuteDataFilter(t *testing.T) { } }() - Apply(actions, &tt.args.data) + executor := &Executor{ + Actions: actions, + Data: &tt.args.data, + } + executor.Apply() }) } } diff --git a/executor/executor.go b/executor/executor.go index 8e3113f..a822bca 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1,6 +1,7 @@ package executor import ( + "fmt" "github.com/kaanaktas/go-slm/cache" "github.com/kaanaktas/go-slm/config" "github.com/kaanaktas/go-slm/datafilter" @@ -12,6 +13,10 @@ import ( var cacheIn = cache.NewInMemory() +type Executor interface { + Apply() +} + type specification struct { PolicyRuleSetPath string `envconfig:"policy_rule_set_path"` CommonRulesPath string `envconfig:"common_policies_path"` @@ -56,11 +61,18 @@ func Apply(data, serviceName, direction string) { } for _, statement := range policyStatements { - switch statement.Type { - case config.StatementSchedule: - schedule.Apply(statement.Actions) - case config.StatementData: - datafilter.Apply(statement.Actions, &data) - } + statementExecutor := createExecutor(data, statement.Type, statement.Actions) + statementExecutor.Apply() + } +} + +func createExecutor(data string, statementType string, actions []policy.Action) Executor { + switch statementType { + case config.StatementSchedule: + return &schedule.Executor{Actions: actions} + case config.StatementData: + return &datafilter.Executor{Actions: actions, Data: &data} + default: + panic(fmt.Sprintf("StatementType: %s doesn't exist", statementType)) } } diff --git a/schedule/schedule.go b/schedule/schedule.go index b0096ef..e62cfc6 100644 --- a/schedule/schedule.go +++ b/schedule/schedule.go @@ -26,29 +26,22 @@ type schedule struct { Message string `yaml:"message"` } -func Load(scheduleStatementPath string) { - if scheduleStatementPath == "" { - panic("GO_SLM_SCHEDULE_STATEMENT_PATH hasn't been set") - } - content := config.MustReadFile(filepath.Join(config.RootDirectory, scheduleStatementPath)) - var schedules []schedule - config.MustUnmarshalYaml(scheduleStatementPath, content, &schedules) - - cacheIn.Set(key, schedules, cache.NoExpiration) +type Executor struct { + Actions []policy.Action } -func Apply(actions []policy.Action) { +func (e *Executor) Apply() { cachedSchedule, ok := cacheIn.Get(key) if !ok { panic("schedule doesn't exist in the cache") } schedules := cachedSchedule.([]schedule) - sort.Slice(actions, func(i, j int) bool { - return actions[i].Order < actions[j].Order + sort.Slice(e.Actions, func(i, j int) bool { + return e.Actions[i].Order < e.Actions[j].Order }) - for _, action := range actions { + for _, action := range e.Actions { if action.Active { for _, sc := range schedules { if sc.ScheduleName == action.Name { @@ -61,6 +54,17 @@ func Apply(actions []policy.Action) { } } +func Load(scheduleStatementPath string) { + if scheduleStatementPath == "" { + panic("GO_SLM_SCHEDULE_STATEMENT_PATH hasn't been set") + } + content := config.MustReadFile(filepath.Join(config.RootDirectory, scheduleStatementPath)) + var schedules []schedule + config.MustUnmarshalYaml(scheduleStatementPath, content, &schedules) + + cacheIn.Set(key, schedules, cache.NoExpiration) +} + func isScheduleMatchWithPolicy(sc schedule) bool { if isScheduledDayActive(sc.Days) { return isCurrentTimeInScheduledTime(generateStartTime(sc.Start), time.Duration(sc.Duration))