Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bump golang to 1.21 #145

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 23 additions & 4 deletions service/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"strings"
"time"

"github.com/IceWhaleTech/CasaOS-AppManagement/common"
"github.com/IceWhaleTech/CasaOS-AppManagement/pkg/docker"
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
})
}
}