Skip to content

Commit 21477c1

Browse files
OlegChuevmarjune163
authored andcommitted
refactor(archive): replaced int32 with *atomic.Uint32
1 parent ccfef21 commit 21477c1

File tree

7 files changed

+36
-25
lines changed

7 files changed

+36
-25
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ ghfs [options]
260260
A download link will appear on top part of the page.
261261
--archive-max-workers <number>
262262
Maximum number of concurrent archive operations.
263-
Set to -1 for unlimited (default).
263+
Set to 0 for unlimited (default).
264264
When the limit is reached, new archive requests will receive 429 Too Many Requests.
265265
--archive <url-path> ...
266266
--archive-user <separator><url-path>[<separator><allowed-username>...] ...

src/goNixArgParser/parseResult.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ func (r *ParseResult) GetInt(key string) (value int, found bool) {
118118
return
119119
}
120120

121-
func (r *ParseResult) GetInt32(key string) (value int32, found bool) {
121+
func (r *ParseResult) GetUint32(key string) (value uint32, found bool) {
122122
str, found := r.GetString(key)
123123
if !found {
124124
return
125125
}
126126

127-
value, err := toInt32(str)
127+
value, err := toUint32(str)
128128
found = err == nil
129129
return
130130
}

src/goNixArgParser/util.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ func toInts(input []string) ([]int, error) {
8181
return output, nil
8282
}
8383

84-
func toInt32(input string) (int32, error) {
85-
v, err := strconv.ParseInt(input, 10, 32)
86-
return int32(v), err
84+
func toUint32(input string) (uint32, error) {
85+
v, err := strconv.ParseUint(input, 10, 32)
86+
return uint32(v), err
8787
}
8888

8989
func toInt64(input string) (int64, error) {

src/param/cli.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func NewCliCmd() *goNixArgParser.Command {
148148
err = options.AddFlagValues("archivedirsusers", "--archive-dir-user", "", nil, "file system path that allow archive files for specific users, <sep><fs-path>[<sep><user>...]")
149149
serverError.CheckFatal(err)
150150

151-
err = options.AddFlagValue("archivemaxworkers", "--archive-max-workers", "", "-1", "maximum number of concurrent archive operations (-1 for unlimited)")
151+
err = options.AddFlagValue("archivemaxworkers", "--archive-max-workers", "", "0", "maximum number of concurrent archive operations (0 for unlimited)")
152152
serverError.CheckFatal(err)
153153

154154
err = options.AddFlag("globalcors", "--global-cors", "GHFS_GLOBAL_CORS", "enable CORS headers for all directories")
@@ -440,7 +440,7 @@ func CmdResultsToParams(results []*goNixArgParser.ParseResult) (params Params, e
440440
archiveDirsUsers, _ := result.GetStrings("archivedirsusers")
441441
param.ArchiveDirsUsers = SplitAllKeyValues(archiveDirsUsers)
442442

443-
param.ArchiveMaxWorkers, _ = result.GetInt32("archivemaxworkers")
443+
param.ArchiveMaxWorkers, _ = result.GetUint32("archivemaxworkers")
444444

445445
// global restrict access
446446
if result.HasKey("globalrestrictaccess") {

src/param/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type Param struct {
6262
ArchiveUrlsUsers [][]string // [][path, user...]
6363
ArchiveDirs []string
6464
ArchiveDirsUsers [][]string // [][path, user...]
65-
ArchiveMaxWorkers int32
65+
ArchiveMaxWorkers uint32
6666

6767
GlobalCors bool
6868
CorsUrls []string

src/serverHandler/aliasHandler.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ type aliasHandler struct {
5050
archive *hierarchyAvailability
5151
cors *hierarchyAvailability
5252

53-
archiveMaxWorkers int32
54-
archiveWorkers *int32
53+
archiveMaxWorkers uint32
54+
archiveWorkers *atomic.Uint32
5555

5656
globalRestrictAccess []string
5757
restrictAccessUrls pathStringsList
@@ -146,13 +146,28 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
146146

147147
func (h *aliasHandler) createArchive(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) bool {
148148
if h.archiveMaxWorkers > 0 {
149-
current := atomic.AddInt32(h.archiveWorkers, -1)
150-
defer atomic.AddInt32(h.archiveWorkers, 1)
151-
152-
if current < 0 {
153-
data.Status = http.StatusTooManyRequests
154-
return false
149+
for {
150+
current := h.archiveWorkers.Load()
151+
if current >= h.archiveMaxWorkers {
152+
data.Status = http.StatusTooManyRequests
153+
return false
154+
}
155+
if h.archiveWorkers.CompareAndSwap(current, current+1) {
156+
break
157+
}
155158
}
159+
160+
defer func() {
161+
for {
162+
current := h.archiveWorkers.Load()
163+
if current == 0 {
164+
break // prevent underflow just in case
165+
}
166+
if h.archiveWorkers.CompareAndSwap(current, current-1) {
167+
break
168+
}
169+
}
170+
}()
156171
}
157172

158173
switch session.archiveFormat {

src/serverHandler/vhostHandler.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serverHandler
33
import (
44
"net/http"
55
"regexp"
6+
"sync/atomic"
67

78
"mjpclab.dev/ghfs/src/param"
89
"mjpclab.dev/ghfs/src/serverError"
@@ -30,8 +31,8 @@ type vhostContext struct {
3031
archiveUrlsUsers pathIntsList
3132
archiveDirsUsers pathIntsList
3233

33-
archiveMaxWorkers int32
34-
archiveWorkers *int32
34+
archiveMaxWorkers uint32
35+
archiveWorkers *atomic.Uint32
3536

3637
shows *regexp.Regexp
3738
showDirs *regexp.Regexp
@@ -99,11 +100,6 @@ func NewVhostHandler(
99100
theme = defaultTheme.DefaultTheme
100101
}
101102

102-
var archiveMaxWorkers int32
103-
if p.ArchiveMaxWorkers > 0 {
104-
archiveMaxWorkers = p.ArchiveMaxWorkers
105-
}
106-
107103
// alias param
108104
vhostCtx := &vhostContext{
109105
logger: logger,
@@ -124,7 +120,7 @@ func NewVhostHandler(
124120
archiveDirsUsers: pathUsernamesToPathUids(users, p.ArchiveDirsUsers),
125121

126122
archiveMaxWorkers: p.ArchiveMaxWorkers,
127-
archiveWorkers: &archiveMaxWorkers,
123+
archiveWorkers: &atomic.Uint32{},
128124

129125
shows: shows,
130126
showDirs: showDirs,

0 commit comments

Comments
 (0)