diff --git a/README.MD b/README.MD index 6119ad65..b560a5bc 100644 --- a/README.MD +++ b/README.MD @@ -88,8 +88,8 @@ cluster_id: ${CLUSTER_ID} cluster_secret: ${CLUSTER_SECRET} # 文件同步间隔 (分钟) sync_interval: 10 -# 连接超时限制(秒),网不好就调高点 -connect_timeout: 10 +# 发送心跳包的超时限制 (秒), 网不好就调高点 +keepalive_timeout: 10 # 同步文件时最多打开的连接数量 download_max_conn: 64 # 是否启用 gzip 压缩 diff --git a/cluster.go b/cluster.go index fd4753fc..80a39bd0 100644 --- a/cluster.go +++ b/cluster.go @@ -232,7 +232,7 @@ func (cr *Cluster) Enable(ctx context.Context) (err error) { return } logInfo("Sending enable packet") - tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.ConnectTimeout)) + tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.KeepaliveTimeout)) data, err := cr.socket.EmitAckContext(tctx, "enable", Map{ "host": cr.host, "port": cr.publicPort, @@ -343,11 +343,11 @@ func (cr *Cluster) Disable(ctx context.Context) (ok bool) { logInfo("Disabling cluster") { logInfo("Making keepalive before disable") - tctx, cancel := context.WithTimeout(ctx, time.Second*10) + tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.KeepaliveTimeout)) ok = cr.KeepAlive(tctx) cancel() if ok { - tctx, cancel := context.WithTimeout(ctx, time.Second*10) + tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.KeepaliveTimeout)) data, err := cr.socket.EmitAckContext(tctx, "disable") cancel() if err != nil { @@ -589,9 +589,25 @@ func (cr *Cluster) CheckFiles(dir string, files []FileInfo, heavy bool) (missing checkThrCount int checkResCh chan *FileInfo disabled = cr.Disabled() + pollCheckSlot func() ) if heavy { checkResCh = make(chan *FileInfo, 16) + pollCheckSlot = func() { + if checkThrCount >= checkSlotLimit { + select { + case f := <-checkResCh: + if f != nil { + missing = append(missing, *f) + } + case <-disabled: + logWarn("File check interrupted") + return nil + } + } else { + checkThrCount++ + } + } } for i, f := range files { @@ -606,19 +622,7 @@ func (cr *Cluster) CheckFiles(dir string, files []FileInfo, heavy bool) (missing continue } if heavy { - if checkThrCount >= checkSlotLimit { - select { - case f := <-checkResCh: - if f != nil { - missing = append(missing, *f) - } - case <-disabled: - logWarn("File check interrupted") - return nil - } - } else { - checkThrCount++ - } + pollCheckSlot() go func(f FileInfo) { var missing *FileInfo = nil defer func() { @@ -658,19 +662,7 @@ func (cr *Cluster) CheckFiles(dir string, files []FileInfo, heavy bool) (missing p += ".gz" if _, err := os.Stat(p); err == nil { if heavy { - if checkThrCount >= checkSlotLimit { - select { - case f := <-checkResCh: - if f != nil { - missing = append(missing, *f) - } - case <-disabled: - logWarn("File check interrupted") - return nil - } - } else { - checkThrCount++ - } + pollCheckSlot() go func(f FileInfo) { var missing *FileInfo = nil defer func() { diff --git a/config.go b/config.go index 59870175..19c6f9d4 100644 --- a/config.go +++ b/config.go @@ -57,43 +57,43 @@ type HijackConfig struct { } type Config struct { - Debug bool `yaml:"debug"` - RecordServeInfo bool `yaml:"record_serve_info"` - Nohttps bool `yaml:"nohttps"` - NoOpen bool `yaml:"noopen"` - NoHeavyCheck bool `yaml:"no_heavy_check"` - PublicHost string `yaml:"public_host"` - PublicPort uint16 `yaml:"public_port"` - Port uint16 `yaml:"port"` - ClusterId string `yaml:"cluster_id"` - ClusterSecret string `yaml:"cluster_secret"` - SyncInterval int `yaml:"sync_interval"` - ConnectTimeout int `yaml:"connect_timeout"` - DownloadMaxConn int `yaml:"download_max_conn"` - UseGzip bool `yaml:"use_gzip"` - ServeLimit ServeLimitConfig `yaml:"serve_limit"` - Oss OSSConfig `yaml:"oss"` - Hijack HijackConfig `yaml:"hijack_port"` + Debug bool `yaml:"debug"` + RecordServeInfo bool `yaml:"record_serve_info"` + Nohttps bool `yaml:"nohttps"` + NoOpen bool `yaml:"noopen"` + NoHeavyCheck bool `yaml:"no_heavy_check"` + PublicHost string `yaml:"public_host"` + PublicPort uint16 `yaml:"public_port"` + Port uint16 `yaml:"port"` + ClusterId string `yaml:"cluster_id"` + ClusterSecret string `yaml:"cluster_secret"` + SyncInterval int `yaml:"sync_interval"` + KeepaliveTimeout int `yaml:"keepalive_timeout"` + DownloadMaxConn int `yaml:"download_max_conn"` + UseGzip bool `yaml:"use_gzip"` + ServeLimit ServeLimitConfig `yaml:"serve_limit"` + Oss OSSConfig `yaml:"oss"` + Hijack HijackConfig `yaml:"hijack_port"` } func readConfig() (config Config) { const configPath = "config.yaml" config = Config{ - Debug: false, - RecordServeInfo: false, - Nohttps: false, - NoOpen: false, - NoHeavyCheck: false, - PublicHost: "example.com", - PublicPort: 8080, - Port: 4000, - ClusterId: "${CLUSTER_ID}", - ClusterSecret: "${CLUSTER_SECRET}", - SyncInterval: 10, - ConnectTimeout: 10, - DownloadMaxConn: 64, - UseGzip: false, + Debug: false, + RecordServeInfo: false, + Nohttps: false, + NoOpen: false, + NoHeavyCheck: false, + PublicHost: "example.com", + PublicPort: 8080, + Port: 4000, + ClusterId: "${CLUSTER_ID}", + ClusterSecret: "${CLUSTER_SECRET}", + SyncInterval: 10, + KeepaliveTimeout: 10, + DownloadMaxConn: 64, + UseGzip: false, ServeLimit: ServeLimitConfig{ Enable: false, MaxConn: 16384, diff --git a/config.yaml b/config.yaml index ac9935f0..df68332d 100644 --- a/config.yaml +++ b/config.yaml @@ -9,7 +9,7 @@ port: 4000 cluster_id: ${CLUSTER_ID} cluster_secret: ${CLUSTER_SECRET} sync_interval: 10 -connect_timeout: 10 +enable_timeout: 10 download_max_conn: 64 use_gzip: false serve_limit: