From 4e740e000f61f7ee9107c8950aa4c02c1c3f83dd Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 2 Dec 2018 13:48:44 +0800 Subject: [PATCH] integrate with http server --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++-- README_ZH.md | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 104 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 82df2ad..77d26e7 100644 --- a/README.md +++ b/README.md @@ -101,10 +101,60 @@ func main() { } ``` +## Integrate with http server +```go +package main + +import ( + "io/ioutil" + "net/http" + + "github.com/panjf2000/ants" +) + +func main() { + result := make(chan []byte) + pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) { + param, ok := payload.([]byte) + if !ok { + param = []byte("") + } + + reverseParam := func(s []byte) []byte { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } + return s + }(param) + + result <- reverseParam + }) + defer pool.Release() + + http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) { + req, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "request error", http.StatusInternalServerError) + } + defer r.Body.Close() + + // Throttle the requests with ants pool. This process is asynchronous and + // you can receive a result from the channel defined outside. + if err := pool.Serve(req); err != nil { + http.Error(w, "throttle limit error", http.StatusInternalServerError) + } + + w.Write(<-result) + }) + + http.ListenAndServe(":8080", nil) +} +``` + ## Submit tasks Tasks can be submitted by calling `ants.Submit(func())` ```go -ants.Submit(func() error {}) +ants.Submit(func(){}) ``` ## Custom limited pool @@ -114,7 +164,7 @@ Ants also supports custom limited pool. You can use the `NewPool` method to crea // set 10000 the size of goroutine pool p, _ := ants.NewPool(10000) // submit a task -p.Submit(func() error {}) +p.Submit(func(){}) ``` ## Readjusting pool capacity diff --git a/README_ZH.md b/README_ZH.md index 3995bca..f408f53 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -98,12 +98,62 @@ func main() { fmt.Printf("running goroutines: %d\n", p.Running()) fmt.Printf("finish all tasks, result is %d\n", sum) } +``` + +## 与http server集成 +```go +package main + +import ( + "io/ioutil" + "net/http" + + "github.com/panjf2000/ants" +) + +func main() { + result := make(chan []byte) + pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) { + param, ok := payload.([]byte) + if !ok { + param = []byte("") + } + + reverseParam := func(s []byte) []byte { + for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { + s[i], s[j] = s[j], s[i] + } + return s + }(param) + + result <- reverseParam + }) + defer pool.Release() + + http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) { + req, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, "request error", http.StatusInternalServerError) + } + defer r.Body.Close() + + // Throttle the requests with ants pool. This process is asynchronous and + // you can receive a result from the channel defined outside. + if err := pool.Serve(req); err != nil { + http.Error(w, "throttle limit error", http.StatusInternalServerError) + } + + w.Write(<-result) + }) + + http.ListenAndServe(":8080", nil) +} ``` ## 任务提交 提交任务通过调用 `ants.Submit(func())`方法: ```go -ants.Submit(func() error {}) +ants.Submit(func(){}) ``` ## 自定义池 @@ -113,7 +163,7 @@ ants.Submit(func() error {}) // set 10000 the size of goroutine pool p, _ := ants.NewPool(10000) // submit a task -p.Submit(func() error {}) +p.Submit(func(){}) ``` ## 动态调整协程池容量