-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
96 lines (79 loc) · 2.55 KB
/
Copy pathmain.go
File metadata and controls
96 lines (79 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"fetch-down/collector"
"fetch-down/config"
"fetch-down/controller"
"fetch-down/limiter"
"fetch-down/logger"
"fetch-down/state"
"fetch-down/stats"
)
func main() {
configPath := flag.String("config", "config.yaml", "path to config file")
flag.Parse()
cfg, err := config.Load(*configPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config: %v\n", err)
os.Exit(1)
}
logger.Init(cfg.LogLevel)
logger.Info("Starting Fetch-Down in %s mode", cfg.Mode)
coll, err := collector.New(cfg.UplinkInterface)
if err != nil {
logger.Error("Failed to initialize collector: %v", err)
os.Exit(1)
}
dlStats := stats.NewDownloadStats()
var bucket *limiter.TokenBucket
if cfg.BandwidthLimitMbps > 0 {
bpsLimit := cfg.BandwidthLimitMbps * 1000 * 1000 / 8
bucket = limiter.NewTokenBucket(bpsLimit, bpsLimit)
logger.Info("Bandwidth limit: %.1f Mbps (%.0f bytes/sec)", cfg.BandwidthLimitMbps, bpsLimit)
} else {
bucket = limiter.NewTokenBucket(0, 0)
logger.Info("Bandwidth limit: unlimited")
}
logger.Info("Config: max_concurrent=%d, urls=%d, uplink_interface=%s",
cfg.MaxConcurrent, len(cfg.DownloadURLs), coll.Interface())
if cfg.Mode == "bandwidth" {
logger.Info("Bandwidth mode: target down/up ratio = %.2f", cfg.Ratio)
} else {
logger.Info("Traffic mode: multiplier=%.2f, window=%s",
cfg.CumulativeMultiplier, cfg.WindowDuration)
}
var stateMgr *state.Manager
if cfg.StateFile != "" {
stateMgr = state.NewManager(cfg.StateFile, cfg.StateSaveIntervalParsed())
logger.Info("State persistence: file=%s, save_interval=%ds", cfg.StateFile, cfg.StateSaveInterval)
}
ctrl := controller.New(cfg, coll, dlStats, bucket, stateMgr)
if stateMgr != nil {
savedState, err := stateMgr.Load()
if err != nil {
logger.Warn("Failed to load state file: %v", err)
} else if savedState != nil {
ctrl.RestoreState(savedState)
} else {
logger.Info("No persisted state file, fresh start")
}
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigCh
logger.Info("Received signal: %v, shutting down...", sig)
ctrl.SaveAndStop()
logger.Info("=== Final Statistics ===")
logger.Info("Total downloaded: %s", dlStats.FormatBytes(dlStats.GetTotalBytes()))
logger.Info("Total requests: %d (success: %d, failed: %d)",
dlStats.GetTotalRequests(), dlStats.GetSuccessRequests(), dlStats.GetFailedRequests())
logger.Info("Uptime: %s", dlStats.Uptime())
logger.Info("=== Shutdown complete ===")
}()
ctrl.Start()
}