48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
//go:build linux || freebsd
|
|
// +build linux freebsd
|
|
|
|
package core
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// TimeoutConfig configures smart timeout
|
|
type TimeoutConfig struct {
|
|
Min time.Duration
|
|
Max time.Duration
|
|
MinSpeedBytesPerSec int64
|
|
SafetyFactor float64
|
|
DefaultUnknown time.Duration
|
|
}
|
|
|
|
// DefaultTimeoutConfig returns default settings
|
|
func DefaultTimeoutConfig() *TimeoutConfig {
|
|
return &TimeoutConfig{
|
|
Min: 30 * time.Second,
|
|
Max: 24 * time.Hour,
|
|
MinSpeedBytesPerSec: 10 * 1024,
|
|
SafetyFactor: 3.0,
|
|
DefaultUnknown: 30 * time.Minute,
|
|
}
|
|
}
|
|
|
|
// CalculateTimeout calculates the recommended timeout
|
|
func (tc *TimeoutConfig) CalculateTimeout(fileSize int64, userOverride time.Duration) time.Duration {
|
|
if userOverride > 0 {
|
|
return userOverride
|
|
}
|
|
if fileSize <= 0 {
|
|
return tc.DefaultUnknown
|
|
}
|
|
expectedSeconds := float64(fileSize) / float64(tc.MinSpeedBytesPerSec)
|
|
calculated := time.Duration(expectedSeconds * tc.SafetyFactor * float64(time.Second))
|
|
if calculated < tc.Min {
|
|
return tc.Min
|
|
}
|
|
if calculated > tc.Max {
|
|
return tc.Max
|
|
}
|
|
return calculated
|
|
}
|