Use efficient APIs and simplify loops

Minor refactors across the codebase to improve readability and use more efficient APIs: replace fmt.Sprintf+base64 encoding with fmt.Appendf when building Shadowsocks userInfo; compute elapsed using max(now-prev.at, window) to simplify logic; use strings.SplitSeq for splitting in two places; simplify test and goroutine loops to range-based iterations and use errgroup's Go helper; and align/clean up struct field formatting and test map literals. Mostly stylistic/efficiency changes with no intended behavior changes.
This commit is contained in:
MHSanaei
2026-06-23 14:12:28 +02:00
parent 852b53db79
commit 1c0b76c27a
8 changed files with 62 additions and 69 deletions
+4 -4
View File
@@ -16,18 +16,18 @@ func TestLogWriterLastLineConcurrent(t *testing.T) {
var wg sync.WaitGroup
wg.Add(writers + readers)
for i := 0; i < writers; i++ {
for range writers {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
for range iterations {
_, _ = lw.Write([]byte("2024/01/01 00:00:00.000000 [Info] connection accepted"))
}
}()
}
for i := 0; i < readers; i++ {
for range readers {
go func() {
defer wg.Done()
for j := 0; j < iterations; j++ {
for range iterations {
_ = lw.LastLine()
}
}()
+5 -9
View File
@@ -19,9 +19,7 @@ func TestProcessLifecycleFieldsRaceSafe(t *testing.T) {
stop := make(chan struct{})
// Writer: churn cmd/done/exitErr like Start + waitForCommand.
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for {
select {
case <-stop:
@@ -34,13 +32,11 @@ func TestProcessLifecycleFieldsRaceSafe(t *testing.T) {
p.mu.Unlock()
p.setExitErr(errors.New("boom"))
}
}()
})
// Readers: the concurrent status getters.
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range 4 {
wg.Go(func() {
for {
select {
case <-stop:
@@ -51,7 +47,7 @@ func TestProcessLifecycleFieldsRaceSafe(t *testing.T) {
_ = p.GetErr()
_ = p.GetResult()
}
}()
})
}
time.Sleep(50 * time.Millisecond)