iorate

package module
v0.0.0-...-3509f63 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2021 License: MIT Imports: 5 Imported by: 0

README

io-rate

limit io write speed

limit := NewLimiter(512)
// w write speed to 512B/s
w := NewWriter(ioutil.Discard, limit)

limit http response write speed

func download(w http.ResponseWriter, r *http.Request)
    // rw write speed to 1MB/s
    rw = iorate.NewResponseWrite(rw, 1024*1024)
    ...
}

limit http server global speed

// 1M/s speed shared by all clients
limit := NewLimiter(1024*1024)
func download(w http.ResponseWriter, r *http.Request)
    rw = iorate.NewResponseWriteByLimit(rw, limit)
    ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLimiter

func NewLimiter(r int) *rate.Limiter

NewLimiter 初始化一个限速令牌桶

func NewResponseWrite

func NewResponseWrite(w http.ResponseWriter, r int) http.ResponseWriter

NewResponseWrite 封装ResponseWriter为下载限速模式,rate为限速值,单位byte

例子 w = Newiorate(w, 100*1024) 限制客户端下载速度为100kb

func NewResponseWriteByLimit

func NewResponseWriteByLimit(w http.ResponseWriter, limit *rate.Limiter) http.ResponseWriter

NewResponseWriteByLimit 类似 NewResponseWrite 可传入limiter,便于多个ResponseWrite共用一个限速器,达到全局限速

Types

type Write

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

Write 限速的写流

func NewWriter

func NewWriter(w io.Writer, b *rate.Limiter) *Write

NewWriter 初始化一个限速的写流

func (*Write) Write

func (lw *Write) Write(data []byte) (int, error)

Write 实现writer接口

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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