github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/proxy/proxymanifeststore.go (about)

     1  package proxy
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/docker/distribution"
     7  	"github.com/docker/distribution/context"
     8  	"github.com/docker/distribution/digest"
     9  	"github.com/docker/distribution/registry/proxy/scheduler"
    10  )
    11  
    12  // todo(richardscothern): from cache control header or config
    13  const repositoryTTL = time.Duration(24 * 7 * time.Hour)
    14  
    15  type proxyManifestStore struct {
    16  	ctx             context.Context
    17  	localManifests  distribution.ManifestService
    18  	remoteManifests distribution.ManifestService
    19  	repositoryName  string
    20  	scheduler       *scheduler.TTLExpirationScheduler
    21  }
    22  
    23  var _ distribution.ManifestService = &proxyManifestStore{}
    24  
    25  func (pms proxyManifestStore) Exists(ctx context.Context, dgst digest.Digest) (bool, error) {
    26  	exists, err := pms.localManifests.Exists(ctx, dgst)
    27  	if err != nil {
    28  		return false, err
    29  	}
    30  	if exists {
    31  		return true, nil
    32  	}
    33  
    34  	return pms.remoteManifests.Exists(ctx, dgst)
    35  }
    36  
    37  func (pms proxyManifestStore) Get(ctx context.Context, dgst digest.Digest, options ...distribution.ManifestServiceOption) (distribution.Manifest, error) {
    38  	// At this point `dgst` was either specified explicitly, or returned by the
    39  	// tagstore with the most recent association.
    40  	var fromRemote bool
    41  	manifest, err := pms.localManifests.Get(ctx, dgst, options...)
    42  	if err != nil {
    43  		manifest, err = pms.remoteManifests.Get(ctx, dgst, options...)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		fromRemote = true
    48  	}
    49  
    50  	_, payload, err := manifest.Payload()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	proxyMetrics.ManifestPush(uint64(len(payload)))
    56  	if fromRemote {
    57  		proxyMetrics.ManifestPull(uint64(len(payload)))
    58  
    59  		_, err = pms.localManifests.Put(ctx, manifest)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  
    64  		// Schedule the repo for removal
    65  		pms.scheduler.AddManifest(pms.repositoryName, repositoryTTL)
    66  
    67  		// Ensure the manifest blob is cleaned up
    68  		pms.scheduler.AddBlob(dgst.String(), repositoryTTL)
    69  	}
    70  
    71  	return manifest, err
    72  }
    73  
    74  func (pms proxyManifestStore) Put(ctx context.Context, manifest distribution.Manifest, options ...distribution.ManifestServiceOption) (digest.Digest, error) {
    75  	var d digest.Digest
    76  	return d, distribution.ErrUnsupported
    77  }
    78  
    79  func (pms proxyManifestStore) Delete(ctx context.Context, dgst digest.Digest) error {
    80  	return distribution.ErrUnsupported
    81  }
    82  
    83  /*func (pms proxyManifestStore) Enumerate(ctx context.Context, manifests []distribution.Manifest, last distribution.Manifest) (n int, err error) {
    84  	return 0, distribution.ErrUnsupported
    85  }
    86  */