Documentation ¶
Overview ¶
Package downloader 多线程下载器, 重构版
Index ¶
- Constants
- Variables
- 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 RandomNumber(min, max int) int
- func ServerEqual(resp, subResp *http.Response) bool
- type Config
- type DlStatus
- type DownloadStatus
- func (ds *DownloadStatus) Add(i int64)
- func (ds *DownloadStatus) AddDownloaded(d int64)
- func (ds *DownloadStatus) AddSpeedsDownloaded(d int64)
- func (ds *DownloadStatus) Downloaded() int64
- func (ds *DownloadStatus) MaxSpeeds() int64
- func (ds *DownloadStatus) ResetMaxSpeeds()
- func (ds *DownloadStatus) SpeedsDownloaded() int64
- func (ds *DownloadStatus) SpeedsPerSecond() int64
- func (ds *DownloadStatus) StoreMaxSpeeds(speeds int64)
- func (ds *DownloadStatus) StoreSpeedsPerSecond(speeds int64)
- func (ds *DownloadStatus) TimeElapsed() time.Duration
- func (ds *DownloadStatus) TotalSize() int64
- type Downloader
- func (der *Downloader) AddLoadBalanceServer(urls ...string)
- func (der *Downloader) Cancel()
- func (der *Downloader) Execute() error
- func (der *Downloader) GetDownloadStatusChan() <-chan DlStatus
- func (der *Downloader) OnCancel(onCancelEvent Event)
- func (der *Downloader) OnExecute(onExecuteEvent Event)
- func (der *Downloader) OnFinish(onFinishEvent Event)
- func (der *Downloader) OnPause(onPauseEvent Event)
- func (der *Downloader) OnResume(onResumeEvent Event)
- func (der *Downloader) OnSuccess(onSuccessEvent Event)
- func (der *Downloader) Pause()
- func (der *Downloader) PrintAllWorkers()
- func (der *Downloader) Resume()
- func (der *Downloader) SetClient(client *requester.HTTPClient)
- func (der *Downloader) TryHTTP(t bool)
- type Event
- type InstanceInfo
- type InstanceState
- type LoadBalancerResponse
- type LoadBalancerResponseList
- type Monitor
- func (mt *Monitor) AllCompleted() <-chan struct{}
- func (mt *Monitor) Append(worker *Worker)
- func (mt *Monitor) CompletedChan() <-chan struct{}
- func (mt *Monitor) Err() error
- func (mt *Monitor) Execute(cancelCtx context.Context)
- func (mt *Monitor) GetAllWorkersRange() (ranges []*Range)
- func (mt *Monitor) GetAvaliableWorker() *Worker
- func (mt *Monitor) GetSpeedsPerSecondFunc() func() int64
- 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 func(key int, worker *Worker) bool)
- func (mt *Monitor) ResetAllFailedAndNetErrorWorkers()
- func (mt *Monitor) Resume()
- func (mt *Monitor) SetInstanceState(instanceState *InstanceState)
- func (mt *Monitor) SetReloadWorker(b bool)
- func (mt *Monitor) SetStatus(status *DownloadStatus)
- func (mt *Monitor) SetWorkers(workers []*Worker)
- func (mt *Monitor) ShowWorkers() string
- func (mt *Monitor) Status() *DownloadStatus
- type Range
- type RangeList
- type Status
- type StatusCode
- type Worker
- func (wer *Worker) Cancel() error
- func (wer *Worker) Canceled() bool
- func (wer *Worker) CleanStatus()
- func (wer *Worker) Completed() bool
- func (wer *Worker) Err() error
- func (wer *Worker) Execute()
- func (wer *Worker) Failed() bool
- func (wer *Worker) GetRange() *Range
- func (wer *Worker) GetSpeedsPerSecond() int64
- func (wer *Worker) GetStatus() Status
- func (wer *Worker) ID() int
- func (wer *Worker) Pause()
- func (wer *Worker) Reset()
- func (wer *Worker) Resume()
- func (wer *Worker) SetCacheSize(size int)
- func (wer *Worker) SetClient(c *requester.HTTPClient)
- func (wer *Worker) SetDownloadStatus(downloadStatus *DownloadStatus)
- func (wer *Worker) SetRange(acceptRanges string, r Range)
- func (wer *Worker) SetReferer(referer string)
- func (wer *Worker) SetWriteMutex(mu *sync.Mutex)
- type WorkerStatus
Constants ¶
const (
//CacheSize 默认的下载缓存
CacheSize = 8192
)
Variables ¶
var ( //ErrNoWokers no workers ErrNoWokers = errors.New("no workers") )
var ( // MinParallelSize 单个线程最小的数据量 MinParallelSize int64 = 128 * 1024 // 128kb )
Functions ¶
func GetFileName ¶
func GetFileName(uri string, client *requester.HTTPClient) (filename string, err error)
GetFileName 获取文件名
Types ¶
type Config ¶
type Config struct { MaxParallel int // 最大下载并发量 CacheSize int // 下载缓冲 InstanceStatePath string // 断点续传信息路径 IsTest bool // 是否测试下载 // contains filtered or unexported fields }
Config 下载配置
type DlStatus ¶
type DlStatus interface { TotalSize() int64 Downloaded() int64 SpeedsPerSecond() int64 TimeElapsed() time.Duration }
DlStatus 下载状态接口
type DownloadStatus ¶
type DownloadStatus struct {
// contains filtered or unexported fields
}
DownloadStatus 下载状态及统计信息
func NewDownloadStatus ¶
func NewDownloadStatus() *DownloadStatus
NewDownloadStatus 初始化DownloadStatus
func (*DownloadStatus) AddDownloaded ¶
func (ds *DownloadStatus) AddDownloaded(d int64)
AddDownloaded 增加已下载数据量
func (*DownloadStatus) AddSpeedsDownloaded ¶
func (ds *DownloadStatus) AddSpeedsDownloaded(d int64)
AddSpeedsDownloaded 增加已下载数据量, 用于统计速度
func (*DownloadStatus) Downloaded ¶
func (ds *DownloadStatus) Downloaded() int64
Downloaded 返回已下载数据量
func (*DownloadStatus) ResetMaxSpeeds ¶
func (ds *DownloadStatus) ResetMaxSpeeds()
ResetMaxSpeeds 清空最大速度统计
func (*DownloadStatus) SpeedsDownloaded ¶
func (ds *DownloadStatus) SpeedsDownloaded() int64
SpeedsDownloaded 返回用于统计速度的已下载数据量
func (*DownloadStatus) SpeedsPerSecond ¶
func (ds *DownloadStatus) SpeedsPerSecond() int64
SpeedsPerSecond 返回每秒速度
func (*DownloadStatus) StoreMaxSpeeds ¶
func (ds *DownloadStatus) StoreMaxSpeeds(speeds int64)
StoreMaxSpeeds 储存最大速度, 原子操作
func (*DownloadStatus) StoreSpeedsPerSecond ¶
func (ds *DownloadStatus) StoreSpeedsPerSecond(speeds int64)
StoreSpeedsPerSecond 储存速度, 原子操作
func (*DownloadStatus) TimeElapsed ¶
func (ds *DownloadStatus) TimeElapsed() time.Duration
TimeElapsed 返回花费的时间
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) GetDownloadStatusChan ¶
func (der *Downloader) GetDownloadStatusChan() <-chan DlStatus
GetDownloadStatusChan 获取下载统计信息
func (*Downloader) OnCancel ¶
func (der *Downloader) OnCancel(onCancelEvent Event)
OnCancel 设置取消下载事件
func (*Downloader) OnExecute ¶
func (der *Downloader) OnExecute(onExecuteEvent Event)
OnExecute 设置开始下载事件
func (*Downloader) OnFinish ¶
func (der *Downloader) OnFinish(onFinishEvent Event)
OnFinish 设置结束下载事件
func (*Downloader) OnResume ¶
func (der *Downloader) OnResume(onResumeEvent Event)
OnResume 设置恢复下载事件
func (*Downloader) OnSuccess ¶
func (der *Downloader) OnSuccess(onSuccessEvent Event)
OnSuccess 设置成功下载事件
func (*Downloader) PrintAllWorkers ¶
func (der *Downloader) PrintAllWorkers()
PrintAllWorkers 输出所有的worker
func (*Downloader) SetClient ¶
func (der *Downloader) SetClient(client *requester.HTTPClient)
SetClient 设置http客户端
type InstanceInfo ¶
type InstanceInfo struct { DlStatus *DownloadStatus Ranges RangeList }
InstanceInfo 状态详细信息, 用于导出状态文件
type InstanceState ¶
type InstanceState struct {
// contains filtered or unexported fields
}
InstanceState 状态, 断点续传信息
func NewInstanceState ¶
func NewInstanceState(saveFile *os.File) *InstanceState
NewInstanceState 初始化InstanceState
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) AllCompleted ¶
func (mt *Monitor) AllCompleted() <-chan struct{}
AllCompleted 全部完成则发送消息
func (*Monitor) CompletedChan ¶
func (mt *Monitor) CompletedChan() <-chan struct{}
CompletedChan 获取completed chan
func (*Monitor) GetAllWorkersRange ¶
GetAllWorkersRange 获取所以worker的范围
func (*Monitor) GetAvaliableWorker ¶
GetAvaliableWorker 获取空闲的worker
func (*Monitor) GetSpeedsPerSecondFunc ¶
GetSpeedsPerSecondFunc 获取每秒的速度, 返回获取速度的函数
func (*Monitor) InitMonitorCapacity ¶
InitMonitorCapacity 初始化workers, 用于Append
func (*Monitor) IsLeftWorkersAllFailed ¶
IsLeftWorkersAllFailed 剩下的线程是否全部失败
func (*Monitor) NumLeftWorkers ¶
NumLeftWorkers 剩余的worker数量
func (*Monitor) RangeWorker ¶
RangeWorker 遍历worker
func (*Monitor) ResetAllFailedAndNetErrorWorkers ¶
func (mt *Monitor) ResetAllFailedAndNetErrorWorkers()
ResetAllFailedAndNetErrorWorkers 重设所有网络错误的worker
func (*Monitor) SetInstanceState ¶
func (mt *Monitor) SetInstanceState(instanceState *InstanceState)
SetInstanceState 设置状态
func (*Monitor) SetReloadWorker ¶
SetReloadWorker 是否重载worker
func (*Monitor) SetStatus ¶
func (mt *Monitor) SetStatus(status *DownloadStatus)
SetStatus 设置DownloadStatus
func (*Monitor) SetWorkers ¶
SetWorkers 设置workers, 此操作会覆盖原有的workers
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 Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker 工作单元
func (*Worker) GetSpeedsPerSecond ¶
GetSpeedsPerSecond 获取每秒的速度
func (*Worker) SetClient ¶
func (wer *Worker) SetClient(c *requester.HTTPClient)
SetClient 设置http客户端
func (*Worker) SetDownloadStatus ¶
func (wer *Worker) SetDownloadStatus(downloadStatus *DownloadStatus)
SetDownloadStatus 增加其他需要统计的数据
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状态码