Skip to content

Commit

Permalink
fix: replace the lock with an RW lock to only lock the read on list a…
Browse files Browse the repository at this point in the history
…nd fetch
  • Loading branch information
Skarlso committed Dec 18, 2024
1 parent 934f789 commit 43ed229
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions api/tech/oras/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Client struct {
client *auth.Client
plainHTTP bool
ref string
mu sync.Mutex
mu sync.RWMutex
}

var (
Expand All @@ -38,10 +38,34 @@ func New(opts ClientOptions) *Client {
return &Client{client: opts.Client, plainHTTP: opts.PlainHTTP}
}

func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descriptor, error) {
func (c *Client) Fetcher(ctx context.Context, ref string) (Fetcher, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Pusher(ctx context.Context, ref string) (Pusher, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Lister(ctx context.Context, ref string) (Lister, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descriptor, error) {
c.mu.RLock()
defer c.mu.RUnlock()

src, err := c.createRepository(ref)
if err != nil {
return "", ociv1.Descriptor{}, err
Expand All @@ -64,33 +88,9 @@ func (c *Client) Resolve(ctx context.Context, ref string) (string, ociv1.Descrip
return "", desc, nil
}

func (c *Client) Fetcher(ctx context.Context, ref string) (Fetcher, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Pusher(ctx context.Context, ref string) (Pusher, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Lister(ctx context.Context, ref string) (Lister, error) {
c.mu.Lock()
defer c.mu.Unlock()

c.ref = ref
return c, nil
}

func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) error {
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()

reader, err := src.Reader()
if err != nil {
Expand Down Expand Up @@ -124,8 +124,8 @@ func (c *Client) Push(ctx context.Context, d ociv1.Descriptor, src Source) error
}

func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadCloser, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()

src, err := c.createRepository(c.ref)
if err != nil {
Expand Down Expand Up @@ -167,8 +167,8 @@ func (c *Client) Fetch(ctx context.Context, desc ociv1.Descriptor) (io.ReadClose
}

func (c *Client) List(ctx context.Context) ([]string, error) {
c.mu.Lock()
defer c.mu.Unlock()
c.mu.RLock()
defer c.mu.RUnlock()

src, err := c.createRepository(c.ref)
if err != nil {
Expand Down

0 comments on commit 43ed229

Please sign in to comment.