Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultHTTPRequestFunc(ctx context.Context, params *HTTPRequestParams) (*http.Response, error)
- func GetHTTPClient() *http.Client
- func GetRangedHTTPReader(readCloser io.ReadCloser, offset, length int64) (io.ReadCloser, error)
- func NewHTTPClient() *http.Client
- func ProcessHeader(origin, override http.Header) http.Header
- func RequestHTTP(ctx context.Context, method string, headerOverride http.Header, URL string) (*http.Response, error)
- func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time.Time, ...) error
- type Buf
- type ConcurrencyLimit
- type Downloader
- type ErrorHTTPStatusCode
- type HTTPRequestFunc
- type HTTPRequestParams
- type LimitedReadCloser
- type MultiReadCloser
Constants ¶
const DefaultDownloadConcurrency = 2
DefaultDownloadConcurrency 是使用 Download() 时默认的并发数
const DefaultDownloadPartSize = utils.MB * 10
DefaultDownloadPartSize 是使用 Download() 时默认的字节范围大小
const DefaultPartBodyMaxRetries = 3
DefaultPartBodyMaxRetries 是下载分片失败时的默认重试次数
Variables ¶
var ErrExceedMaxConcurrency = ErrorHTTPStatusCode(http.StatusTooManyRequests)
ErrExceedMaxConcurrency 表示超出最大并发数的错误
Functions ¶
func DefaultHTTPRequestFunc ¶ added in v4.2.6
DefaultHTTPRequestFunc 是默认的HTTP请求函数
func GetHTTPClient ¶ added in v4.2.6
func GetRangedHTTPReader ¶ added in v4.2.5
func GetRangedHTTPReader(readCloser io.ReadCloser, offset, length int64) (io.ReadCloser, error)
GetRangedHTTPReader 获取指定范围的 HTTP 读取器
func NewHTTPClient ¶ added in v4.2.6
func RequestHTTP ¶ added in v4.2.5
func RequestHTTP(ctx context.Context, method string, headerOverride http.Header, URL string) (*http.Response, error)
RequestHTTP deal with Header properly then send the request
func ServeHTTP ¶
func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time.Time, size int64, rangeReadCloser model.RangeReadCloserIF) error
ServeHTTP replies to the request using the content in the provided RangeReadCloser. The main benefit of ServeHTTP is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.
If the response's Content-Type header is not set, ServeHTTP first tries to deduce the type from name's file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType. The name is otherwise unused; in particular it can be empty and is never sent in the response.
If modtime is not the zero time or Unix epoch, ServeHTTP includes it in a Last-Modified header in the response. If the request includes an If-Modified-Since header, ServeHTTP uses modtime to decide whether the content needs to be sent at all.
The content's RangeReadCloser method must work: ServeHTTP gives a range, caller will give the reader for that Range.
If the caller has set w's ETag header formatted per RFC 7232, section 2.3, ServeHTTP uses it to handle requests using If-Match, If-None-Match, or If-Range.
Types ¶
type ConcurrencyLimit ¶
type ConcurrencyLimit struct {
Limit int // 需要大于0
// contains filtered or unexported fields
}
ConcurrencyLimit 用于限制并发数
var DefaultConcurrencyLimit *ConcurrencyLimit
DefaultConcurrencyLimit 是默认的并发限制
type Downloader ¶
type Downloader struct {
// PartSize 是每个分片的大小
PartSize int
// PartBodyMaxRetries 是分片下载失败时的重试次数
PartBodyMaxRetries int
// Concurrency 是并行发送分片的goroutine数量
// 如果设置为零,将使用DefaultDownloadConcurrency值
// Concurrency为1时将按顺序下载分片
Concurrency int
// HTTPClient 是执行HTTP请求的函数
HTTPClient HTTPRequestFunc
// ConcurrencyLimit 是并发限制器
*ConcurrencyLimit
}
Downloader 是一个多线程HTTP下载器
func NewDownloader ¶
func NewDownloader(options ...func(*Downloader)) *Downloader
NewDownloader 创建一个新的下载器实例
func (Downloader) Download ¶
func (d Downloader) Download(ctx context.Context, p *HTTPRequestParams) (io.ReadCloser, error)
Download 方法使用多线程HTTP请求从远程URL下载数据 每个分块(除了最后一个)的大小为PartSize,缓存部分数据,然后返回包含组装数据的Reader 支持范围请求,不支持未知文件大小,如果文件大小不正确将会失败 内存使用量约为Concurrency*PartSize,请谨慎使用
type ErrorHTTPStatusCode ¶ added in v4.2.5
type ErrorHTTPStatusCode int
func (ErrorHTTPStatusCode) Error ¶ added in v4.2.5
func (e ErrorHTTPStatusCode) Error() string
type HTTPRequestFunc ¶ added in v4.2.6
HTTPRequestFunc 是执行HTTP请求的函数类型
func GetRangeReaderHTTPRequestFunc ¶ added in v4.2.6
func GetRangeReaderHTTPRequestFunc(rangeReader model.RangeReaderIF) HTTPRequestFunc
type HTTPRequestParams ¶ added in v4.2.6
type HTTPRequestParams struct {
URL string
Range http_range.Range // 只需要此范围内的数据
HeaderRef http.Header
Size int64 // 文件总大小
}
HTTPRequestParams 包含HTTP请求的参数
type LimitedReadCloser ¶
type LimitedReadCloser struct {
// contains filtered or unexported fields
}
LimitedReadCloser 是一个有限制的读取关闭器
type MultiReadCloser ¶
type MultiReadCloser struct {
// contains filtered or unexported fields
}
MultiReadCloser 是一个多重读取关闭器
func NewMultiReadCloser ¶
func NewMultiReadCloser(buf *Buf, c closerFunc, fb finishBufFunc) *MultiReadCloser
NewMultiReadCloser 创建一个新的多重读取关闭器