Skip to content

Commit

Permalink
quick serve for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jan 22, 2024
1 parent fb5f88c commit 7696844
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
15 changes: 12 additions & 3 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func NewCluster(
return
}

func (cr *Cluster) usedOSS() bool {
return cr.ossList != nil
}

func (cr *Cluster) Connect(ctx context.Context) bool {
cr.mux.Lock()
defer cr.mux.Unlock()
Expand Down Expand Up @@ -455,14 +459,14 @@ func (cr *Cluster) SyncFiles(ctx context.Context, files0 []FileInfo) {
return
}

if cr.ossList == nil {
cr.syncFiles(ctx, cr.cacheDir, files0)
} else {
if cr.usedOSS() {
for _, item := range cr.ossList {
if err := cr.syncFiles(ctx, filepath.Join(item.FolderPath, "download"), files0); err != nil {
break
}
}
} else {
cr.syncFiles(ctx, cr.cacheDir, files0)
}

cr.issync.Store(false)
Expand Down Expand Up @@ -518,9 +522,14 @@ func (cr *Cluster) syncFiles(ctx context.Context, dir string, files0 []FileInfo)

func (cr *Cluster) CheckFiles(dir string, files []FileInfo) (missing []FileInfo) {
logInfof("Start checking files at %q", dir)
usedOSS := cr.usedOSS()
for i, f := range files {
p := filepath.Join(dir, hashToFilename(f.Hash))
logDebugf("Checking file %s [%.2f%%]", p, (float32)(i+1)/(float32)(len(files))*100)
if usedOSS && f.Size == 0 {
logDebugf("Skipped empty file %s", p)
continue
}
stat, err := os.Stat(p)
if err == nil {
if sz := stat.Size(); sz != f.Size {
Expand Down
25 changes: 25 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package main

import (
"crypto"
"encoding/hex"
"errors"
"io"
"net"
Expand Down Expand Up @@ -143,6 +145,18 @@ func (cr *Cluster) GetHandler() (handler http.Handler) {
return
}

var emptyHashes = func() (hashes map[string]struct{}) {
hashMethods := []crypto.Hash{
crypto.MD5, crypto.SHA1,
}
hashes = make(map[string]struct{}, len(hashMethods))
for _, h := range hashMethods {
hs := hex.EncodeToString(h.New().Sum(nil))
hashes[hs] = struct{}{}
}
return
}()

func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
method := req.Method
u := req.URL
Expand All @@ -161,6 +175,17 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
name := req.Form.Get("name")

if _, ok := emptyHashes[hash]; ok {
rw.Header().Set("Cache-Control", "max-age=2592000") // 30 days
rw.Header().Set("Content-Type", "application/octet-stream")
rw.Header().Set("X-Bmclapi-Hash", hash)
rw.WriteHeader(http.StatusOK)
http.ServeContent(rw, req, name, time.Time{}, NullReader)
cr.hits.Add(1)
// cr.hbts.Add(0) // empty bytes
return
}

hashFilename := hashToFilename(hash)

// if use OSS redirect
Expand Down
14 changes: 14 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"math/rand"
"path/filepath"
"strings"
Expand Down Expand Up @@ -180,3 +181,16 @@ func forEachSliceFromRandomIndex(leng int, cb func(i int) (done bool)) (done boo
}
return false
}

type nullReader struct{}

var (
NullReader = nullReader{}

_ io.ReaderAt = NullReader
_ io.ReadSeeker = NullReader
)

func (nullReader) Read([]byte) (int, error) { return 0, io.EOF }
func (nullReader) ReadAt([]byte, int64) (int, error) { return 0, io.EOF }
func (nullReader) Seek(int64, int) (int64, error) { return 0, nil }

0 comments on commit 7696844

Please sign in to comment.