Golang实现请求限流的几种办法(小结)
在开发高并发系统时,有三把利器用来保护系统:缓存、降级和限流。那么何为限流呢?顾名思义,限流就是限制流量,就像你宽带包了1个g的流量,用完了就没了。
简单的并发控制
利用 channel 的缓冲设定,我们就可以来实现并发的限制。我们只要在执行并发的同时,往一个带有缓冲的 channel 里写入点东西(随便写啥,内容不重要)。让并发的 goroutine在执行完成后把这个 channel 里的东西给读走。这样整个并发的数量就讲控制在这个 channel的缓冲区大小上。
比如我们可以用一个 bool 类型的带缓冲 channel 作为并发限制的计数器。
chlimit := make(chan bool, 1)
然后在并发执行的地方,每创建一个新的 goroutine,都往 chlimit 里塞个东西。
for i, sleeptime := range input { chs[i] = make(chan string, 1) chlimit <- true go limitfunc(chlimit, chs[i], i, sleeptime, timeout) }
这里通过 go 关键字并发执行的是新构造的函数。他在执行完后,会把 chlimit的缓冲区里给消费掉一个。
limitfunc := func(chlimit chan bool, ch chan string, task_id, sleeptime, timeout int) { run(task_id, sleeptime, timeout, ch) <-chlimit }
这样一来,当创建的 goroutine 数量到达 chlimit 的缓冲区上限后。主 goroutine 就挂起阻塞了,直到这些 goroutine 执行完毕,消费掉了 chlimit 缓冲区中的数据,程序才会继续创建新的 goroutine 。我们并发数量限制的目的也就达到了。
例子
package main import ( "fmt" "time" ) func run(task_id, sleeptime, timeout int, ch chan string) { ch_run := make(chan string) go run(task_id, sleeptime, ch_run) select { case re := <-ch_run: ch <- re case <-time.after(time.duration(timeout) * time.second): re := fmt.sprintf("task id %d , timeout", task_id) ch <- re } } func run(task_id, sleeptime int, ch chan string) { time.sleep(time.duration(sleeptime) * time.second) ch <- fmt.sprintf("task id %d , sleep %d second", task_id, sleeptime) return } func main() { input := []int{3, 2, 1} timeout := 2 chlimit := make(chan bool, 1) chs := make([]chan string, len(input)) limitfunc := func(chlimit chan bool, ch chan string, task_id, sleeptime, timeout int) { run(task_id, sleeptime, timeout, ch) <-chlimit } starttime := time.now() fmt.println("multirun start") for i, sleeptime := range input { chs[i] = make(chan string, 1) chlimit <- true go limitfunc(chlimit, chs[i], i, sleeptime, timeout) } for _, ch := range chs { fmt.println(<-ch) } endtime := time.now() fmt.printf("multissh finished. process time %s. number of task is %d", endtime.sub(starttime), len(input)) }
运行结果:
multirun start
task id 0 , timeout
task id 1 , timeout
task id 2 , sleep 1 second
multissh finished. process time 5s. number of task is 3
如果修改并发限制为2:
chlimit := make(chan bool, 2)
运行结果:
multirun start
task id 0 , timeout
task id 1 , timeout
task id 2 , sleep 1 second
multissh finished. process time 3s. number of task is 3
使用计数器实现请求限流
限流的要求是在指定的时间间隔内,server 最多只能服务指定数量的请求。实现的原理是我们启动一个计数器,每次服务请求会把计数器加一,同时到达指定的时间间隔后会把计数器清零;这个计数器的实现代码如下所示:
type requestlimitservice struct { interval time.duration maxcount int lock sync.mutex reqcount int } func newrequestlimitservice(interval time.duration, maxcnt int) *requestlimitservice { reqlimit := &requestlimitservice{ interval: interval, maxcount: maxcnt, } go func() { ticker := time.newticker(interval) for { <-ticker.c reqlimit.lock.lock() fmt.println("reset count...") reqlimit.reqcount = 0 reqlimit.lock.unlock() } }() return reqlimit } func (reqlimit *requestlimitservice) increase() { reqlimit.lock.lock() defer reqlimit.lock.unlock() reqlimit.reqcount += 1 } func (reqlimit *requestlimitservice) isavailable() bool { reqlimit.lock.lock() defer reqlimit.lock.unlock() return reqlimit.reqcount < reqlimit.maxcount }
在服务请求的时候, 我们会对当前计数器和阈值进行比较,只有未超过阈值时才进行服务:
var requestlimit = newrequestlimitservice(10 * time.second, 5) func hellohandler(w http.responsewriter, r *http.request) { if requestlimit.isavailable() { requestlimit.increase() fmt.println(requestlimit.reqcount) io.writestring(w, "hello world!\n") } else { fmt.println("reach request limiting!") io.writestring(w, "reach request limit!\n") } } func main() { fmt.println("server started!") http.handlefunc("/", hellohandler) http.listenandserve(":8000", nil) }
完整代码 url
使用golang官方包实现httpserver频率限制
使用golang来编写httpserver时,可以使用官方已经有实现好的包:
import( "fmt" "net" "golang.org/x/net/netutil" ) func main() { l, err := net.listen("tcp", "127.0.0.1:0") if err != nil { fmt.fatalf("listen: %v", err) } defer l.close() l = limitlistener(l, max) http.serve(l, http.handlerfunc()) //bla bla bla................. }
源码[url] ( ),基本思路就是为连接数计数,通过make chan来建立一个最大连接数的channel, 每次accept就+1,close时候就-1. 当到达最大连接数时,就等待空闲连接出来之后再accept。
// copyright 2013 the go authors. all rights reserved. // use of this source code is governed by a bsd-style // license that can be found in the license file. // package netutil provides network utility functions, complementing the more // common ones in the net package. package netutil // import "golang.org/x/net/netutil" import ( "net" "sync" ) // limitlistener returns a listener that accepts at most n simultaneous // connections from the provided listener. func limitlistener(l net.listener, n int) net.listener { return &limitlistener{ listener: l, sem: make(chan struct{}, n), done: make(chan struct{}), } } type limitlistener struct { net.listener sem chan struct{} closeonce sync.once // ensures the done chan is only closed once done chan struct{} // no values sent; closed when close is called } // acquire acquires the limiting semaphore. returns true if successfully // accquired, false if the listener is closed and the semaphore is not // acquired. func (l *limitlistener) acquire() bool { select { case <-l.done: return false case l.sem <- struct{}{}: return true } } func (l *limitlistener) release() { <-l.sem } func (l *limitlistener) accept() (net.conn, error) { //如果sem满了,就会阻塞在这 acquired := l.acquire() // if the semaphore isn't acquired because the listener was closed, expect // that this call to accept won't block, but immediately return an error. c, err := l.listener.accept() if err != nil { if acquired { l.release() } return nil, err } return &limitlistenerconn{conn: c, release: l.release}, nil } func (l *limitlistener) close() error { err := l.listener.close() l.closeonce.do(func() { close(l.done) }) return err } type limitlistenerconn struct { net.conn releaseonce sync.once release func() } func (l *limitlistenerconn) close() error { err := l.conn.close() //close时释放占用的sem l.releaseonce.do(l.release) return err }
使用token bucket(令牌桶算法)实现请求限流
在开发高并发系统时有三把利器用来保护系统:缓存、降级和限流!为了保证在业务高峰期,线上系统也能保证一定的弹性和稳定性,最有效的方案就是进行服务降级了,而限流就是降级系统最常采用的方案之一。
这里为大家推荐一个开源库 ,但是,如果您想要一些简单的、轻量级的或者只是想要学习的东西,实现自己的中间件来处理速率限制并不困难。今天我们就来聊聊如何实现自己的一个限流中间件
首先我们需要安装一个提供了 token bucket (令牌桶算法)的依赖包,上面提到的toolbooth 的实现也是基于它实现的:
$ go get golang.org/x/time/rate
demo代码的实现
package main import ( "net/http" "golang.org/x/time/rate" ) var limiter = rate.newlimiter(2, 5) func limit(next http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { if limiter.allow() == false { http.error(w, http.statustext(429), http.statustoomanyrequests) return } next.servehttp(w, r) }) } func main() { mux := http.newservemux() mux.handlefunc("/", okhandler) // wrap the servemux with the limit middleware. http.listenandserve(":4000", limit(mux)) } func okhandler(w http.responsewriter, r *http.request) { w.write([]byte("ok")) }
算法描述:用户配置的平均发送速率为r,则每隔1/r秒一个令牌被加入到桶中(每秒会有r个令牌放入桶中),桶中最多可以存放b个令牌。如果令牌到达时令牌桶已经满了,那么这个令牌会被丢弃;
实现
// copyright 2015 the go authors. all rights reserved. // use of this source code is governed by a bsd-style // license that can be found in the license file. // package rate provides a rate limiter. package rate import ( "fmt" "math" "sync" "time" "golang.org/x/net/context" ) // limit defines the maximum frequency of some events. // limit is represented as number of events per second. // a zero limit allows no events. type limit float64 // inf is the infinite rate limit; it allows all events (even if burst is zero). const inf = limit(math.maxfloat64) // every converts a minimum time interval between events to a limit. func every(interval time.duration) limit { if interval <= 0 { return inf } return 1 / limit(interval.seconds()) } // a limiter controls how frequently events are allowed to happen. // it implements a "token bucket" of size b, initially full and refilled // at rate r tokens per second. // informally, in any large enough time interval, the limiter limits the // rate to r tokens per second, with a maximum burst size of b events. // as a special case, if r == inf (the infinite rate), b is ignored. // see https://en.wikipedia.org/wiki/token_bucket for more about token buckets. // // the zero value is a valid limiter, but it will reject all events. // use newlimiter to create non-zero limiters. // // limiter has three main methods, allow, reserve, and wait. // most callers should use wait. // // each of the three methods consumes a single token. // they differ in their behavior when no token is available. // if no token is available, allow returns false. // if no token is available, reserve returns a reservation for a future token // and the amount of time the caller must wait before using it. // if no token is available, wait blocks until one can be obtained // or its associated context.context is canceled. // // the methods allown, reserven, and waitn consume n tokens. type limiter struct { //maximum token, token num per second limit limit //burst field, max token num burst int mu sync.mutex //tokens num, change tokens float64 // last is the last time the limiter's tokens field was updated last time.time // lastevent is the latest time of a rate-limited event (past or future) lastevent time.time } // limit returns the maximum overall event rate. func (lim *limiter) limit() limit { lim.mu.lock() defer lim.mu.unlock() return lim.limit } // burst returns the maximum burst size. burst is the maximum number of tokens // that can be consumed in a single call to allow, reserve, or wait, so higher // burst values allow more events to happen at once. // a zero burst allows no events, unless limit == inf. func (lim *limiter) burst() int { return lim.burst } // newlimiter returns a new limiter that allows events up to rate r and permits // bursts of at most b tokens. func newlimiter(r limit, b int) *limiter { return &limiter{ limit: r, burst: b, } } // allow is shorthand for allown(time.now(), 1). func (lim *limiter) allow() bool { return lim.allown(time.now(), 1) } // allown reports whether n events may happen at time now. // use this method if you intend to drop / skip events that exceed the rate limit. // otherwise use reserve or wait. func (lim *limiter) allown(now time.time, n int) bool { return lim.reserven(now, n, 0).ok } // a reservation holds information about events that are permitted by a limiter to happen after a delay. // a reservation may be canceled, which may enable the limiter to permit additional events. type reservation struct { ok bool lim *limiter tokens int //this is the time to action timetoact time.time // this is the limit at reservation time, it can change later. limit limit } // ok returns whether the limiter can provide the requested number of tokens // within the maximum wait time. if ok is false, delay returns infduration, and // cancel does nothing. func (r *reservation) ok() bool { return r.ok } // delay is shorthand for delayfrom(time.now()). func (r *reservation) delay() time.duration { return r.delayfrom(time.now()) } // infduration is the duration returned by delay when a reservation is not ok. const infduration = time.duration(1<<63 - 1) // delayfrom returns the duration for which the reservation holder must wait // before taking the reserved action. zero duration means act immediately. // infduration means the limiter cannot grant the tokens requested in this // reservation within the maximum wait time. func (r *reservation) delayfrom(now time.time) time.duration { if !r.ok { return infduration } delay := r.timetoact.sub(now) if delay < 0 { return 0 } return delay } // cancel is shorthand for cancelat(time.now()). func (r *reservation) cancel() { r.cancelat(time.now()) return } // cancelat indicates that the reservation holder will not perform the reserved action // and reverses the effects of this reservation on the rate limit as much as possible, // considering that other reservations may have already been made. func (r *reservation) cancelat(now time.time) { if !r.ok { return } r.lim.mu.lock() defer r.lim.mu.unlock() if r.lim.limit == inf || r.tokens == 0 || r.timetoact.before(now) { return } // calculate tokens to restore // the duration between lim.lastevent and r.timetoact tells us how many tokens were reserved // after r was obtained. these tokens should not be restored. restoretokens := float64(r.tokens) - r.limit.tokensfromduration(r.lim.lastevent.sub(r.timetoact)) if restoretokens <= 0 { return } // advance time to now now, _, tokens := r.lim.advance(now) // calculate new number of tokens tokens += restoretokens if burst := float64(r.lim.burst); tokens > burst { tokens = burst } // update state r.lim.last = now r.lim.tokens = tokens if r.timetoact == r.lim.lastevent { prevevent := r.timetoact.add(r.limit.durationfromtokens(float64(-r.tokens))) if !prevevent.before(now) { r.lim.lastevent = prevevent } } return } // reserve is shorthand for reserven(time.now(), 1). func (lim *limiter) reserve() *reservation { return lim.reserven(time.now(), 1) } // reserven returns a reservation that indicates how long the caller must wait before n events happen. // the limiter takes this reservation into account when allowing future events. // reserven returns false if n exceeds the limiter's burst size. // usage example: // r, ok := lim.reserven(time.now(), 1) // if !ok { // // not allowed to act! did you remember to set lim.burst to be > 0 ? // } // time.sleep(r.delay()) // act() // use this method if you wish to wait and slow down in accordance with the rate limit without dropping events. // if you need to respect a deadline or cancel the delay, use wait instead. // to drop or skip events exceeding rate limit, use allow instead. func (lim *limiter) reserven(now time.time, n int) *reservation { r := lim.reserven(now, n, infduration) return &r } // wait is shorthand for waitn(ctx, 1). func (lim *limiter) wait(ctx context.context) (err error) { return lim.waitn(ctx, 1) } // waitn blocks until lim permits n events to happen. // it returns an error if n exceeds the limiter's burst size, the context is // canceled, or the expected wait time exceeds the context's deadline. func (lim *limiter) waitn(ctx context.context, n int) (err error) { if n > lim.burst { return fmt.errorf("rate: wait(n=%d) exceeds limiter's burst %d", n, lim.burst) } // check if ctx is already cancelled select { case <-ctx.done(): return ctx.err() default: } // determine wait limit now := time.now() waitlimit := infduration if deadline, ok := ctx.deadline(); ok { waitlimit = deadline.sub(now) } // reserve r := lim.reserven(now, n, waitlimit) if !r.ok { return fmt.errorf("rate: wait(n=%d) would exceed context deadline", n) } // wait t := time.newtimer(r.delayfrom(now)) defer t.stop() select { case <-t.c: // we can proceed. return nil case <-ctx.done(): // context was canceled before we could proceed. cancel the // reservation, which may permit other events to proceed sooner. r.cancel() return ctx.err() } } // setlimit is shorthand for setlimitat(time.now(), newlimit). func (lim *limiter) setlimit(newlimit limit) { lim.setlimitat(time.now(), newlimit) } // setlimitat sets a new limit for the limiter. the new limit, and burst, may be violated // or underutilized by those which reserved (using reserve or wait) but did not yet act // before setlimitat was called. func (lim *limiter) setlimitat(now time.time, newlimit limit) { lim.mu.lock() defer lim.mu.unlock() now, _, tokens := lim.advance(now) lim.last = now lim.tokens = tokens lim.limit = newlimit } // reserven is a helper method for allown, reserven, and waitn. // maxfuturereserve specifies the maximum reservation wait duration allowed. // reserven returns reservation, not *reservation, to avoid allocation in allown and waitn. func (lim *limiter) reserven(now time.time, n int, maxfuturereserve time.duration) reservation { lim.mu.lock() defer lim.mu.unlock() if lim.limit == inf { return reservation{ ok: true, lim: lim, tokens: n, timetoact: now, } } now, last, tokens := lim.advance(now) // calculate the remaining number of tokens resulting from the request. tokens -= float64(n) // calculate the wait duration var waitduration time.duration if tokens < 0 { waitduration = lim.limit.durationfromtokens(-tokens) } // decide result ok := n <= lim.burst && waitduration <= maxfuturereserve // prepare reservation r := reservation{ ok: ok, lim: lim, limit: lim.limit, } if ok { r.tokens = n r.timetoact = now.add(waitduration) } // update state if ok { lim.last = now lim.tokens = tokens lim.lastevent = r.timetoact } else { lim.last = last } return r } // advance calculates and returns an updated state for lim resulting from the passage of time. // lim is not changed. func (lim *limiter) advance(now time.time) (newnow time.time, newlast time.time, newtokens float64) { last := lim.last if now.before(last) { last = now } // avoid making delta overflow below when last is very old. maxelapsed := lim.limit.durationfromtokens(float64(lim.burst) - lim.tokens) elapsed := now.sub(last) if elapsed > maxelapsed { elapsed = maxelapsed } // calculate the new number of tokens, due to time that passed. delta := lim.limit.tokensfromduration(elapsed) tokens := lim.tokens + delta if burst := float64(lim.burst); tokens > burst { tokens = burst } return now, last, tokens } // durationfromtokens is a unit conversion function from the number of tokens to the duration // of time it takes to accumulate them at a rate of limit tokens per second. func (limit limit) durationfromtokens(tokens float64) time.duration { seconds := tokens / float64(limit) return time.nanosecond * time.duration(1e9*seconds) } // tokensfromduration is a unit conversion function from a time duration to the number of tokens // which could be accumulated during that duration at a rate of limit tokens per second. func (limit limit) tokensfromduration(d time.duration) float64 { return d.seconds() * float64(limit) }
虽然在某些情况下使用单个全局速率限制器非常有用,但另一种常见情况是基于ip地址或api密钥等标识符为每个用户实施速率限制器。我们将使用ip地址作为标识符。简单实现代码如下:
package main import ( "net/http" "sync" "time" "golang.org/x/time/rate" ) // create a custom visitor struct which holds the rate limiter for each // visitor and the last time that the visitor was seen. type visitor struct { limiter *rate.limiter lastseen time.time } // change the the map to hold values of the type visitor. var visitors = make(map[string]*visitor) var mtx sync.mutex // run a background goroutine to remove old entries from the visitors map. func init() { go cleanupvisitors() } func addvisitor(ip string) *rate.limiter { limiter := rate.newlimiter(2, 5) mtx.lock() // include the current time when creating a new visitor. visitors[ip] = &visitor{limiter, time.now()} mtx.unlock() return limiter } func getvisitor(ip string) *rate.limiter { mtx.lock() v, exists := visitors[ip] if !exists { mtx.unlock() return addvisitor(ip) } // update the last seen time for the visitor. v.lastseen = time.now() mtx.unlock() return v.limiter } // every minute check the map for visitors that haven't been seen for // more than 3 minutes and delete the entries. func cleanupvisitors() { for { time.sleep(time.minute) mtx.lock() for ip, v := range visitors { if time.now().sub(v.lastseen) > 3*time.minute { delete(visitors, ip) } } mtx.unlock() } } func limit(next http.handler) http.handler { return http.handlerfunc(func(w http.responsewriter, r *http.request) { limiter := getvisitor(r.remoteaddr) if limiter.allow() == false { http.error(w, http.statustext(429), http.statustoomanyrequests) return } next.servehttp(w, r) }) }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: golang之数据校验的实现代码示例
下一篇: go实现整型的二进制转化的方法