Documentation ¶
Overview ¶
Package downloader 多线程下载器, 重构版
Index ¶
- Constants
- Variables
- func DefaultDURLCheckFunc(client *requester.HTTPClient, durl string) (contentLength int64, resp *http.Response, err error)
- func DefaultLoadBalancerCompareFunc(info map[string]string, subResp *http.Response) bool
- func DoDownload(durl string, savePath string, cfg *Config)
- func GetFileName(uri string, client *requester.HTTPClient) (filename string, err error)
- func GetStatusText(sc StatusCode) string
- func ParseContentRange(contentRange string) (contentLength int64)
- func RandomNumber(min, max int) int
- type ByLeftDesc
- type Config
- type DURLCheckFunc
- type DownloadFirstInfo
- type DownloadStatusFunc
- type Downloader
- func (der *Downloader) AddLoadBalanceServer(urls ...string)
- func (der *Downloader) Cancel()
- func (der *Downloader) Execute() error
- func (der *Downloader) OnCancel(onCancelEvent requester.Event)
- func (der *Downloader) OnDownloadStatusEvent(f DownloadStatusFunc)
- func (der *Downloader) OnExecute(onExecuteEvent requester.Event)
- func (der *Downloader) OnFinish(onFinishEvent requester.Event)
- func (der *Downloader) OnPause(onPauseEvent requester.Event)
- func (der *Downloader) OnResume(onResumeEvent requester.Event)
- func (der *Downloader) OnSuccess(onSuccessEvent requester.Event)
- func (der *Downloader) Pause()
- func (der *Downloader) Resume()
- func (der *Downloader) SelectBlockSizeAndInitRangeGen(single bool, status *transfer.DownloadStatus, parallel int) (blockSize int64, initErr error)
- func (der *Downloader) SelectCacheSize(confCacheSize int, blockSize int64) (cacheSize int)
- func (der *Downloader) SelectParallel(single bool, maxParallel int, totalSize int64, ...) (parallel int)
- func (der *Downloader) SetClient(client *requester.HTTPClient)
- func (der *Downloader) SetDURLCheckFunc(f DURLCheckFunc)
- func (der *Downloader) SetFirstInfo(i *DownloadFirstInfo)
- func (der *Downloader) SetLoadBalancerCompareFunc(f LoadBalancerCompareFunc)
- func (der *Downloader) SetStatusCodeBodyCheckFunc(f StatusCodeBodyCheckFunc)
- type Fder
- type InstanceState
- type InstanceStateStorageFormat
- type LoadBalancerCompareFunc
- type LoadBalancerResponse
- type LoadBalancerResponseList
- type Monitor
- func (mt *Monitor) Append(worker *Worker)
- func (mt *Monitor) CompletedChan() <-chan struct{}
- func (mt *Monitor) DynamicSplitWorker(worker *Worker)
- func (mt *Monitor) Err() error
- func (mt *Monitor) Execute(cancelCtx context.Context)
- func (mt *Monitor) GetAllWorkersRange() transfer.RangeList
- func (mt *Monitor) GetAvailableWorker() *Worker
- func (mt *Monitor) InitMonitorCapacity(capacity int)
- func (mt *Monitor) IsLeftWorkersAllFailed() bool
- func (mt *Monitor) NumLeftWorkers() (num int)
- func (mt *Monitor) Pause()
- func (mt *Monitor) RangeWorker(f RangeWorkerFunc)
- func (mt *Monitor) ResetFailedAndNetErrorWorkers()
- func (mt *Monitor) ResetWorker(worker *Worker)
- func (mt *Monitor) Resume()
- func (mt *Monitor) SetInstanceState(instanceState *InstanceState)
- func (mt *Monitor) SetReloadWorker(b bool)
- func (mt *Monitor) SetStatus(status *transfer.DownloadStatus)
- func (mt *Monitor) SetWorkers(workers WorkerList)
- func (mt *Monitor) Status() *transfer.DownloadStatus
- func (mt *Monitor) TryAddNewWork()
- type RangeWorkerFunc
- type ResetController
- type StatusCode
- type StatusCodeBodyCheckFunc
- type Worker
- func (wer *Worker) Cancel() error
- func (wer *Worker) Canceled() bool
- func (wer *Worker) ClearStatus()
- func (wer *Worker) Completed() bool
- func (wer *Worker) Err() error
- func (wer *Worker) Execute()
- func (wer *Worker) Failed() bool
- func (wer *Worker) GetRange() *transfer.Range
- func (wer *Worker) GetSpeedsPerSecond() int64
- func (wer *Worker) GetStatus() WorkerStatuser
- func (wer *Worker) ID() int
- func (wer *Worker) Pause()
- func (wer *Worker) Reset()
- func (wer *Worker) Resume()
- func (wer *Worker) SetAcceptRange(acceptRanges string)
- func (wer *Worker) SetClient(c *requester.HTTPClient)
- func (wer *Worker) SetDownloadStatus(downloadStatus *transfer.DownloadStatus)
- func (wer *Worker) SetRange(r *transfer.Range)
- func (wer *Worker) SetReferer(referer string)
- func (wer *Worker) SetTotalSize(size int64)
- func (wer *Worker) SetWriteMutex(mu *sync.Mutex)
- type WorkerList
- type WorkerStatus
- type WorkerStatuser
- type Writer
Constants ¶
const ( // InstanceStateStorageFormatJSON json 格式 InstanceStateStorageFormatJSON = iota // InstanceStateStorageFormatProto3 protobuf 格式 InstanceStateStorageFormatProto3 )
const (
//CacheSize 默认的下载缓存
CacheSize = 8192
)
const (
// DefaultAcceptRanges 默认的 Accept-Ranges
DefaultAcceptRanges = "bytes"
)
Variables ¶
var ( // ContentRangeRE Content-Range 正则 ContentRangeRE = regexp.MustCompile(`^.*? \d*?-\d*?/(\d*?)$`) )
var ( //ErrNoWokers no workers ErrNoWokers = errors.New("no workers") )
var ( // MinParallelSize 单个线程最小的数据量 MinParallelSize int64 = 128 * 1024 // 128kb )
Functions ¶
func DefaultDURLCheckFunc ¶
func DefaultDURLCheckFunc(client *requester.HTTPClient, durl string) (contentLength int64, resp *http.Response, err error)
DefaultDURLCheckFunc 默认的 DURLCheckFunc
func DefaultLoadBalancerCompareFunc ¶
DefaultLoadBalancerCompareFunc 检测负载均衡的服务器是否一致
func GetFileName ¶
func GetFileName(uri string, client *requester.HTTPClient) (filename string, err error)
GetFileName 获取文件名
func ParseContentRange ¶
ParseContentRange 解析Content-Range
Types ¶
type Config ¶
type Config struct { Mode transfer.RangeGenMode // 下载Range分配模式 MaxParallel int // 最大下载并发量 CacheSize int // 下载缓冲 BlockSize int64 // 每个Range区块的大小, RangeGenMode 为 RangeGenMode2 时才有效 MaxRate int64 // 限制最大下载速度 InstanceStateStorageFormat InstanceStateStorageFormat // 断点续传储存类型 InstanceStatePath string // 断点续传信息路径 IsTest bool // 是否测试下载 TryHTTP bool // 是否尝试使用 http 连接 }
Config 下载配置
type DURLCheckFunc ¶
type DURLCheckFunc func(client *requester.HTTPClient, durl string) (contentLength int64, resp *http.Response, err error)
DURLCheckFunc 下载URL检测函数
type DownloadFirstInfo ¶
type DownloadFirstInfo struct { ContentLength int64 ContentMD5 string ContentCRC32 string AcceptRanges string Referer string }
func NewDownloadFirstInfoByResp ¶
func NewDownloadFirstInfoByResp(contentLength int64, resp *http.Response) (dfi *DownloadFirstInfo)
func (*DownloadFirstInfo) Compare ¶
func (dfi *DownloadFirstInfo) Compare(n *DownloadFirstInfo) bool
func (*DownloadFirstInfo) ToMap ¶
func (dfi *DownloadFirstInfo) ToMap() map[string]string
ToMap 转换为map
func (*DownloadFirstInfo) ToMapByReflect ¶
func (dfi *DownloadFirstInfo) ToMapByReflect() map[string]string
ToMapByReflect 用reflect转换为map
type DownloadStatusFunc ¶
type DownloadStatusFunc func(status transfer.DownloadStatuser, workersCallback func(RangeWorkerFunc))
DownloadStatusFunc 下载状态处理函数
type Downloader ¶
type Downloader struct {
// contains filtered or unexported fields
}
Downloader 下载
func NewDownloader ¶
func NewDownloader(durl string, writer io.WriterAt, config *Config) (der *Downloader)
NewDownloader 初始化Downloader
func (*Downloader) AddLoadBalanceServer ¶
func (der *Downloader) AddLoadBalanceServer(urls ...string)
AddLoadBalanceServer 增加负载均衡服务器
func (*Downloader) OnCancel ¶
func (der *Downloader) OnCancel(onCancelEvent requester.Event)
OnCancel 设置取消下载事件
func (*Downloader) OnDownloadStatusEvent ¶
func (der *Downloader) OnDownloadStatusEvent(f DownloadStatusFunc)
OnDownloadStatusEvent 设置状态处理函数
func (*Downloader) OnExecute ¶
func (der *Downloader) OnExecute(onExecuteEvent requester.Event)
OnExecute 设置开始下载事件
func (*Downloader) OnFinish ¶
func (der *Downloader) OnFinish(onFinishEvent requester.Event)
OnFinish 设置结束下载事件
func (*Downloader) OnPause ¶
func (der *Downloader) OnPause(onPauseEvent requester.Event)
OnPause 设置暂停下载事件
func (*Downloader) OnResume ¶
func (der *Downloader) OnResume(onResumeEvent requester.Event)
OnResume 设置恢复下载事件
func (*Downloader) OnSuccess ¶
func (der *Downloader) OnSuccess(onSuccessEvent requester.Event)
OnSuccess 设置成功下载事件
func (*Downloader) SelectBlockSizeAndInitRangeGen ¶
func (der *Downloader) SelectBlockSizeAndInitRangeGen(single bool, status *transfer.DownloadStatus, parallel int) (blockSize int64, initErr error)
SelectBlockSizeAndInitRangeGen 获取合适的 BlockSize, 和初始化 RangeGen
func (*Downloader) SelectCacheSize ¶
func (der *Downloader) SelectCacheSize(confCacheSize int, blockSize int64) (cacheSize int)
SelectCacheSize 获取合适的 cacheSize
func (*Downloader) SelectParallel ¶
func (der *Downloader) SelectParallel(single bool, maxParallel int, totalSize int64, instanceRangeList transfer.RangeList) (parallel int)
SelectParallel 获取合适的 parallel
func (*Downloader) SetClient ¶
func (der *Downloader) SetClient(client *requester.HTTPClient)
SetClient 设置http客户端
func (*Downloader) SetDURLCheckFunc ¶
func (der *Downloader) SetDURLCheckFunc(f DURLCheckFunc)
SetDURLCheckFunc 设置下载URL检测函数
func (*Downloader) SetFirstInfo ¶
func (der *Downloader) SetFirstInfo(i *DownloadFirstInfo)
SetFirstInfo 设置初始信息 如果设置了此值, 将忽略检测url
func (*Downloader) SetLoadBalancerCompareFunc ¶
func (der *Downloader) SetLoadBalancerCompareFunc(f LoadBalancerCompareFunc)
SetLoadBalancerCompareFunc 设置负载均衡检测函数
func (*Downloader) SetStatusCodeBodyCheckFunc ¶
func (der *Downloader) SetStatusCodeBodyCheckFunc(f StatusCodeBodyCheckFunc)
SetStatusCodeBodyCheckFunc 设置响应状态码出错的检查函数, 当FirstCheckMethod不为HEAD时才有效
type InstanceState ¶
type InstanceState struct {
// contains filtered or unexported fields
}
InstanceState 状态, 断点续传信息
func NewInstanceState ¶
func NewInstanceState(saveFile *os.File, format InstanceStateStorageFormat) *InstanceState
NewInstanceState 初始化InstanceState
func (*InstanceState) Get ¶
func (is *InstanceState) Get() (eii *transfer.DownloadInstanceInfo)
Get 获取断点续传信息
func (*InstanceState) Put ¶
func (is *InstanceState) Put(eii *transfer.DownloadInstanceInfo)
Put 提交断点续传信息
type InstanceStateStorageFormat ¶
type InstanceStateStorageFormat int
InstanceStateStorageFormat 断点续传储存类型
type LoadBalancerCompareFunc ¶
type LoadBalancerResponse ¶
LoadBalancerResponse 负载均衡响应状态
type LoadBalancerResponseList ¶
type LoadBalancerResponseList struct {
// contains filtered or unexported fields
}
LoadBalancerResponseList 负载均衡列表
func NewLoadBalancerResponseList ¶
func NewLoadBalancerResponseList(lbr []*LoadBalancerResponse) *LoadBalancerResponseList
NewLoadBalancerResponseList 初始化负载均衡列表
func (*LoadBalancerResponseList) RandomGet ¶
func (lbrl *LoadBalancerResponseList) RandomGet() *LoadBalancerResponse
RandomGet 随机获取
func (*LoadBalancerResponseList) SequentialGet ¶
func (lbrl *LoadBalancerResponseList) SequentialGet() *LoadBalancerResponse
SequentialGet 顺序获取
type Monitor ¶
type Monitor struct {
// contains filtered or unexported fields
}
Monitor 线程监控器
func (*Monitor) CompletedChan ¶
func (mt *Monitor) CompletedChan() <-chan struct{}
CompletedChan 获取completed chan
func (*Monitor) DynamicSplitWorker ¶
DynamicSplitWorker 动态分配线程
func (*Monitor) GetAllWorkersRange ¶
GetAllWorkersRange 获取所有worker的范围
func (*Monitor) GetAvailableWorker ¶
GetAvailableWorker 获取空闲的worker
func (*Monitor) InitMonitorCapacity ¶
InitMonitorCapacity 初始化workers, 用于Append
func (*Monitor) IsLeftWorkersAllFailed ¶
IsLeftWorkersAllFailed 剩下的线程是否全部失败
func (*Monitor) NumLeftWorkers ¶
NumLeftWorkers 剩余的worker数量
func (*Monitor) RangeWorker ¶
func (mt *Monitor) RangeWorker(f RangeWorkerFunc)
RangeWorker 遍历worker
func (*Monitor) ResetFailedAndNetErrorWorkers ¶
func (mt *Monitor) ResetFailedAndNetErrorWorkers()
ResetFailedAndNetErrorWorkers 重设部分网络错误的worker
func (*Monitor) ResetWorker ¶
ResetWorker 重设长时间无响应, 和下载速度为 0 的 Worker
func (*Monitor) SetInstanceState ¶
func (mt *Monitor) SetInstanceState(instanceState *InstanceState)
SetInstanceState 设置状态
func (*Monitor) SetReloadWorker ¶
SetReloadWorker 是否重载worker
func (*Monitor) SetStatus ¶
func (mt *Monitor) SetStatus(status *transfer.DownloadStatus)
SetStatus 设置DownloadStatus
func (*Monitor) SetWorkers ¶
func (mt *Monitor) SetWorkers(workers WorkerList)
SetWorkers 设置workers, 此操作会覆盖原有的workers
func (*Monitor) Status ¶
func (mt *Monitor) Status() *transfer.DownloadStatus
Status 返回DownloadStatus
type RangeWorkerFunc ¶
RangeWorkerFunc 遍历workers的函数
type ResetController ¶
type ResetController struct {
// contains filtered or unexported fields
}
ResetController 网络连接控制器
func NewResetController ¶
func NewResetController(maxResetNum int) *ResetController
NewResetController 初始化*ResetController
type StatusCode ¶
type StatusCode int
StatusCode 状态码
const ( //StatusCodeInit 初始化 StatusCodeInit StatusCode = iota //StatusCodeSuccessed 成功 StatusCodeSuccessed //StatusCodePending 等待响应 StatusCodePending //StatusCodeDownloading 下载中 StatusCodeDownloading //StatusCodeWaitToWrite 等待写入数据 StatusCodeWaitToWrite //StatusCodeInternalError 内部错误 StatusCodeInternalError //StatusCodeTooManyConnections 连接数太多 StatusCodeTooManyConnections //StatusCodeNetError 网络错误 StatusCodeNetError //StatusCodeFailed 下载失败 StatusCodeFailed //StatusCodePaused 已暂停 StatusCodePaused //StatusCodeReseted 已重设连接 StatusCodeReseted //StatusCodeCanceled 已取消 StatusCodeCanceled )
type StatusCodeBodyCheckFunc ¶
StatusCodeBodyCheckFunc 响应状态码出错的检查函数
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker 工作单元
func (*Worker) GetSpeedsPerSecond ¶
GetSpeedsPerSecond 获取每秒的速度
func (*Worker) SetAcceptRange ¶
SetAcceptRange 设置AcceptRange
func (*Worker) SetClient ¶
func (wer *Worker) SetClient(c *requester.HTTPClient)
SetClient 设置http客户端
func (*Worker) SetDownloadStatus ¶
func (wer *Worker) SetDownloadStatus(downloadStatus *transfer.DownloadStatus)
SetDownloadStatus 增加其他需要统计的数据
func (*Worker) SetTotalSize ¶
SetTotalSize 设置整个文件的大小, worker请求range时会获取尝试获取该值, 如果不匹配, 则返回错误
func (*Worker) SetWriteMutex ¶
SetWriteMutex 设置数据写锁
type WorkerStatus ¶
type WorkerStatus struct {
// contains filtered or unexported fields
}
WorkerStatus worker状态
func (*WorkerStatus) SetStatusCode ¶
func (ws *WorkerStatus) SetStatusCode(sc StatusCode)
SetStatusCode 设置worker状态码
type WorkerStatuser ¶
type WorkerStatuser interface { StatusCode() StatusCode //状态码 StatusText() string }
WorkerStatuser 状态