Skip to content

Commit 0b0f312

Browse files
committed
Backend: Upgrade all packages from "math/rand" to "math/rand/v2"
Signed-off-by: Michael Mayer <[email protected]>
1 parent 9540708 commit 0b0f312

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Note: Across our public documentation, official images, and in production, the c
194194

195195
- Go: run `make fmt-go swag-fmt` to reformat the backend code + Swagger annotations (see `Makefile` for additional targets)
196196
- Doc comments for packages and exported identifiers must be complete sentences that begin with the name of the thing being described and end with a period.
197+
- All newly added functions, including unexported helpers, must have a concise doc comment that explains their behavior.
197198
- For short examples inside comments, indent code rather than using backticks; godoc treats indented blocks as preformatted.
198199
- Branding: Always spell the product name as `PhotoPrism`; this proper noun is an exception to generic naming rules.
199200
- Every Go package must contain a `<package>.go` file in its root (for example, `internal/auth/jwt/jwt.go`) with the standard license header and a short package description comment explaining its purpose.

internal/ai/tensorflow/util.go

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

3-
import "math/rand"
3+
import "math/rand/v2"
44

55
func randomString(length int) string {
66
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
77
result := make([]byte, length)
88
for i := range result {
9-
result[i] = charset[rand.Intn(len(charset))]
9+
result[i] = charset[rand.IntN(len(charset))]
1010
}
1111
return string(result)
1212
}

internal/auth/jwt/verifier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"encoding/json"
88
"errors"
99
"fmt"
10-
"math/rand"
10+
"math/rand/v2"
1111
"net/http"
1212
"os"
1313
"path/filepath"
@@ -49,7 +49,7 @@ const (
4949
)
5050

5151
// randInt63n is defined for deterministic testing of jitter (overridable in tests).
52-
var randInt63n = rand.Int63n
52+
var randInt63n = rand.Int64N
5353

5454
// cacheEntry stores the JWKS material cached on disk and in memory.
5555
type cacheEntry struct {

internal/config/config.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ Additional information can be found in our Developer Guide:
2525
package config
2626

2727
import (
28+
"context"
2829
"crypto/tls"
2930
"fmt"
31+
"math/rand/v2"
3032
"net/http"
3133
"os"
3234
"path/filepath"
@@ -75,6 +77,8 @@ type Config struct {
7577
db *gorm.DB
7678
dbVersion string
7779
hub *hub.Config
80+
hubCancel context.CancelFunc
81+
hubLock sync.Mutex
7882
token string
7983
serial string
8084
env string
@@ -622,8 +626,22 @@ func (c *Config) SetLogLevel(level logrus.Level) {
622626
SetLogLevel(level)
623627
}
624628

629+
// stopHubTicker stops the periodic hub renewal ticker if it is running.
630+
func (c *Config) stopHubTicker() {
631+
c.hubLock.Lock()
632+
cancel := c.hubCancel
633+
c.hubCancel = nil
634+
c.hubLock.Unlock()
635+
636+
if cancel != nil {
637+
cancel()
638+
}
639+
}
640+
625641
// Shutdown shuts down the active processes and closes the database connection.
626642
func (c *Config) Shutdown() {
643+
c.stopHubTicker()
644+
627645
// App is no longer accepting requests.
628646
c.ready.Store(false)
629647

@@ -820,13 +838,24 @@ func (c *Config) initHub() {
820838

821839
c.hub.Propagate()
822840

823-
ticker := time.NewTicker(time.Hour * 24)
841+
ctx, cancel := context.WithCancel(context.Background())
842+
843+
c.hubLock.Lock()
844+
c.hubCancel = cancel
845+
c.hubLock.Unlock()
846+
847+
d := 23*time.Hour + time.Duration(float64(2*time.Hour)*rand.Float64())
848+
ticker := time.NewTicker(d)
824849

825850
go func() {
851+
defer ticker.Stop()
852+
826853
for {
827854
select {
828855
case <-ticker.C:
829856
c.RenewApiKeys()
857+
case <-ctx.Done():
858+
return
830859
}
831860
}
832861
}()

0 commit comments

Comments
 (0)