Skip to content

Latest commit

 

History

History
38 lines (33 loc) · 712 Bytes

README.md

File metadata and controls

38 lines (33 loc) · 712 Bytes

raterw

基于golang.org/x/time/rate 实现的限速io.Writer,io.Reader

Writer

func main() {
	b := []byte("qweasdzxc123")
	buf := bytes.NewBuffer(nil)
	ratew := raterw.NewRateWriter(buf, 4, 4) //4byte/s 的速度写入
	n, err := ratew.Write(b)
	log.Println(n, err)
}

Reader

	b := []byte("qweasdzxc123")
	r := bytes.NewReader(b)
	reader := raterw.NewRateReader(r, 2, 2) //2byte/s 的速度读取
	var p = make([]byte, 9) 
	n, err := reader.Read(p)
	log.Println(string(p), n, err)

AddRate

import ( 
		"github.com/lixiangzhong/raterw"
		"golang.org/x/time/rate"
	)
	l := rate.NewLimiter(10, 20)
	rr := raterw.AddRateWriter(w, l)
	rw := raterw.AddRateReader(r, l)