Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bandwidth limit example #158

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/handler/ping.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/hertz_gen/openapi/openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/hertz_gen/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/router/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/router/user/middleware.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/biz/router/user/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bizdemo/hertz_swagger_gen/readme_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- 使用 `thrift-gen-http-swagger` 插件生成 `swagger` 文件和 `swagger ui` 服务

- `/swagger` 提供 `swagger` 文件和 `swagger ui` 服务器
- `/hander` 包含更新用户、添加用户、删除用户、查询用户的基础业务逻辑
- `/handler` 包含更新用户、添加用户、删除用户、查询用户的基础业务逻辑

## IDL

Expand Down
16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/router.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/router_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bizdemo/hertz_swagger_gen/swagger/swagger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/add_parameters/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Hertz client : add request content
You can learn about using hertz cient to add request content:
You can learn about using hertz client to add request content:
* Add Request Query
* Sending 'www-url-encoded' requests
* Sending 'multipart/form-data' requests
Expand Down
2 changes: 1 addition & 1 deletion client/streaming_read/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Hertz client : streaming read
You can learn about using hertz cient to read response with streaming.
You can learn about using hertz client to read response with streaming.
## how to run
please click [server streaming](https://github.com/cloudwego/hertz-examples/tree/main/streaming)
7 changes: 7 additions & 0 deletions middleware/bandwidth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## How to run
* start basicauth middleware server
`go run middleware/bandwidth/main.go`
* use your browser to access "http://127.0.0.1:8080/rateLimit"
## How to test
* start unit_test
`go test ./middleware/bandwidth`
57 changes: 57 additions & 0 deletions middleware/bandwidth/bandwidth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main

import (
"context"
"io/ioutil"
"net/http"
"testing"
"time"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/stretchr/testify/assert"
)

func TestRateLimitMiddleware(t *testing.T) {
h := server.Default(server.WithHostPorts("127.0.0.1:8888"))
h.Use(rateLimitMiddleware(100)) // set rate limit to 100KB/s

h.GET("/rateLimit", func(ctx context.Context, c *app.RequestContext) {
// Simulate a large response
data := make([]byte, 200*1024) // 200KB of data
c.Data(consts.StatusOK, "application/octet-stream", data)
})

go h.Spin()

time.Sleep(1 * time.Second) // Wait for the server to start

startTime := time.Now()
resp, err := http.Get("http://127.0.0.1:8888/rateLimit")
assert.NoError(t, err)
defer resp.Body.Close()

_, err = ioutil.ReadAll(resp.Body)
assert.NoError(t, err)

elapsedTime := time.Since(startTime)
expectedTime := 2 * time.Second // 200KB at 100KB/s should take 2 seconds

assert.GreaterOrEqual(t, elapsedTime, expectedTime, "Response time should be at least 2 seconds")
}
49 changes: 49 additions & 0 deletions middleware/bandwidth/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main

import (
"context"
"time"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)

func rateLimitMiddleware(rateLimitKBps int) app.HandlerFunc {
bytesPerSecond := rateLimitKBps * 1024
return func(ctx context.Context, c *app.RequestContext) {
startTime := time.Now()
c.Next(ctx)
elapsedTime := time.Since(startTime)
expectedTime := time.Duration(len(c.Response.Body()) * int(time.Second) / bytesPerSecond)
if elapsedTime < expectedTime {
time.Sleep(expectedTime - elapsedTime)
}
}
}

func main() {
h := server.Default(server.WithHostPorts("127.0.0.1:8888"))
h.Use(rateLimitMiddleware(100)) // set rate limit to 100KB/s

h.GET("/rateLimit", func(ctx context.Context, c *app.RequestContext) {
c.String(consts.StatusOK, "hello hertz")
})

h.Spin()
}