github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/retryable_reader.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"io"
    19  )
    20  
    21  type retryableReader struct {
    22  	current     io.ReadCloser
    23  	offset      int64
    24  	newReader   func(offset int64) (io.ReadCloser, error)
    25  	shouldRetry func(error) bool
    26  }
    27  
    28  func newRetryableReader(
    29  	newReader func(offset int64) (io.ReadCloser, error),
    30  	initOffset int64,
    31  	shouldRetry func(error) bool,
    32  ) (*retryableReader, error) {
    33  	r, err := newReader(initOffset)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return &retryableReader{
    38  		current:     r,
    39  		offset:      initOffset,
    40  		newReader:   newReader,
    41  		shouldRetry: shouldRetry,
    42  	}, nil
    43  }
    44  
    45  var _ io.ReadCloser = new(retryableReader)
    46  
    47  func (r *retryableReader) Read(buf []byte) (int, error) {
    48  	for {
    49  		n, err := r.current.Read(buf)
    50  		if err != nil {
    51  			if r.shouldRetry(err) {
    52  				// retry
    53  				newReader, err := r.newReader(r.offset)
    54  				if err != nil {
    55  					return 0, err
    56  				}
    57  				r.current.Close()
    58  				r.current = newReader
    59  				continue
    60  			} else {
    61  				// not retryable or EOF
    62  				return n, err
    63  			}
    64  		}
    65  		r.offset += int64(n)
    66  		return n, nil
    67  	}
    68  }
    69  
    70  func (r *retryableReader) Close() error {
    71  	return r.current.Close()
    72  }