refactor: Refactor the cron function in http sync (#1600)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
Refactor the cron function in the `Sync` function for the HTTP sync.
This might improve the performance as well because it reduces the number
of HTTP requests.

Signed-off-by: Zhiwei Liang <zhiwei.liang27@pm.me>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
This commit is contained in:
Zhiwei Liang 2025-03-27 15:02:48 -05:00 committed by GitHub
parent 8c5ac2f2c3
commit babcacfe4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 23 deletions

View File

@ -82,30 +82,20 @@ func (hs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
if body == "" { if body == "" {
hs.Logger.Debug("configuration deleted") hs.Logger.Debug("configuration deleted")
} else { return
if hs.LastBodySHA == "" {
hs.Logger.Debug("new configuration created")
msg, err := hs.Fetch(ctx)
if err != nil {
hs.Logger.Error(fmt.Sprintf("error fetching: %s", err.Error()))
} else {
dataSync <- sync.DataSync{FlagData: msg, Source: hs.URI, Type: sync.ALL}
}
} else {
currentSHA := hs.generateSha([]byte(body))
if hs.LastBodySHA != currentSHA {
hs.Logger.Debug("configuration modified")
msg, err := hs.Fetch(ctx)
if err != nil {
hs.Logger.Error(fmt.Sprintf("error fetching: %s", err.Error()))
} else {
dataSync <- sync.DataSync{FlagData: msg, Source: hs.URI, Type: sync.ALL}
}
}
hs.LastBodySHA = currentSHA
}
} }
currentSHA := hs.generateSha([]byte(body))
if hs.LastBodySHA == "" {
hs.Logger.Debug("new configuration created")
dataSync <- sync.DataSync{FlagData: body, Source: hs.URI, Type: sync.ALL}
} else if hs.LastBodySHA != currentSHA {
hs.Logger.Debug("configuration modified")
dataSync <- sync.DataSync{FlagData: body, Source: hs.URI, Type: sync.ALL}
}
hs.LastBodySHA = currentSHA
}) })
hs.Cron.Start() hs.Cron.Start()