Skip to content

Commit

Permalink
rename connect_timeout -> keepalive_timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Jan 26, 2024
1 parent 6353da9 commit 05246c2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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 压缩
Expand Down
50 changes: 21 additions & 29 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
62 changes: 31 additions & 31 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 05246c2

Please sign in to comment.