Skip to content

Commit 965b1d1

Browse files
feat: use context aware semaphore instead of mutex
1 parent 171edfb commit 965b1d1

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

internal/server/handler_batch.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,12 @@ func (s *Server) batchGetPlugins(w http.ResponseWriter, r *http.Request) {
137137
batchResponse.DownloadURL = s.config.GetPublicPluginCacheDownloadURL(archiveKey)
138138

139139
// allow only one batch archive process at a time
140-
// TODO: this might be to conservative, we could allow multiple archives to be created at the same time
141-
s.batchMu.Lock()
142-
defer s.batchMu.Unlock()
140+
err = s.batchArchiveSemaphore.Acquire(r.Context(), 1)
141+
if err != nil {
142+
s.writeJSONError(w, r, http.StatusTooManyRequests, err, "could not acquire semaphore")
143+
return
144+
}
145+
defer s.batchArchiveSemaphore.Release(1)
143146

144147
headRes, err := s.storage.HeadObject(r.Context(), &s3.HeadObjectInput{
145148
Bucket: s.config.GetBucket(),

internal/server/handlers.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ func (s *Server) listPlugins(w http.ResponseWriter, r *http.Request) {
1717
}
1818

1919
func (s *Server) updateAllPlugins(w http.ResponseWriter, r *http.Request) {
20-
s.ghMutex.Lock()
21-
defer s.ghMutex.Unlock()
22-
s.log.Warn("updating all plugins")
20+
err := s.ghSemaphore.Acquire(r.Context(), 1)
21+
if err != nil {
22+
s.writeJSONError(w, r, http.StatusTooManyRequests, err, "could not acquire semaphore")
23+
return
24+
}
25+
defer s.ghSemaphore.Release(1)
26+
27+
s.log.Warn("updating all plugins...")
2328
for _, p := range config.Plugins {
2429
s.log.Infof("updating plugin %s", p.GetFullName())
2530
err := p.Update(r.Context(), s.db, s.ghClient, "")
@@ -33,8 +38,13 @@ func (s *Server) updateAllPlugins(w http.ResponseWriter, r *http.Request) {
3338
}
3439

3540
func (s *Server) updatePlugin(w http.ResponseWriter, r *http.Request) {
36-
s.ghMutex.Lock()
37-
defer s.ghMutex.Unlock()
41+
err := s.ghSemaphore.Acquire(r.Context(), 1)
42+
if err != nil {
43+
s.writeJSONError(w, r, http.StatusTooManyRequests, err, "could not acquire semaphore")
44+
return
45+
}
46+
defer s.ghSemaphore.Release(1)
47+
3848
pluginVersion := chi.URLParam(r, "version")
3949
pluginName := chi.URLParam(r, "plugin")
4050
p := config.Plugins.Find(pluginName)

internal/server/server.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package server
33
import (
44
"fmt"
55
"net/http"
6-
"sync"
76
"time"
87

98
"cloud.google.com/go/firestore"
@@ -15,18 +14,20 @@ import (
1514
"github.com/google/go-github/v50/github"
1615
"github.com/patrickmn/go-cache"
1716
"github.com/sirupsen/logrus"
17+
"golang.org/x/sync/semaphore"
1818
)
1919

2020
type Server struct {
2121
router chi.Router
2222
log *logrus.Logger
2323
db *firestore.Client
2424
ghClient *github.Client
25-
ghMutex sync.Mutex
2625
storage *s3.Client
2726
config *config.ServerConfig
2827
cache *cache.Cache
29-
batchMu sync.Mutex
28+
29+
ghSemaphore *semaphore.Weighted
30+
batchArchiveSemaphore *semaphore.Weighted
3031
}
3132

3233
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -52,13 +53,15 @@ func (s *Server) indexHandler(w http.ResponseWriter, r *http.Request) {
5253
func New(log *logrus.Logger, db *firestore.Client, ghClient *github.Client, storage *s3.Client, serverCfg *config.ServerConfig) *Server {
5354
router := chi.NewRouter()
5455
server := &Server{
55-
router: router,
56-
log: log,
57-
db: db,
58-
ghClient: ghClient,
59-
storage: storage,
60-
config: serverCfg,
61-
cache: cache.New(5*time.Minute, 10*time.Minute),
56+
router: router,
57+
log: log,
58+
db: db,
59+
ghClient: ghClient,
60+
storage: storage,
61+
config: serverCfg,
62+
cache: cache.New(5*time.Minute, 10*time.Minute),
63+
ghSemaphore: semaphore.NewWeighted(1),
64+
batchArchiveSemaphore: semaphore.NewWeighted(1),
6265
}
6366
router.Use(middleware.RequestID)
6467
router.Use(middleware.RealIP)

0 commit comments

Comments
 (0)