Skip to content

Commit

Permalink
🎨 Improve file read/write locking siyuan-note/siyuan#9748
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 5, 2023
1 parent 557ce24 commit 163c30e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/siyuan-note/dejavu
go 1.18

require (
github.com/88250/gulu v1.2.3-0.20230501031728-4d62370997cd
github.com/88250/gulu v1.2.3-0.20231205093500-bcc43ee27cba
github.com/aws/aws-sdk-go v1.48.6
github.com/dgraph-io/ristretto v0.1.1
github.com/dustin/go-humanize v1.0.1
Expand All @@ -14,7 +14,7 @@ require (
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75
github.com/siyuan-note/eventbus v0.0.0-20230804030110-cf250f838c80
github.com/siyuan-note/filelock v0.0.0-20231107122348-6ed75b0b525a
github.com/siyuan-note/filelock v0.0.0-20231205094230-68ac1f62dabf
github.com/siyuan-note/httpclient v0.0.0-20231120083123-750db4d28b38
github.com/siyuan-note/logging v0.0.0-20231030034701-8265764f00ff
github.com/studio-b12/gowebdav v0.9.0
Expand Down Expand Up @@ -50,3 +50,5 @@ require (
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.0 // indirect
)

//replace github.com/siyuan-note/filelock => D:\88250\filelock
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/88250/gulu v1.2.3-0.20230501031728-4d62370997cd h1:PgbmnoTqfVbYyhUljovnAz6l/oWPlbM1d5yrkVDHO1w=
github.com/88250/gulu v1.2.3-0.20230501031728-4d62370997cd/go.mod h1:pTWnjt+6qUqNnP9xltswsJxgCBVu3C7eW09u48LWX0k=
github.com/88250/gulu v1.2.3-0.20231205093500-bcc43ee27cba h1:ypA1nYRPXm+oac3GnAziPOhMx49nTLtrajePha1CU0g=
github.com/88250/gulu v1.2.3-0.20231205093500-bcc43ee27cba/go.mod h1:pTWnjt+6qUqNnP9xltswsJxgCBVu3C7eW09u48LWX0k=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef h1:2JGTg6JapxP9/R33ZaagQtAM4EkkSYnIAlOG5EI8gkM=
Expand Down Expand Up @@ -96,8 +96,8 @@ github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75 h1:Bi7/7f29
github.com/siyuan-note/encryption v0.0.0-20220713091850-5ecd92177b75/go.mod h1:H8fyqqAbp9XreANjeSbc72zEdFfKTXYN34tc1TjZwtw=
github.com/siyuan-note/eventbus v0.0.0-20230804030110-cf250f838c80 h1:XghjHKJd+SiL0DkGYFVC+UGUDFtnR4v9gkAbPeh9Eq8=
github.com/siyuan-note/eventbus v0.0.0-20230804030110-cf250f838c80/go.mod h1:Sqo4FYX5lAXu7gWkbEdJF0e6P57tNNVV4WDKYDctokI=
github.com/siyuan-note/filelock v0.0.0-20231107122348-6ed75b0b525a h1:fNWhSpB+e0zOc76isiXAhjiQRaHocitRhWthCaCYTJA=
github.com/siyuan-note/filelock v0.0.0-20231107122348-6ed75b0b525a/go.mod h1:HjtfP1NLDw53BRSeAPJgZDIGqyxi4bANYn1IhgDCQUY=
github.com/siyuan-note/filelock v0.0.0-20231205094230-68ac1f62dabf h1:Uv0ySSkICiS+6zZTKtD2keNT4TSQTgkWyaAbYlRr5+k=
github.com/siyuan-note/filelock v0.0.0-20231205094230-68ac1f62dabf/go.mod h1:4teUA+79ESPDbagztng2dJfyv66pGrri5ldGJQG/saI=
github.com/siyuan-note/httpclient v0.0.0-20231120083123-750db4d28b38 h1:deUrbUOwmsNYRT0x12GHaJ8exxmDMwXFL/1J4dVnLMM=
github.com/siyuan-note/httpclient v0.0.0-20231120083123-750db4d28b38/go.mod h1:QOTSBBSeKU90Kb4aeDVcQ0G+2zJcNuhkwAlsJ2cnmkQ=
github.com/siyuan-note/logging v0.0.0-20231030034701-8265764f00ff h1:5GcxrTOJTsusXOLhg4GuHWbEa4M5gu+CNfL0giwNjDM=
Expand Down
15 changes: 6 additions & 9 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,7 @@ func (repo *Repo) putFileChunks(file *entity.File, context map[string]interface{
return
}

filelock.RWLock.Lock()
defer filelock.RWLock.Unlock()

reader, err := os.OpenFile(absPath, os.O_RDONLY, 0644)
reader, err := filelock.OpenFile(absPath, os.O_RDONLY, 0644)
if nil != err {
logging.LogErrorf("open file [%s] failed: %s", absPath, err)
return
Expand All @@ -570,7 +567,7 @@ func (repo *Repo) putFileChunks(file *entity.File, context map[string]interface{
if nil != chnkErr {
err = chnkErr
logging.LogErrorf("chunk file [%s] failed: %s", absPath, chnkErr)
if closeErr := reader.Close(); nil != closeErr {
if closeErr := filelock.CloseFile(reader); nil != closeErr {
logging.LogErrorf("close file [%s] failed: %s", absPath, closeErr)
}
return
Expand All @@ -581,14 +578,14 @@ func (repo *Repo) putFileChunks(file *entity.File, context map[string]interface{
chunk := &entity.Chunk{ID: chunkHash, Data: chnk.Data}
if err = repo.store.PutChunk(chunk); nil != err {
logging.LogErrorf("put chunk [%s] failed: %s", chunkHash, err)
if closeErr := reader.Close(); nil != closeErr {
if closeErr := filelock.CloseFile(reader); nil != closeErr {
logging.LogErrorf("close file [%s] failed: %s", absPath, closeErr)
}
return
}
}

if err = reader.Close(); nil != err {
if err = filelock.CloseFile(reader); nil != err {
logging.LogErrorf("close file [%s] failed: %s", absPath, err)
return
}
Expand Down Expand Up @@ -728,8 +725,8 @@ func (repo *Repo) checkoutFile(file *entity.File, checkoutDir string, count, tot
return
}

filelock.RWLock.Lock()
defer filelock.RWLock.Unlock()
filelock.Lock(absPath)
defer filelock.Unlock(absPath)

for i := 0; i < 3; i++ {
err = os.Rename(f.Name(), absPath) // Windows 上重命名是非原子的
Expand Down

0 comments on commit 163c30e

Please sign in to comment.