gpt4 book ai didi

go - 您如何限制Cloud Run上托管的Golang服务器的速率?

转载 作者:行者123 更新时间:2023-12-01 22:14:04 25 4
gpt4 key购买 nike

由于Cloud run是“无状态”的,因此我假设状态在请求之间不会持久,因此创建IP地址映射将不起作用。还是像limiter这样的东西有用?

最佳答案

请求由请求处理程序处理,该处理程序是请求范围的,而您的限制器具有全局范围。

让我用一些代码来说明。我们有请求范围的变量i
全局范围的变量j。此外,我们有一个全局限制器。

因此,正好有一个限制器和j的实例,但是对于到达请求,将创建一个名为i的变量,该变量对于该请求是不同的。

package main

import (
"flag"
"fmt"
"log"
"net/http"
"sync/atomic"
"time"

"github.com/ulule/limiter/v3"
"github.com/ulule/limiter/v3/drivers/middleware/stdlib"
"github.com/ulule/limiter/v3/drivers/store/memory"
)

var bind string

func init() {
// Make the bind address configurable
flag.StringVar(&bind, "bind", ":9090", "address to bind to")
}

func main() {
flag.Parse()

// The rate you want to employ
// We use unusual values here for testing purposes
rate := limiter.Rate{
Period: 5 * time.Second,
Limit: 1,
}

// We use an in-memory store for the sake of simplicity.
// Furthermore, as a security measure, persistence might introduce
// an unneccessary complexity as well as a point of attack itself
// by overloading the persistence mechanism.
l := limiter.New(memory.NewStore(), rate)

middleware := stdlib.NewMiddleware(l)

// for further clarification, we add a globally scoped counter
var j uint64

// We tell the http server to take requests to /hello...
http.Handle("/hello",
// put them through or globally scoped middleware
// which will enfore the rate limit and...
middleware.Handler(

// executes the actual http.Handler if the limit is not reached.
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// request scoped...
var i int
// so i will always be 1 after increment
i++

// But we also increment our globally scoped j.
// Since multiple goroutines might access j simultaneously, we need
// to take the precaution of an atomic operation.
atomic.AddUint64(&j, 1)

w.Write([]byte(
fmt.Sprintf("Hello, world!\nrequest scoped i: %d, global scoped j:%d\n", i, atomic.LoadUint64(&j))))
})))

// Last but not least we start the server
log.Printf("Starting server bound to '%s'", bind)
log.Fatal(http.ListenAndServe(bind, nil))
}

现在,当我们运行此代码并使用curl调用URL时,我们得到一个响应(没有加入限制),并且 ij的值均为1。

$ curl -iv --no-keepalive http://localhost:9090/hello
* Trying ::1:9090...
* Connected to localhost (::1) port 9090 (#0)
> GET /hello HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.69.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< X-Ratelimit-Limit: 1
X-Ratelimit-Limit: 1
< X-Ratelimit-Remaining: 0
X-Ratelimit-Remaining: 0
< X-Ratelimit-Reset: 1588596822
X-Ratelimit-Reset: 1588596822
< Date: Mon, 04 May 2020 12:53:37 GMT
Date: Mon, 04 May 2020 12:53:37 GMT
< Content-Length: 53
Content-Length: 53
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8

<
Hello, world!
request scoped i: 1, global scoped j:1
* Connection #0 to host localhost left intact

如果我们在5秒钟内再次调用该URL,则速率限制器会启动,并拒绝我们访问:

$ curl -iv --no-keepalive http://localhost:9090/hello
* Trying ::1:9090...
* Connected to localhost (::1) port 9090 (#0)
> GET /hello HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.69.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 429 Too Many Requests
HTTP/1.1 429 Too Many Requests
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
X-Content-Type-Options: nosniff
< X-Ratelimit-Limit: 1
X-Ratelimit-Limit: 1
< X-Ratelimit-Remaining: 0
X-Ratelimit-Remaining: 0
< X-Ratelimit-Reset: 1588596822
X-Ratelimit-Reset: 1588596822
< Date: Mon, 04 May 2020 12:53:38 GMT
Date: Mon, 04 May 2020 12:53:38 GMT
< Content-Length: 15
Content-Length: 15

<
Limit exceeded
* Connection #0 to host localhost left intact

并且,在等待了几秒钟之后,我们再次调用该URL,全局范围变量将递增,而请求范围变量再次为1:

$ curl -iv --no-keepalive http://localhost:9090/hello
* Trying ::1:9090...
* Connected to localhost (::1) port 9090 (#0)
> GET /hello HTTP/1.1
> Host: localhost:9090
> User-Agent: curl/7.69.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< X-Ratelimit-Limit: 1
X-Ratelimit-Limit: 1
< X-Ratelimit-Remaining: 0
X-Ratelimit-Remaining: 0
< X-Ratelimit-Reset: 1588596884
X-Ratelimit-Reset: 1588596884
< Date: Mon, 04 May 2020 12:54:39 GMT
Date: Mon, 04 May 2020 12:54:39 GMT
< Content-Length: 53
Content-Length: 53
< Content-Type: text/plain; charset=utf-8
Content-Type: text/plain; charset=utf-8

<
Hello, world!
request scoped i: 1, global scoped j:2
* Connection #0 to host localhost left intac

关于go - 您如何限制Cloud Run上托管的Golang服务器的速率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61583486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com