From fcc024d1fb3b697d62d109917221a624d0c58852 Mon Sep 17 00:00:00 2001 From: saixiaoxi <101516994+saixiaoxii@users.noreply.github.com> Date: Tue, 26 Nov 2024 20:25:45 +0800 Subject: [PATCH 1/4] feat: add bandwidth limit example --- middleware/bandwidth/README.md | 7 ++++ middleware/bandwidth/bandwidth_test.go | 57 ++++++++++++++++++++++++++ middleware/bandwidth/main.go | 49 ++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 middleware/bandwidth/README.md create mode 100644 middleware/bandwidth/bandwidth_test.go create mode 100644 middleware/bandwidth/main.go diff --git a/middleware/bandwidth/README.md b/middleware/bandwidth/README.md new file mode 100644 index 00000000..c9a8258d --- /dev/null +++ b/middleware/bandwidth/README.md @@ -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` \ No newline at end of file diff --git a/middleware/bandwidth/bandwidth_test.go b/middleware/bandwidth/bandwidth_test.go new file mode 100644 index 00000000..d25fa7ea --- /dev/null +++ b/middleware/bandwidth/bandwidth_test.go @@ -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") +} diff --git a/middleware/bandwidth/main.go b/middleware/bandwidth/main.go new file mode 100644 index 00000000..50117864 --- /dev/null +++ b/middleware/bandwidth/main.go @@ -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() +} From eb891315a260b6ed348f58456f93a6738e2d8215 Mon Sep 17 00:00:00 2001 From: saixiaoxi <101516994+saixiaoxii@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:41:00 +0800 Subject: [PATCH 2/4] fix: add license header --- bizdemo/hertz_swagger_gen/biz/handler/ping.go | 16 ++++++++++++++++ .../biz/handler/user/user_service.go | 16 ++++++++++++++++ .../biz/hertz_gen/openapi/openapi.go | 16 ++++++++++++++++ .../hertz_swagger_gen/biz/hertz_gen/user/user.go | 16 ++++++++++++++++ bizdemo/hertz_swagger_gen/biz/router/register.go | 16 ++++++++++++++++ .../biz/router/user/middleware.go | 16 ++++++++++++++++ .../hertz_swagger_gen/biz/router/user/user.go | 16 ++++++++++++++++ bizdemo/hertz_swagger_gen/main.go | 16 ++++++++++++++++ bizdemo/hertz_swagger_gen/router.go | 16 ++++++++++++++++ bizdemo/hertz_swagger_gen/router_gen.go | 16 ++++++++++++++++ bizdemo/hertz_swagger_gen/swagger/swagger.go | 16 ++++++++++++++++ 11 files changed, 176 insertions(+) diff --git a/bizdemo/hertz_swagger_gen/biz/handler/ping.go b/bizdemo/hertz_swagger_gen/biz/handler/ping.go index 950d4767..f1ac0222 100755 --- a/bizdemo/hertz_swagger_gen/biz/handler/ping.go +++ b/bizdemo/hertz_swagger_gen/biz/handler/ping.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. package handler diff --git a/bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go b/bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go index 28a5935d..7e4e778a 100644 --- a/bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go +++ b/bizdemo/hertz_swagger_gen/biz/handler/user/user_service.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. package user diff --git a/bizdemo/hertz_swagger_gen/biz/hertz_gen/openapi/openapi.go b/bizdemo/hertz_swagger_gen/biz/hertz_gen/openapi/openapi.go index 3ef90b3d..e42b4459 100644 --- a/bizdemo/hertz_swagger_gen/biz/hertz_gen/openapi/openapi.go +++ b/bizdemo/hertz_swagger_gen/biz/hertz_gen/openapi/openapi.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by thriftgo (0.3.14). DO NOT EDIT. package openapi diff --git a/bizdemo/hertz_swagger_gen/biz/hertz_gen/user/user.go b/bizdemo/hertz_swagger_gen/biz/hertz_gen/user/user.go index 5e52da6c..9382df6c 100644 --- a/bizdemo/hertz_swagger_gen/biz/hertz_gen/user/user.go +++ b/bizdemo/hertz_swagger_gen/biz/hertz_gen/user/user.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by thriftgo (0.3.14). DO NOT EDIT. package user diff --git a/bizdemo/hertz_swagger_gen/biz/router/register.go b/bizdemo/hertz_swagger_gen/biz/router/register.go index 442090ed..491bd6a1 100644 --- a/bizdemo/hertz_swagger_gen/biz/router/register.go +++ b/bizdemo/hertz_swagger_gen/biz/router/register.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. DO NOT EDIT. package router diff --git a/bizdemo/hertz_swagger_gen/biz/router/user/middleware.go b/bizdemo/hertz_swagger_gen/biz/router/user/middleware.go index 67abd2ca..d6109a09 100644 --- a/bizdemo/hertz_swagger_gen/biz/router/user/middleware.go +++ b/bizdemo/hertz_swagger_gen/biz/router/user/middleware.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. package user diff --git a/bizdemo/hertz_swagger_gen/biz/router/user/user.go b/bizdemo/hertz_swagger_gen/biz/router/user/user.go index 8ee640fc..83271adf 100644 --- a/bizdemo/hertz_swagger_gen/biz/router/user/user.go +++ b/bizdemo/hertz_swagger_gen/biz/router/user/user.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. DO NOT EDIT. package user diff --git a/bizdemo/hertz_swagger_gen/main.go b/bizdemo/hertz_swagger_gen/main.go index d414b90f..d0a4e6b6 100755 --- a/bizdemo/hertz_swagger_gen/main.go +++ b/bizdemo/hertz_swagger_gen/main.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. package main diff --git a/bizdemo/hertz_swagger_gen/router.go b/bizdemo/hertz_swagger_gen/router.go index 4bf2574a..802b3b53 100755 --- a/bizdemo/hertz_swagger_gen/router.go +++ b/bizdemo/hertz_swagger_gen/router.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. package main diff --git a/bizdemo/hertz_swagger_gen/router_gen.go b/bizdemo/hertz_swagger_gen/router_gen.go index cac2a5e9..ac4e9ab5 100755 --- a/bizdemo/hertz_swagger_gen/router_gen.go +++ b/bizdemo/hertz_swagger_gen/router_gen.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by hertz generator. DO NOT EDIT. package main diff --git a/bizdemo/hertz_swagger_gen/swagger/swagger.go b/bizdemo/hertz_swagger_gen/swagger/swagger.go index 684943f9..a623203c 100644 --- a/bizdemo/hertz_swagger_gen/swagger/swagger.go +++ b/bizdemo/hertz_swagger_gen/swagger/swagger.go @@ -1,3 +1,19 @@ +/* + * 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. + */ + // Code generated by thrift-gen-http-swagger. package swagger From 51c05f116a449b378653d4564e29691e00453e6e Mon Sep 17 00:00:00 2001 From: saixiaoxi <101516994+saixiaoxii@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:19:32 +0800 Subject: [PATCH 3/4] fix: hander should be handler --- bizdemo/hertz_swagger_gen/readme_cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bizdemo/hertz_swagger_gen/readme_cn.md b/bizdemo/hertz_swagger_gen/readme_cn.md index 40a0d2a9..5c3db297 100644 --- a/bizdemo/hertz_swagger_gen/readme_cn.md +++ b/bizdemo/hertz_swagger_gen/readme_cn.md @@ -10,7 +10,7 @@ - 使用 `thrift-gen-http-swagger` 插件生成 `swagger` 文件和 `swagger ui` 服务 - `/swagger` 提供 `swagger` 文件和 `swagger ui` 服务器 -- `/hander` 包含更新用户、添加用户、删除用户、查询用户的基础业务逻辑 +- `/handler` 包含更新用户、添加用户、删除用户、查询用户的基础业务逻辑 ## IDL From badfe6b16bb0d254b190c760dbfd600bd812ecf4 Mon Sep 17 00:00:00 2001 From: saixiaoxi <101516994+saixiaoxii@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:19:49 +0800 Subject: [PATCH 4/4] fix: cient should be client --- client/add_parameters/README.md | 2 +- client/streaming_read/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/add_parameters/README.md b/client/add_parameters/README.md index 651c07dc..52acda89 100644 --- a/client/add_parameters/README.md +++ b/client/add_parameters/README.md @@ -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 diff --git a/client/streaming_read/README.md b/client/streaming_read/README.md index 22f8af45..fd9ae958 100644 --- a/client/streaming_read/README.md +++ b/client/streaming_read/README.md @@ -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)