net

package
v4.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2025 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDownloadConcurrency = 2

DefaultDownloadConcurrency 是使用 Download() 时默认的并发数

View Source
const DefaultDownloadPartSize = utils.MB * 10

DefaultDownloadPartSize 是使用 Download() 时默认的字节范围大小

View Source
const DefaultPartBodyMaxRetries = 3

DefaultPartBodyMaxRetries 是下载分片失败时的默认重试次数

Variables

ErrExceedMaxConcurrency 表示超出最大并发数的错误

Functions

func DefaultHTTPRequestFunc added in v4.2.6

func DefaultHTTPRequestFunc(ctx context.Context, params *HTTPRequestParams) (*http.Response, error)

DefaultHTTPRequestFunc 是默认的HTTP请求函数

func GetHTTPClient added in v4.2.6

func GetHTTPClient() *http.Client

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 NewHTTPClient() *http.Client

func ProcessHeader

func ProcessHeader(origin, override http.Header) http.Header

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 Buf

type Buf struct {
	// contains filtered or unexported fields
}

Buf 是一个缓冲区

func NewBuf

func NewBuf(ctx context.Context, maxSize int) *Buf

NewBuf 创建一个新的缓冲区

func (*Buf) Close

func (br *Buf) Close()

Close 关闭缓冲区

func (*Buf) Read

func (br *Buf) Read(p []byte) (n int, err error)

Read 实现io.Reader接口

func (*Buf) Reset

func (br *Buf) Reset(size int)

Reset 重置缓冲区

func (*Buf) Write

func (br *Buf) Write(p []byte) (n int, err error)

Write 实现io.Writer接口

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

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

type HTTPRequestFunc func(ctx context.Context, params *HTTPRequestParams) (*http.Response, error)

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 是一个有限制的读取关闭器

func (*LimitedReadCloser) Close

func (l *LimitedReadCloser) Close() error

Close 实现 io.Closer 接口

func (*LimitedReadCloser) Read

func (l *LimitedReadCloser) Read(buf []byte) (int, error)

Read 实现 io.Reader 接口

type MultiReadCloser

type MultiReadCloser struct {
	// contains filtered or unexported fields
}

MultiReadCloser 是一个多重读取关闭器

func NewMultiReadCloser

func NewMultiReadCloser(buf *Buf, c closerFunc, fb finishBufFunc) *MultiReadCloser

NewMultiReadCloser 创建一个新的多重读取关闭器

func (MultiReadCloser) Close

func (mr MultiReadCloser) Close() error

Close 实现io.Closer接口

func (MultiReadCloser) Read

func (mr MultiReadCloser) Read(p []byte) (n int, err error)

Read 实现io.Reader接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL