mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
Merge pull request #51444 from thaJeztah/buildcache_singleflight
daemon/internal/builder-next: Builder.DiskUsage(): use singleflight
This commit is contained in:
@@ -40,6 +40,7 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
grpcmetadata "google.golang.org/grpc/metadata"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"resenje.org/singleflight"
|
||||
"tags.cncf.io/container-device-interface/pkg/cdi"
|
||||
)
|
||||
|
||||
@@ -106,6 +107,7 @@ type Builder struct {
|
||||
controller *control.Controller
|
||||
dnsconfig config.DNSConfig
|
||||
reqBodyHandler *reqBodyHandler
|
||||
diskUsage singleflight.Group[buildbackend.DiskUsageOptions, *buildbackend.DiskUsage]
|
||||
|
||||
mu sync.Mutex
|
||||
jobs map[string]*buildJob
|
||||
@@ -151,49 +153,52 @@ func (b *Builder) Cancel(ctx context.Context, id string) error {
|
||||
|
||||
// DiskUsage returns a report about space used by build cache
|
||||
func (b *Builder) DiskUsage(ctx context.Context, options buildbackend.DiskUsageOptions) (*buildbackend.DiskUsage, error) {
|
||||
duResp, err := b.controller.DiskUsage(ctx, &controlapi.DiskUsageRequest{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting build cache usage: %w", err)
|
||||
}
|
||||
res, _, err := b.diskUsage.Do(ctx, options, func(ctx context.Context) (*buildbackend.DiskUsage, error) {
|
||||
duResp, err := b.controller.DiskUsage(ctx, &controlapi.DiskUsageRequest{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting build cache usage: %w", err)
|
||||
}
|
||||
|
||||
var usage buildbackend.DiskUsage
|
||||
for _, r := range duResp.Record {
|
||||
usage.TotalCount++
|
||||
usage.TotalSize += r.Size
|
||||
if r.InUse {
|
||||
usage.ActiveCount++
|
||||
var usage buildbackend.DiskUsage
|
||||
for _, r := range duResp.Record {
|
||||
usage.TotalCount++
|
||||
usage.TotalSize += r.Size
|
||||
if r.InUse {
|
||||
usage.ActiveCount++
|
||||
}
|
||||
if !r.InUse && !r.Shared {
|
||||
usage.Reclaimable += r.Size
|
||||
}
|
||||
if !options.Verbose {
|
||||
continue
|
||||
}
|
||||
usage.Items = append(usage.Items, build.CacheRecord{
|
||||
ID: r.ID,
|
||||
Parents: r.Parents,
|
||||
Type: r.RecordType,
|
||||
Description: r.Description,
|
||||
InUse: r.InUse,
|
||||
Shared: r.Shared,
|
||||
Size: r.Size,
|
||||
CreatedAt: func() time.Time {
|
||||
if r.CreatedAt != nil {
|
||||
return r.CreatedAt.AsTime()
|
||||
}
|
||||
return time.Time{}
|
||||
}(),
|
||||
LastUsedAt: func() *time.Time {
|
||||
if r.LastUsedAt == nil {
|
||||
return nil
|
||||
}
|
||||
t := r.LastUsedAt.AsTime()
|
||||
return &t
|
||||
}(),
|
||||
UsageCount: int(r.UsageCount),
|
||||
})
|
||||
}
|
||||
if !r.InUse && !r.Shared {
|
||||
usage.Reclaimable += r.Size
|
||||
}
|
||||
if !options.Verbose {
|
||||
continue
|
||||
}
|
||||
usage.Items = append(usage.Items, build.CacheRecord{
|
||||
ID: r.ID,
|
||||
Parents: r.Parents,
|
||||
Type: r.RecordType,
|
||||
Description: r.Description,
|
||||
InUse: r.InUse,
|
||||
Shared: r.Shared,
|
||||
Size: r.Size,
|
||||
CreatedAt: func() time.Time {
|
||||
if r.CreatedAt != nil {
|
||||
return r.CreatedAt.AsTime()
|
||||
}
|
||||
return time.Time{}
|
||||
}(),
|
||||
LastUsedAt: func() *time.Time {
|
||||
if r.LastUsedAt == nil {
|
||||
return nil
|
||||
}
|
||||
t := r.LastUsedAt.AsTime()
|
||||
return &t
|
||||
}(),
|
||||
UsageCount: int(r.UsageCount),
|
||||
})
|
||||
}
|
||||
return &usage, nil
|
||||
return &usage, nil
|
||||
})
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Prune clears all reclaimable build cache.
|
||||
|
||||
Reference in New Issue
Block a user