Skip to content

Commit

Permalink
fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 11, 2024
1 parent 24d0208 commit 48d7f64
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
13 changes: 7 additions & 6 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Cluster struct {
cancelKeepalive context.CancelFunc
downloadMux sync.Mutex
downloading map[string]chan error
fileMux sync.RWMutex
fileset map[string]int64

client *http.Client
Expand Down Expand Up @@ -402,8 +403,8 @@ func (cr *Cluster) Disabled() <-chan struct{} {
}

func (cr *Cluster) CachedFileSize(hash string) (size int64, ok bool) {
cr.mux.RLock()
defer cr.mux.RUnlock()
cr.fileMux.RLock()
defer cr.fileMux.RUnlock()
size, ok = cr.fileset[hash]
return
}
Expand Down Expand Up @@ -544,10 +545,10 @@ func (cr *Cluster) SyncFiles(ctx context.Context, files []FileInfo, heavyCheck b
for _, f := range files {
fileset[f.Hash] = f.Size
}
cr.mux.Lock()
cr.fileMux.Lock()
cr.fileset = fileset
cr.issync.Store(false)
cr.mux.Unlock()
cr.fileMux.Unlock()

go cr.gc()
}
Expand Down Expand Up @@ -959,8 +960,8 @@ func (cr *Cluster) DownloadFile(ctx context.Context, hash string) (err error) {
}
}

cr.mux.Lock()
cr.fileMux.Lock()
cr.fileset[hash] = size
cr.mux.Unlock()
cr.fileMux.Unlock()
return
}
4 changes: 4 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
http.Error(rw, "404 Not Found", http.StatusNotFound)
return
}
logDebugf("Serving download %s", hash)

query := req.URL.Query()
if !checkQuerySign(hash, cr.password, query) {
http.Error(rw, "Cannot verify signature", http.StatusForbidden)
return
}

logDebugf("Handling download %s", hash)
cr.handleDownload(rw, req, hash)
return
case strings.HasPrefix(rawpath, "/measure/"):
Expand Down Expand Up @@ -281,7 +283,9 @@ func (cr *Cluster) handleDownload(rw http.ResponseWriter, req *http.Request, has

var err error
// check if file was indexed in the fileset
logDebugf("Getting cached file size for %s", hash)
size, ok := cr.CachedFileSize(hash)
logDebugf("Cached file size for %s is %d", hash, size)
if !ok {
logInfof("Downloading %s", hash)
if err := cr.DownloadFile(req.Context(), hash); err != nil {
Expand Down
1 change: 1 addition & 0 deletions storage_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (s *MountStorage) ServeDownload(rw http.ResponseWriter, req *http.Request,
}
s.lastCheck = now
s.checkMux.Unlock()

tctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
if supportRange, err := s.checkAlive(tctx, 0); err == nil {
Expand Down

0 comments on commit 48d7f64

Please sign in to comment.