Skip to content

Commit 1fe4f6d

Browse files
committed
fix(atomic): migrate more atomic useage
1 parent 8d1474b commit 1fe4f6d

20 files changed

Lines changed: 122 additions & 109 deletions

File tree

drivers/115_open/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package _115_open
33
import (
44
"time"
55

6+
sdk "github.com/OpenListTeam/115-sdk-go"
67
"github.com/OpenListTeam/OpenList/v4/internal/model"
78
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
8-
sdk "github.com/OpenListTeam/115-sdk-go"
99
)
1010

1111
type Obj sdk.GetFilesResp_File

drivers/189pc/torrent.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ func (y *Cloud189PC) RapidUploadFromTorrent(ctx context.Context, dstDir model.Ob
8989
sliceMd5Hex = strings.ToUpper(utils.GetMD5EncodeStr(strings.Join(upperSliceMD5s, "\n")))
9090
}
9191

92-
9392
// 使用与 Web 端一致的三步秒传流程
9493
fullUrl := "https://upload.cloud.189.cn"
9594
if isFamily {
@@ -110,7 +109,6 @@ func (y *Cloud189PC) RapidUploadFromTorrent(ctx context.Context, dstDir model.Ob
110109
initParams.Set("familyId", y.FamilyID)
111110
}
112111

113-
114112
var uploadInfo InitMultiUploadResp
115113
_, err = y.request(fullUrl+"/initMultiUpload", "GET", func(req *resty.Request) {
116114
req.SetContext(ctx)
@@ -119,7 +117,6 @@ func (y *Cloud189PC) RapidUploadFromTorrent(ctx context.Context, dstDir model.Ob
119117
return nil, fmt.Errorf("initMultiUpload 失败: %w", err)
120118
}
121119

122-
123120
uploadFileId := uploadInfo.Data.UploadFileID
124121

125122
// Step 2: checkTransSecond(用 fileMd5 + sliceMd5 + uploadFileId 检查秒传)
@@ -129,7 +126,6 @@ func (y *Cloud189PC) RapidUploadFromTorrent(ctx context.Context, dstDir model.Ob
129126
"uploadFileId": uploadFileId,
130127
}
131128

132-
133129
var checkResp struct {
134130
Data struct {
135131
FileDataExists int `json:"fileDataExists"`
@@ -143,7 +139,6 @@ func (y *Cloud189PC) RapidUploadFromTorrent(ctx context.Context, dstDir model.Ob
143139
return nil, fmt.Errorf("秒传检查失败: %w", err)
144140
}
145141

146-
147142
if checkResp.Data.FileDataExists != 1 {
148143
return nil, fmt.Errorf("秒传失败:云端不存在该文件(fileMD5=%s, sliceMD5=%s, size=%d)", fileMD5Upper, sliceMd5Hex, fileSize)
149144
}

drivers/lanzou/driver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lanzou
33
import (
44
"context"
55
"net/http"
6+
"sync/atomic"
67

78
"github.com/OpenListTeam/OpenList/v4/drivers/base"
89
"github.com/OpenListTeam/OpenList/v4/internal/driver"
@@ -18,7 +19,7 @@ type LanZou struct {
1819
uid string
1920
vei string
2021

21-
flag int32
22+
flag atomic.Int32
2223
}
2324

2425
func (d *LanZou) Config() driver.Config {

drivers/lanzou/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"strconv"
1111
"strings"
1212
"sync"
13-
"sync/atomic"
1413
"time"
1514

1615
"github.com/OpenListTeam/OpenList/v4/drivers/base"
@@ -43,17 +42,17 @@ func (d *LanZou) get(url string, callback base.ReqCallback) ([]byte, error) {
4342
func (d *LanZou) post(url string, callback base.ReqCallback, resp interface{}) ([]byte, error) {
4443
data, err := d._post(url, callback, resp, false)
4544
if err == ErrCookieExpiration && d.IsAccount() {
46-
if atomic.CompareAndSwapInt32(&d.flag, 0, 1) {
45+
if d.flag.CompareAndSwap(0, 1) {
4746
_, err2 := d.Login()
48-
atomic.SwapInt32(&d.flag, 0)
47+
d.flag.Swap(0)
4948
if err2 != nil {
5049
err = errors.Join(err, err2)
5150
d.Status = err.Error()
5251
op.MustSaveDriverStorage(d)
5352
return data, err
5453
}
5554
}
56-
for atomic.LoadInt32(&d.flag) != 0 {
55+
for d.flag.Load() != 0 {
5756
runtime.Gosched()
5857
}
5958
return d._post(url, callback, resp, false)

drivers/mediafire/meta.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,3 @@ func init() {
5858
}
5959
})
6060
}
61-

drivers/mediafire/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,3 @@ type MediafireUserInfoResponse struct {
244244
CurrentAPIVersion string `json:"current_api_version"`
245245
} `json:"response"`
246246
}
247-

drivers/mediafire/util.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,3 @@ func (d *Mediafire) getFileByHash(ctx context.Context, hash string) (*model.ObjT
732732
file := resp.Response.FileInfo[0]
733733
return d.fileToObj(file), nil
734734
}
735-

drivers/smb/driver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path"
77
"path/filepath"
88
"strings"
9+
"sync/atomic"
910

1011
"github.com/OpenListTeam/OpenList/v4/internal/driver"
1112
"github.com/OpenListTeam/OpenList/v4/internal/model"
@@ -16,7 +17,7 @@ import (
1617
)
1718

1819
type SMB struct {
19-
lastConnTime int64
20+
lastConnTime atomic.Int64
2021
model.Storage
2122
Addition
2223
fs *smb2.Share

drivers/smb/util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/fs"
77
"os"
88
"path/filepath"
9-
"sync/atomic"
109
"time"
1110

1211
"github.com/OpenListTeam/OpenList/v4/pkg/singleflight"
@@ -16,15 +15,15 @@ import (
1615
)
1716

1817
func (d *SMB) updateLastConnTime() {
19-
atomic.StoreInt64(&d.lastConnTime, time.Now().Unix())
18+
d.lastConnTime.Store(time.Now().Unix())
2019
}
2120

2221
func (d *SMB) cleanLastConnTime() {
23-
atomic.StoreInt64(&d.lastConnTime, 0)
22+
d.lastConnTime.Store(0)
2423
}
2524

2625
func (d *SMB) getLastConnTime() time.Time {
27-
return time.Unix(atomic.LoadInt64(&d.lastConnTime), 0)
26+
return time.Unix(d.lastConnTime.Load(), 0)
2827
}
2928

3029
func (d *SMB) initFS(ctx context.Context) error {

drivers/webdav/driver_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestGetMapsMissingPathToObjectNotFound(t *testing.T) {
2323
}
2424

2525
func TestMakeDirAfterMissingWebDAVStat(t *testing.T) {
26-
var mkcolCount int32
26+
var mkcolCount atomic.Int32
2727
d, cleanup := newTestDriver(t, func(w http.ResponseWriter, r *http.Request) bool {
2828
if r.Method == "MKCOL" && (r.URL.Path == "/new" || r.URL.Path == "/new/") {
29-
atomic.AddInt32(&mkcolCount, 1)
29+
mkcolCount.Add(1)
3030
w.WriteHeader(http.StatusCreated)
3131
return true
3232
}
@@ -37,7 +37,7 @@ func TestMakeDirAfterMissingWebDAVStat(t *testing.T) {
3737
if err := op.MakeDir(context.Background(), d, "/new"); err != nil {
3838
t.Fatalf("MakeDir failed: %v", err)
3939
}
40-
if got := atomic.LoadInt32(&mkcolCount); got != 1 {
40+
if got := mkcolCount.Load(); got != 1 {
4141
t.Fatalf("expected one MKCOL request, got %d", got)
4242
}
4343
}

0 commit comments

Comments
 (0)