Skip to content

Commit 5910d1f

Browse files
OlegChuev-MDmarjune163
authored andcommitted
refactor(archive): replaced channels with atomic int
1 parent 9f7e76e commit 5910d1f

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed

src/goNixArgParser/parseResult.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ 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) {
122+
str, found := r.GetString(key)
123+
if !found {
124+
return
125+
}
126+
127+
value, err := toInt32(str)
128+
found = err == nil
129+
return
130+
}
131+
121132
func (r *ParseResult) GetInt64(key string) (value int64, found bool) {
122133
str, found := r.GetString(key)
123134
if !found {

src/goNixArgParser/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ 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
87+
}
88+
8489
func toInt64(input string) (int64, error) {
8590
return strconv.ParseInt(input, 10, 64)
8691
}

src/param/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.GetInt("archivemaxworkers")
443+
param.ArchiveMaxWorkers, _ = result.GetInt32("archivemaxworkers")
444444

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

src/param/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ type Param struct {
6262
ArchiveUrlsUsers [][]string // [][path, user...]
6363
ArchiveDirs []string
6464
ArchiveDirsUsers [][]string // [][path, user...]
65-
ArchiveMaxWorkers int
66-
ArchivationsSem chan struct{}
65+
ArchiveMaxWorkers int32
6766

6867
GlobalCors bool
6968
CorsUrls []string
@@ -166,9 +165,6 @@ func (param *Param) Normalize() (errs []error) {
166165
param.DeleteDirs = NormalizeFsPaths(param.DeleteDirs)
167166
param.ArchiveUrls = NormalizeUrlPaths(param.ArchiveUrls)
168167
param.ArchiveDirs = NormalizeFsPaths(param.ArchiveDirs)
169-
if param.ArchiveMaxWorkers > 0 {
170-
param.ArchivationsSem = make(chan struct{}, param.ArchiveMaxWorkers)
171-
}
172168

173169
param.CorsUrls = NormalizeUrlPaths(param.CorsUrls)
174170
param.CorsDirs = NormalizeFsPaths(param.CorsDirs)

src/serverHandler/aliasHandler.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"regexp"
66
"strconv"
77
"strings"
8+
"sync/atomic"
89

910
"mjpclab.dev/ghfs/src/middleware"
1011
"mjpclab.dev/ghfs/src/param"
@@ -49,7 +50,8 @@ type aliasHandler struct {
4950
archive *hierarchyAvailability
5051
cors *hierarchyAvailability
5152

52-
archiveWorkers chan struct{}
53+
archiveMaxWorkers int32
54+
archiveWorkers *int32
5355

5456
globalRestrictAccess []string
5557
restrictAccessUrls pathStringsList
@@ -143,11 +145,11 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
143145
}
144146

145147
func (h *aliasHandler) createArchive(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) bool {
146-
if h.archiveWorkers != nil {
147-
select {
148-
case h.archiveWorkers <- struct{}{}:
149-
defer func() { <-h.archiveWorkers }()
150-
default:
148+
if h.archiveMaxWorkers > 0 {
149+
current := atomic.AddInt32(h.archiveWorkers, -1)
150+
defer atomic.AddInt32(h.archiveWorkers, 1)
151+
152+
if current < 0 {
151153
data.Status = http.StatusTooManyRequests
152154
return false
153155
}
@@ -192,7 +194,8 @@ func newAliasHandler(
192194
toHttpsPort: p.ToHttpsPort,
193195
defaultSort: p.DefaultSort,
194196

195-
archiveWorkers: p.ArchivationsSem,
197+
archiveMaxWorkers: vhostCtx.archiveMaxWorkers,
198+
archiveWorkers: vhostCtx.archiveWorkers,
196199

197200
users: vhostCtx.users,
198201
theme: vhostCtx.theme,

src/serverHandler/vhostHandler.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package serverHandler
22

33
import (
4+
"net/http"
5+
"regexp"
6+
47
"mjpclab.dev/ghfs/src/param"
58
"mjpclab.dev/ghfs/src/serverError"
69
"mjpclab.dev/ghfs/src/serverLog"
710
"mjpclab.dev/ghfs/src/tpl/defaultTheme"
811
"mjpclab.dev/ghfs/src/tpl/theme"
912
"mjpclab.dev/ghfs/src/user"
10-
"net/http"
11-
"regexp"
1213
)
1314

1415
type vhostContext struct {
@@ -29,6 +30,9 @@ type vhostContext struct {
2930
archiveUrlsUsers pathIntsList
3031
archiveDirsUsers pathIntsList
3132

33+
archiveMaxWorkers int32
34+
archiveWorkers *int32
35+
3236
shows *regexp.Regexp
3337
showDirs *regexp.Regexp
3438
showFiles *regexp.Regexp
@@ -95,6 +99,11 @@ func NewVhostHandler(
9599
theme = defaultTheme.DefaultTheme
96100
}
97101

102+
var archiveMaxWorkers int32
103+
if p.ArchiveMaxWorkers > 0 {
104+
archiveMaxWorkers = p.ArchiveMaxWorkers
105+
}
106+
98107
// alias param
99108
vhostCtx := &vhostContext{
100109
logger: logger,
@@ -114,6 +123,9 @@ func NewVhostHandler(
114123
archiveUrlsUsers: pathUsernamesToPathUids(users, p.ArchiveUrlsUsers),
115124
archiveDirsUsers: pathUsernamesToPathUids(users, p.ArchiveDirsUsers),
116125

126+
archiveMaxWorkers: p.ArchiveMaxWorkers,
127+
archiveWorkers: &archiveMaxWorkers,
128+
117129
shows: shows,
118130
showDirs: showDirs,
119131
showFiles: showFiles,

0 commit comments

Comments
 (0)