X Tutup
Skip to content

Commit dcf7ff5

Browse files
committed
Update apply diff to support context cancellation
Allows cancellation of apply when the grpc service issues a cancel. Adds a timing log for apply. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
1 parent 587f252 commit dcf7ff5

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

archive/tar.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ func Apply(ctx context.Context, root string, r io.Reader) (int64, error) {
107107

108108
// Iterate through the files in the archive.
109109
for {
110+
select {
111+
case <-ctx.Done():
112+
return 0, ctx.Err()
113+
default:
114+
}
115+
110116
hdr, err := tr.Next()
111117
if err == io.EOF {
112118
// end of tar archive
@@ -455,9 +461,8 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
455461
if err != nil {
456462
return err
457463
}
458-
buf := bufferPool.Get().([]byte)
459-
_, err = io.CopyBuffer(file, reader, buf)
460-
bufferPool.Put(buf)
464+
465+
_, err = copyBuffered(ctx, file, reader)
461466
if err1 := file.Close(); err == nil {
462467
err = err1
463468
}
@@ -524,3 +529,41 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
524529

525530
return chtimes(path, boundTime(latestTime(hdr.AccessTime, hdr.ModTime)), boundTime(hdr.ModTime))
526531
}
532+
533+
func copyBuffered(ctx context.Context, dst io.Writer, src io.Reader) (written int64, err error) {
534+
buf := bufferPool.Get().([]byte)
535+
defer bufferPool.Put(buf)
536+
537+
for {
538+
select {
539+
case <-ctx.Done():
540+
err = ctx.Err()
541+
return
542+
default:
543+
}
544+
545+
nr, er := src.Read(buf)
546+
if nr > 0 {
547+
nw, ew := dst.Write(buf[0:nr])
548+
if nw > 0 {
549+
written += int64(nw)
550+
}
551+
if ew != nil {
552+
err = ew
553+
break
554+
}
555+
if nr != nw {
556+
err = io.ErrShortWrite
557+
break
558+
}
559+
}
560+
if er != nil {
561+
if er != io.EOF {
562+
err = er
563+
}
564+
break
565+
}
566+
}
567+
return written, err
568+
569+
}

diff/walking/differ.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
digest "github.com/opencontainers/go-digest"
2525
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2626
"github.com/pkg/errors"
27+
"github.com/sirupsen/logrus"
2728
"golang.org/x/net/context"
2829
)
2930

@@ -63,7 +64,18 @@ func NewWalkingDiff(store content.Store) (diff.Differ, error) {
6364
// Apply applies the content associated with the provided digests onto the
6465
// provided mounts. Archive content will be extracted and decompressed if
6566
// necessary.
66-
func (s *walkingDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount) (ocispec.Descriptor, error) {
67+
func (s *walkingDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mounts []mount.Mount) (d ocispec.Descriptor, err error) {
68+
t1 := time.Now()
69+
defer func() {
70+
if err == nil {
71+
log.G(ctx).WithFields(logrus.Fields{
72+
"d": time.Now().Sub(t1),
73+
"dgst": desc.Digest,
74+
"size": desc.Size,
75+
"media": desc.MediaType,
76+
}).Debugf("diff applied")
77+
}
78+
}()
6779
var isCompressed bool
6880
switch desc.MediaType {
6981
case ocispec.MediaTypeImageLayer, images.MediaTypeDockerSchema2Layer:

0 commit comments

Comments
 (0)
X Tutup