diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-11-10 11:47:16 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-11-10 11:54:47 +0300 |
commit | 58eb567b0cc9d60ba8a26398684abb558780bb7f (patch) | |
tree | 1b2818297ffc9c94a012b81fe77ca7c98bfc85f9 | |
parent | 961bae2f3b1759cc06b398a18468fe6931cb30e4 (diff) |
catfile: Unify reading of object info when requesting objects
Use the new `readInfo()` function to unify how we're reading the object
info between `ReadInfo()` and `ReadObject()`.
-rw-r--r-- | internal/git/catfile/object_reader_test.go | 2 | ||||
-rw-r--r-- | internal/git/catfile/request_queue.go | 24 |
2 files changed, 7 insertions, 19 deletions
diff --git a/internal/git/catfile/object_reader_test.go b/internal/git/catfile/object_reader_test.go index 165d782de..fa525bdba 100644 --- a/internal/git/catfile/object_reader_test.go +++ b/internal/git/catfile/object_reader_test.go @@ -323,7 +323,7 @@ func TestObjectReader_queue(t *testing.T) { require.True(t, queue.isClosed()) _, err = queue.ReadObject() - require.Equal(t, fmt.Errorf("cannot read object: %w", os.ErrClosed), err) + require.Equal(t, fmt.Errorf("cannot read object info: %w", os.ErrClosed), err) }) t.Run("closing queue blocks consuming", func(t *testing.T) { diff --git a/internal/git/catfile/request_queue.go b/internal/git/catfile/request_queue.go index 4defe8ba4..efb51e9fe 100644 --- a/internal/git/catfile/request_queue.go +++ b/internal/git/catfile/request_queue.go @@ -92,14 +92,6 @@ func (q *requestQueue) ReadObject() (*Object, error) { q.currentObjectLock.Lock() defer q.currentObjectLock.Unlock() - if q.isClosed() { - return nil, fmt.Errorf("cannot read object: %w", os.ErrClosed) - } - - if atomic.LoadInt64(&q.outstandingRequests) == 0 { - return nil, fmt.Errorf("no outstanding request") - } - if q.currentObject != nil { // If the current object is still dirty, then we must not try to read a new object. if q.currentObject.isDirty() { @@ -116,23 +108,19 @@ func (q *requestQueue) ReadObject() (*Object, error) { } } - oi, err := ParseObjectInfo(q.stdout) + objectInfo, err := q.readInfo() if err != nil { return nil, err } - q.trace.recordRequest(oi.Type) - - if atomic.AddInt64(&q.outstandingRequests, -1) < 0 { - return nil, fmt.Errorf("negative number of requests") - } + q.trace.recordRequest(objectInfo.Type) q.currentObject = &Object{ - ObjectInfo: *oi, + ObjectInfo: *objectInfo, dataReader: io.LimitedReader{ R: q.stdout, - N: oi.Size, + N: objectInfo.Size, }, - bytesRemaining: oi.Size, + bytesRemaining: objectInfo.Size, } return q.currentObject, nil @@ -167,7 +155,7 @@ func (q *requestQueue) readInfo() (*ObjectInfo, error) { // And when there are, we need to remove one of these queued requests. We do so via // `CompareAndSwapInt64()`, which easily allows us to detect concurrent access to the queue. if !atomic.CompareAndSwapInt64(&q.outstandingRequests, queuedRequests, queuedRequests-1) { - return nil, fmt.Errorf("concurrent access to object info queue") + return nil, fmt.Errorf("concurrent access to request queue") } return ParseObjectInfo(q.stdout) |