Skip to content

Commit b28f498

Browse files
giwtygiwty
authored andcommitted
add file open retry
1 parent 803ad74 commit b28f498

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/asticode/go-astikit v0.8.0
77
github.com/asticode/go-astilectron v0.16.0
88
github.com/asticode/go-astilectron-bootstrap v0.4.1
9+
github.com/avast/retry-go v2.6.1+incompatible
910
github.com/boltdb/bolt v1.3.1
1011
github.com/go-openapi/strfmt v0.19.2 // indirect
1112
github.com/jedib0t/go-pretty v4.3.0+incompatible

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/asticode/go-astilectron-bundler v0.7.0 h1:P5Ot4loRUMSgHMEkH+DIQ0adKvE
1515
github.com/asticode/go-astilectron-bundler v0.7.0/go.mod h1:dHgXoS/SzrIcMIqzH9rhiWbZjUvjj4HNfUg+7sLeV1Q=
1616
github.com/asticode/go-bindata v1.0.0 h1:5whO0unjdx2kbAbzoBMS3307jKAEf3oQ1lJcx5RdgA8=
1717
github.com/asticode/go-bindata v1.0.0/go.mod h1:t/Y+/iCLrvaYkv8Y6PscRnyUeYzy9y9+8JC9CMcKdHY=
18+
github.com/avast/retry-go v2.6.1+incompatible h1:quvLI98pOPWtTq7xnbX4TI5l9PmRJooM2AI1T7mOFUA=
19+
github.com/avast/retry-go v2.6.1+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
1820
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=
1921
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
2022
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

switchfs/splitFileReader.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package switchfs
22

33
import (
44
"errors"
5+
"github.com/avast/retry-go"
56
"io"
67
"io/ioutil"
78
"os"
@@ -22,6 +23,38 @@ type splitFile struct {
2223
chunkSize int64
2324
}
2425

26+
type fileWrapper struct {
27+
file ReadAtCloser
28+
path string
29+
}
30+
31+
func NewFileWrapper(filePath string) (*fileWrapper, error) {
32+
result := fileWrapper{}
33+
result.path = filePath
34+
file, err := _openFile(filePath)
35+
if err != nil {
36+
return nil, err
37+
}
38+
result.file = file
39+
return &result, nil
40+
}
41+
42+
func (sp *fileWrapper) ReadAt(p []byte, off int64) (n int, err error) {
43+
if sp.file != nil {
44+
return sp.file.ReadAt(p, off)
45+
}
46+
return 0, errors.New("file is not opened")
47+
}
48+
49+
func (sp *fileWrapper) Close() error {
50+
51+
if sp.file != nil {
52+
return sp.file.Close()
53+
}
54+
55+
return nil
56+
}
57+
2558
func NewSplitFileReader(filePath string) (*splitFile, error) {
2659
result := splitFile{}
2760
index := strings.LastIndex(filePath, string(os.PathSeparator))
@@ -51,7 +84,7 @@ func (sp *splitFile) ReadAt(p []byte, off int64) (n int, err error) {
5184
}
5285

5386
if len(sp.files) == 0 || sp.files[part] == nil {
54-
file, _ := os.Open(path.Join(sp.path, sp.info[part].Name()))
87+
file, _ := _openFile(path.Join(sp.path, sp.info[part].Name()))
5588
sp.files[part] = file
5689
}
5790
off = off - sp.chunkSize*int64(part)
@@ -62,6 +95,19 @@ func (sp *splitFile) ReadAt(p []byte, off int64) (n int, err error) {
6295
return sp.files[part].ReadAt(p, off)
6396
}
6497

98+
func _openFile(path string) (*os.File, error) {
99+
var file *os.File
100+
var err error
101+
retry.Attempts(5)
102+
err = retry.Do(
103+
func() error {
104+
file, err = os.Open(path)
105+
return err
106+
},
107+
)
108+
return file, err
109+
}
110+
65111
func (sp *splitFile) Close() error {
66112
for _, file := range sp.files {
67113
if file != nil {
@@ -76,6 +122,6 @@ func OpenFile(filePath string) (ReadAtCloser, error) {
76122
if _, err := strconv.Atoi(filePath[len(filePath)-1:]); err == nil {
77123
return NewSplitFileReader(filePath)
78124
} else {
79-
return os.Open(filePath)
125+
return NewFileWrapper(filePath)
80126
}
81127
}

0 commit comments

Comments
 (0)