github.com/rawahars/moby@v24.0.4+incompatible/distribution/utils/progress.go (about)

     1  package utils // import "github.com/docker/docker/distribution/utils"
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"os"
     7  	"syscall"
     8  
     9  	"github.com/docker/docker/pkg/progress"
    10  	"github.com/docker/docker/pkg/streamformatter"
    11  	"github.com/sirupsen/logrus"
    12  )
    13  
    14  // WriteDistributionProgress is a helper for writing progress from chan to JSON
    15  // stream with an optional cancel function.
    16  func WriteDistributionProgress(cancelFunc func(), outStream io.Writer, progressChan <-chan progress.Progress) {
    17  	progressOutput := streamformatter.NewJSONProgressOutput(outStream, false)
    18  	operationCancelled := false
    19  
    20  	for prog := range progressChan {
    21  		if err := progressOutput.WriteProgress(prog); err != nil && !operationCancelled {
    22  			// don't log broken pipe errors as this is the normal case when a client aborts
    23  			if isBrokenPipe(err) {
    24  				logrus.Info("Pull session cancelled")
    25  			} else {
    26  				logrus.Errorf("error writing progress to client: %v", err)
    27  			}
    28  			cancelFunc()
    29  			operationCancelled = true
    30  			// Don't return, because we need to continue draining
    31  			// progressChan until it's closed to avoid a deadlock.
    32  		}
    33  	}
    34  }
    35  
    36  func isBrokenPipe(e error) bool {
    37  	if netErr, ok := e.(*net.OpError); ok {
    38  		e = netErr.Err
    39  		if sysErr, ok := netErr.Err.(*os.SyscallError); ok {
    40  			e = sysErr.Err
    41  		}
    42  	}
    43  	return e == syscall.EPIPE
    44  }