Skip to content

Commit

Permalink
add data/stat.json
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Dec 14, 2023
1 parent c87a430 commit ee34e5d
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
/test
/pems
/cache
/data
/logs
/.tmp
/__hijack
/oss_mirror

# binarys
Expand Down
43 changes: 30 additions & 13 deletions src/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ type Cluster struct {

cacheDir string
tmpDir string
dataDir string
maxConn int
hits atomic.Int32
hbytes atomic.Int64
issync atomic.Bool

stats Stats
hits atomic.Int32
hbts atomic.Int64
issync atomic.Bool

ctx context.Context
mux sync.Mutex
Expand All @@ -55,13 +58,13 @@ type Cluster struct {
}

func NewCluster(
ctx context.Context, cacheDir string,
ctx context.Context, baseDir string,
host string, publicPort uint16,
username string, password string,
address string,
byoc bool, dialer *net.Dialer,
redirectBase string,
) (cr *Cluster) {
) (cr *Cluster, err error) {
transport := &http.Transport{}
if dialer != nil {
transport.DialContext = dialer.DialContext
Expand All @@ -79,8 +82,9 @@ func NewCluster(

redirectBase: redirectBase,

cacheDir: cacheDir,
tmpDir: filepath.Join(cacheDir, ".tmp"),
cacheDir: filepath.Join(baseDir, "cache"),
tmpDir: filepath.Join(baseDir, "cache", ".tmp"),
dataDir: filepath.Join(baseDir, "data"),
maxConn: 128,

client: &http.Client{
Expand All @@ -91,9 +95,17 @@ func NewCluster(
},
}
cr.Server.Handler = cr
os.MkdirAll(cr.cacheDir, 0755)

// create folder strcture
os.RemoveAll(cr.tmpDir)
os.Mkdir(cr.tmpDir, 0700)
os.MkdirAll(cr.cacheDir, 0755)
os.MkdirAll(cr.dataDir, 0755)
os.MkdirAll(cr.tmpDir, 0700)

// read old stats
if err = cr.stats.Load(cr.dataDir); err != nil {
return
}
return
}

Expand Down Expand Up @@ -195,13 +207,18 @@ func (cr *Cluster) Enable() (err error) {
return
}

// KeepAlive will fresh hits & hit bytes data and send the keep-alive packet
func (cr *Cluster) KeepAlive() (ok bool) {
hits, hbytes := cr.hits.Swap(0), cr.hbytes.Swap(0)
hits, hbts := cr.hits.Swap(0), cr.hbts.Swap(0)
cr.stats.AddHits(hits, hbts)
data, err := cr.socket.EmitAck("keep-alive", json.JsonObj{
"time": time.Now().UTC().Format("2006-01-02T15:04:05Z"),
"hits": hits,
"bytes": hbytes,
"bytes": hbts,
})
if e := cr.stats.Save(cr.dataDir); e != nil {
logError("Error when saving status:", e)
}
if err != nil {
logError("Error when keep-alive:", err)
return false
Expand All @@ -210,15 +227,14 @@ func (cr *Cluster) KeepAlive() (ok bool) {
logError("Keep-alive failed:", erro)
return false
}
logInfo("Keep-alive success:", hits, bytesToUnit((float64)(hbytes)), data)
logInfo("Keep-alive success:", hits, bytesToUnit((float64)(hbts)), data)
return true
}

func (cr *Cluster) Disable() (ok bool) {
cr.mux.Lock()
defer cr.mux.Unlock()

cr.KeepAlive()
if !cr.enabled {
logDebug("Extra disable")
return true
Expand All @@ -231,6 +247,7 @@ func (cr *Cluster) Disable() (ok bool) {
if cr.socket == nil {
return true
}
cr.KeepAlive()
data, err := cr.socket.EmitAck("disable")
cr.enabled = false
cr.socket.Close()
Expand Down
4 changes: 2 additions & 2 deletions src/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
http.Redirect(rw, req, target, http.StatusFound)
cr.hits.Add(1)
cr.hbytes.Add(stat.Size())
cr.hbts.Add(stat.Size())
return
}
rw.Header().Set("Cache-Control", "max-age=2592000") // 30 days
Expand All @@ -69,7 +69,7 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
counter := &countReader{ReadSeeker: fd}
http.ServeContent(rw, req, name, time.Time{}, counter)
cr.hits.Add(1)
cr.hbytes.Add(counter.n)
cr.hbts.Add(counter.n)
return
case strings.HasPrefix(rawpath, "/measure/"):
if req.Header.Get("x-openbmclapi-secret") != cr.password {
Expand Down
22 changes: 14 additions & 8 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ func readConfig() {
}
}

const cacheDir = "cache"
const baseDir = "."

var hijackPath = filepath.Join(cacheDir, "__hijack")

const ossMirrorDir = "oss_mirror"
var (
hijackPath = filepath.Join(baseDir, "__hijack")
ossMirrorDir = filepath.Join(baseDir, "oss_mirror")
)

func main() {
defer func() {
Expand Down Expand Up @@ -168,15 +169,19 @@ START:
if config.UseOss {
redirectBase = config.OssRedirectBase
}
cluster := NewCluster(ctx, cacheDir,

logInfof("Starting Go-OpenBmclApi v%s (%s)", ClusterVersion, BuildVersion)
cluster, err := NewCluster(ctx, baseDir,
config.PublicHost, config.PublicPort,
config.ClusterId, config.ClusterSecret,
fmt.Sprintf("%s:%d", "0.0.0.0", config.Port),
config.Nohttps, dialer,
redirectBase,
)

logInfof("Starting Go-OpenBmclApi v%s (%s)", ClusterVersion, BuildVersion)
if err != nil {
logError("Cannot init cluster:", err)
os.Exit(1)
}

{
logInfof("Fetching file list")
Expand Down Expand Up @@ -290,6 +295,7 @@ func createOssMirrorDir() {
logErrorf("Cannot create OSS mirror folder %q: %v", ossMirrorDir, err)
os.Exit(2)
}
cacheDir := filepath.Join(baseDir, "cache")
downloadDir := filepath.Join(ossMirrorDir, "download")
os.RemoveAll(downloadDir)
if err := os.Mkdir(downloadDir, 0755); err != nil && !errors.Is(err, os.ErrExist) {
Expand Down Expand Up @@ -361,7 +367,7 @@ func assertOSS(size int) {
os.Exit(2)
}
if n != (int64)(size)*1024*1024 {
logErrorf("OSS check request failed %q: expected 10MB, but got %d bytes", target, n)
logErrorf("OSS check request failed %q: expected %dMB, but got %d bytes", target, size, n)
os.Exit(2)
}
}
Loading

0 comments on commit ee34e5d

Please sign in to comment.