File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,28 +18,33 @@ package local
1818
1919import (
2020 "sync"
21+ "time"
2122
2223 "github.com/containerd/containerd/errdefs"
2324 "github.com/pkg/errors"
2425)
2526
2627// Handles locking references
2728
29+ type lock struct {
30+ since time.Time
31+ }
32+
2833var (
2934 // locks lets us lock in process
30- locks = map [string ]struct {}{}
35+ locks = make ( map [string ]* lock )
3136 locksMu sync.Mutex
3237)
3338
3439func tryLock (ref string ) error {
3540 locksMu .Lock ()
3641 defer locksMu .Unlock ()
3742
38- if _ , ok := locks [ref ]; ok {
39- return errors .Wrapf (errdefs .ErrUnavailable , "ref %s locked" , ref )
43+ if v , ok := locks [ref ]; ok {
44+ return errors .Wrapf (errdefs .ErrUnavailable , "ref %s locked since %s " , ref , v . since )
4045 }
4146
42- locks [ref ] = struct {}{ }
47+ locks [ref ] = & lock { time . Now () }
4348 return nil
4449}
4550
Original file line number Diff line number Diff line change 1+ /*
2+ Copyright The containerd Authors.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ package local
18+
19+ import (
20+ "testing"
21+
22+ "gotest.tools/v3/assert"
23+ )
24+
25+ func TestTryLock (t * testing.T ) {
26+ err := tryLock ("testref" )
27+ assert .NilError (t , err )
28+ defer unlock ("testref" )
29+
30+ err = tryLock ("testref" )
31+ assert .ErrorContains (t , err , "ref testref locked since " )
32+ }
You can’t perform that action at this time.
0 commit comments