From c339ab1219e55f76cdab53baaa5f4b3a8132aff6 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Tue, 16 Jan 2024 10:57:12 +0800 Subject: [PATCH 1/2] feat: optimize the reduce event implement --- service/image.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/service/image.go b/service/image.go index e94afaab..362a70be 100644 --- a/service/image.go +++ b/service/image.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "strings" + "time" "github.com/IceWhaleTech/CasaOS-AppManagement/common" "github.com/IceWhaleTech/CasaOS-AppManagement/pkg/docker" @@ -175,16 +176,35 @@ type PullOut struct { Id string `json:"id"` } +type Throttler struct { + InvokeInterval time.Duration + LastInvokeTime time.Time +} + +func NewThrottler(interval time.Duration) *Throttler { + return &Throttler{ + InvokeInterval: interval, + } +} + +func (t *Throttler) ThrottleFunc(f func()) { + if time.Since(t.LastInvokeTime) >= t.InvokeInterval { + f() + t.LastInvokeTime = time.Now() + } +} + func pullImageProgress(ctx context.Context, out io.ReadCloser, notificationType string, totalImageNum int, currentImage int) { layerNum := 0 completedLayerNum := 0 - lastProgress := 0 decoder := json.NewDecoder(out) if decoder == nil { logger.Error("failed to create json decoder") return } + throttler := NewThrottler(500 * time.Millisecond) + for decoder.More() { var message jsonmessage.JSONMessage if err := decoder.Decode(&message); err != nil { @@ -209,13 +229,12 @@ func pullImageProgress(ctx context.Context, out io.ReadCloser, notificationType progress := completedFraction * currentImageFraction * 100 // reduce the event send frequency - if int(progress) > lastProgress { - lastProgress = int(progress) + throttler.ThrottleFunc(func() { go func(progress int) { PublishEventWrapper(ctx, common.EventTypeAppInstallProgress, map[string]string{ common.PropertyTypeAppProgress.Name: fmt.Sprintf("%d", progress), }) }(int(progress)) - } + }) } } From 1961f7bc654080cabdd4b063ef9270bae818455a Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Tue, 16 Jan 2024 11:38:47 +0800 Subject: [PATCH 2/2] chore: bump golang 1.21 --- .github/workflows/release.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 66897316..5fea0b9e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,6 +4,9 @@ on: push: tags: - v*.*.* +env: + casaos_cache_version: 1.1 # the value define by causal. the field for to clear the GitHub Action cache + permissions: contents: write jobs: