@@ -10,6 +10,7 @@ import (
1010 "github.com/aws/aws-sdk-go-v2/service/s3"
1111 "io"
1212 "sync"
13+ "sync/atomic"
1314)
1415
1516// concurrentReader receives object parts from working goroutines, composes those chunks in order and read
@@ -81,7 +82,7 @@ func (r *concurrentReader) Read(p []byte) (int, error) {
8182 break
8283 }
8384
84- if r .index == r . getCapacity ( ) {
85+ if r .index == atomic . LoadInt32 ( & r . capacity ) {
8586 continue
8687 }
8788
@@ -202,7 +203,7 @@ func (r *concurrentReader) read(p []byte) (int, error) {
202203
203204 partSize := r .partSize
204205 minIndex := int32 (r .written / partSize )
205- maxIndex := min (int32 ((r .written + int64 (cap (p ))- 1 )/ partSize ), r . getCapacity ( )- 1 )
206+ maxIndex := min (int32 ((r .written + int64 (cap (p ))- 1 )/ partSize ), atomic . LoadInt32 ( & r . capacity )- 1 )
206207 for i := minIndex ; i <= maxIndex ; i ++ {
207208 if e := r .getErr (); e != nil && e != io .EOF {
208209 r .clean ()
@@ -223,9 +224,9 @@ func (r *concurrentReader) read(p []byte) (int, error) {
223224 if c .cur >= c .length {
224225 r .readCount ++
225226 delete (r .buf , i )
226- if r .readCount == r . getCapacity ( ) {
227- capacity := min (r . getCapacity ( )+ r .sectionParts , r .partsCount )
228- r . setCapacity ( capacity )
227+ if r .readCount == atomic . LoadInt32 ( & r . capacity ) {
228+ capacity := min (atomic . LoadInt32 ( & r . capacity )+ r .sectionParts , r .partsCount )
229+ atomic . StoreInt32 ( & r . capacity , capacity )
229230 }
230231 if r .readCount >= r .partsCount {
231232 r .setErr (io .EOF )
@@ -234,7 +235,7 @@ func (r *concurrentReader) read(p []byte) (int, error) {
234235 }
235236 }
236237
237- for r .receiveCount < r . getCapacity ( ) {
238+ for r .receiveCount < atomic . LoadInt32 ( & r . capacity ) {
238239 if e := r .getErr (); e != nil && e != io .EOF {
239240 r .clean ()
240241 return written , e
@@ -263,9 +264,9 @@ func (r *concurrentReader) read(p []byte) (int, error) {
263264 r .buf [oc .index ] = & oc
264265 } else {
265266 r .readCount ++
266- if r .readCount == r . getCapacity ( ) {
267- capacity := min (r . getCapacity ( )+ r .sectionParts , r .partsCount )
268- r . setCapacity ( capacity )
267+ if r .readCount == atomic . LoadInt32 ( & r . capacity ) {
268+ capacity := min (atomic . LoadInt32 ( & r . capacity )+ r .sectionParts , r .partsCount )
269+ atomic . StoreInt32 ( & r . capacity , capacity )
269270 }
270271 if r .readCount >= r .partsCount {
271272 r .setErr (io .EOF )
@@ -276,20 +277,6 @@ func (r *concurrentReader) read(p []byte) (int, error) {
276277 return written , r .getErr ()
277278}
278279
279- func (r * concurrentReader ) setCapacity (n int32 ) {
280- r .m .Lock ()
281- defer r .m .Unlock ()
282-
283- r .capacity = n
284- }
285-
286- func (r * concurrentReader ) getCapacity () int32 {
287- r .m .Lock ()
288- defer r .m .Unlock ()
289-
290- return r .capacity
291- }
292-
293280func (r * concurrentReader ) setDone (done bool ) {
294281 r .m .Lock ()
295282 defer r .m .Unlock ()
0 commit comments