forked from mirror/ants
integrate with http server
This commit is contained in:
commit
87c91dc900
54
README.md
54
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
|
## Submit tasks
|
||||||
Tasks can be submitted by calling `ants.Submit(func())`
|
Tasks can be submitted by calling `ants.Submit(func())`
|
||||||
```go
|
```go
|
||||||
ants.Submit(func() error {})
|
ants.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## Custom limited pool
|
## 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
|
// set 10000 the size of goroutine pool
|
||||||
p, _ := ants.NewPool(10000)
|
p, _ := ants.NewPool(10000)
|
||||||
// submit a task
|
// submit a task
|
||||||
p.Submit(func() error {})
|
p.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## Readjusting pool capacity
|
## Readjusting pool capacity
|
||||||
|
|
54
README_ZH.md
54
README_ZH.md
|
@ -100,10 +100,60 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 与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())`方法:
|
提交任务通过调用 `ants.Submit(func())`方法:
|
||||||
```go
|
```go
|
||||||
ants.Submit(func() error {})
|
ants.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## 自定义池
|
## 自定义池
|
||||||
|
@ -113,7 +163,7 @@ ants.Submit(func() error {})
|
||||||
// set 10000 the size of goroutine pool
|
// set 10000 the size of goroutine pool
|
||||||
p, _ := ants.NewPool(10000)
|
p, _ := ants.NewPool(10000)
|
||||||
// submit a task
|
// submit a task
|
||||||
p.Submit(func() error {})
|
p.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## 动态调整协程池容量
|
## 动态调整协程池容量
|
||||||
|
|
Loading…
Reference in New Issue