From f3973a522c1616718d9ea5eaf4d78b91532d5206 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Sat, 12 Nov 2022 01:17:55 +0530 Subject: [PATCH 1/3] adds example & closes #7 --- .github/workflows/build-test.yml | 5 ++- README.md | 53 +++++++++++++++++++++++++++++++- example/main.go | 37 ++++++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 example/main.go diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 2ee7a85..3f8c12b 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -21,6 +21,5 @@ jobs: - name: Test run: go test ./... - # Todo: create example folder - # - name: Build - # run: go build . \ No newline at end of file + - name: Build + run: go build ./example/main.go \ No newline at end of file diff --git a/README.md b/README.md index 3ef289e..40694c2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,54 @@ # ratelimit -A Golang rate limit implementation +[![License](https://img.shields.io/github/license/projectdiscovery/ratelimit)](LICENSE.md) +![Go version](https://img.shields.io/github/go-mod/go-version/projectdiscovery/ratelimit?filename=go.mod) +[![Release](https://img.shields.io/github/release/projectdiscovery/ratelimit)](https://github.com/projectdiscovery/ratelimit/releases/) +[![Checks](https://github.com/projectdiscovery/ratelimit/actions/workflows/build-test.yml/badge.svg)](https://github.com/projectdiscovery/ratelimit/actions/workflows/build-test.yml) +[![GoDoc](https://pkg.go.dev/badge/projectdiscovery/ratelimit)](https://pkg.go.dev/projectdiscovery/ratelimit) + +A Golang rate limit implementation which allows burst of request during the defined duration. + +## Example + +An Example showing usage of ratelimit as a library is specified below. + +```go +package main + +import ( + "context" + "fmt" + "time" + + "github.com/projectdiscovery/ratelimit" +) + +func main() { + + // create a rate limiter by passing context, max tasks/requests , time interval + limiter := ratelimit.New(context.Background(), 5, time.Duration(10*time.Second)) + + save := time.Now() + + for i := 0; i < 10; i++ { + // run limiter.Take() method before each task + limiter.Take() + fmt.Printf("Task %v completed after %v\n", i, time.Since(save)) + } + + /* + Output: + Task 0 completed after 4.083µs + Task 1 completed after 111.416µs + Task 2 completed after 118µs + Task 3 completed after 121.083µs + Task 4 completed after 124.583µs + Task 5 completed after 10.001356375s + Task 6 completed after 10.001524791s + Task 7 completed after 10.001537583s + Task 8 completed after 10.001542708s + Task 9 completed after 10.001548666s + */ +} + +``` diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..94f739c --- /dev/null +++ b/example/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + "fmt" + "time" + + "github.com/projectdiscovery/ratelimit" +) + +func main() { + + // create a rate limiter by passing context, max tasks/tokens , time interval + limiter := ratelimit.New(context.Background(), 5, time.Duration(10*time.Second)) + + save := time.Now() + + for i := 0; i < 10; i++ { + // run limiter.Take() method before each task + limiter.Take() + fmt.Printf("Task %v completed after %v\n", i, time.Since(save)) + } + + /* + Output: + Task 0 completed after 4.083µs + Task 1 completed after 111.416µs + Task 2 completed after 118µs + Task 3 completed after 121.083µs + Task 4 completed after 124.583µs + Task 5 completed after 10.001356375s + Task 6 completed after 10.001524791s + Task 7 completed after 10.001537583s + Task 8 completed after 10.001542708s + Task 9 completed after 10.001548666s + */ +} From 75cb8d0ce4ed94299c3ebd34d61e4ef0b3665e40 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Sat, 12 Nov 2022 01:29:35 +0530 Subject: [PATCH 2/3] fix godoc link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40694c2..56d717b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ![Go version](https://img.shields.io/github/go-mod/go-version/projectdiscovery/ratelimit?filename=go.mod) [![Release](https://img.shields.io/github/release/projectdiscovery/ratelimit)](https://github.com/projectdiscovery/ratelimit/releases/) [![Checks](https://github.com/projectdiscovery/ratelimit/actions/workflows/build-test.yml/badge.svg)](https://github.com/projectdiscovery/ratelimit/actions/workflows/build-test.yml) -[![GoDoc](https://pkg.go.dev/badge/projectdiscovery/ratelimit)](https://pkg.go.dev/projectdiscovery/ratelimit) +[![GoDoc](https://pkg.go.dev/badge/projectdiscovery/ratelimit)](https://pkg.go.dev/github.com/projectdiscovery/ratelimit) A Golang rate limit implementation which allows burst of request during the defined duration. From 9b8cc092ea1b933ab2b48a6ef92209fd59bc2dc0 Mon Sep 17 00:00:00 2001 From: mzack Date: Sat, 12 Nov 2022 01:57:20 +0100 Subject: [PATCH 3/3] small refactor --- .github/workflows/build-test.yml | 4 ++-- README.md | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 3f8c12b..525ad49 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -21,5 +21,5 @@ jobs: - name: Test run: go test ./... - - name: Build - run: go build ./example/main.go \ No newline at end of file + - name: Build Example + run: go build example/main.go \ No newline at end of file diff --git a/README.md b/README.md index 56d717b..2dadcb0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A Golang rate limit implementation which allows burst of request during the defi ## Example -An Example showing usage of ratelimit as a library is specified below. +An Example showing usage of ratelimit as a library is specified below: ```go package main @@ -50,5 +50,4 @@ func main() { Task 9 completed after 10.001548666s */ } - ```