
Now that we have most of the services required for use with containerd, it was found that common patterns were used throughout services. By defining a central `errdefs` package, we ensure that services will map errors to and from grpc consistently and cleanly. One can decorate an error with as much context as necessary, using `pkg/errors` and still have the error mapped correctly via grpc. We make a few sacrifices. At this point, the common errors we use across the repository all map directly to grpc error codes. While this seems positively crazy, it actually works out quite well. The error conditions that were specific weren't super necessary and the ones that were necessary now simply have better context information. We lose the ability to add new codes, but this constraint may not be a bad thing. Effectively, as long as one uses the errors defined in `errdefs`, the error class will be mapped correctly across the grpc boundary and everything will be good. If you don't use those definitions, the error maps to "unknown" and the error message is preserved. Signed-off-by: Stephen J Day <stephen.day@docker.com>
49 lines
946 B
Go
49 lines
946 B
Go
package docker
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/containerd/containerd/content"
|
|
"github.com/containerd/containerd/errdefs"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type Status struct {
|
|
content.Status
|
|
|
|
// UploadUUID is used by the Docker registry to reference blob uploads
|
|
UploadUUID string
|
|
}
|
|
|
|
type StatusTracker interface {
|
|
GetStatus(string) (Status, error)
|
|
SetStatus(string, Status)
|
|
}
|
|
|
|
type memoryStatusTracker struct {
|
|
statuses map[string]Status
|
|
m sync.Mutex
|
|
}
|
|
|
|
func NewInMemoryTracker() StatusTracker {
|
|
return &memoryStatusTracker{
|
|
statuses: map[string]Status{},
|
|
}
|
|
}
|
|
|
|
func (t *memoryStatusTracker) GetStatus(ref string) (Status, error) {
|
|
t.m.Lock()
|
|
defer t.m.Unlock()
|
|
status, ok := t.statuses[ref]
|
|
if !ok {
|
|
return Status{}, errors.Wrapf(errdefs.ErrNotFound, "status for ref %v", ref)
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
func (t *memoryStatusTracker) SetStatus(ref string, status Status) {
|
|
t.m.Lock()
|
|
t.statuses[ref] = status
|
|
t.m.Unlock()
|
|
}
|