mirror of https://github.com/panjf2000/ants.git
add usage of integrating with http server
This commit is contained in:
commit
724458fccc
29
README.md
29
README.md
|
@ -112,39 +112,44 @@ import (
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type Request struct {
|
||||||
result := make(chan []byte)
|
Param []byte
|
||||||
pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) {
|
Result chan []byte
|
||||||
param, ok := payload.([]byte)
|
}
|
||||||
if !ok {
|
|
||||||
param = []byte("")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) {
|
||||||
|
request, ok := payload.(Request)
|
||||||
|
if !ok {
|
||||||
|
request = Request{Result: make(chan []byte)}
|
||||||
|
}
|
||||||
reverseParam := func(s []byte) []byte {
|
reverseParam := func(s []byte) []byte {
|
||||||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}(param)
|
}(request.Param)
|
||||||
|
|
||||||
result <- reverseParam
|
request.Result <- reverseParam
|
||||||
})
|
})
|
||||||
defer pool.Release()
|
defer pool.Release()
|
||||||
|
|
||||||
http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) {
|
||||||
req, err := ioutil.ReadAll(r.Body)
|
param, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "request error", http.StatusInternalServerError)
|
http.Error(w, "request error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
request := Request{Param: param, Result: make(chan []byte)}
|
||||||
|
|
||||||
// Throttle the requests with ants pool. This process is asynchronous and
|
// Throttle the requests with ants pool. This process is asynchronous and
|
||||||
// you can receive a result from the channel defined outside.
|
// you can receive a result from the channel defined outside.
|
||||||
if err := pool.Serve(req); err != nil {
|
if err := pool.Serve(request); err != nil {
|
||||||
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(<-result)
|
w.Write(<-request.Result)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
|
|
29
README_ZH.md
29
README_ZH.md
|
@ -111,39 +111,44 @@ import (
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type Request struct {
|
||||||
result := make(chan []byte)
|
Param []byte
|
||||||
pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) {
|
Result chan []byte
|
||||||
param, ok := payload.([]byte)
|
}
|
||||||
if !ok {
|
|
||||||
param = []byte("")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) {
|
||||||
|
request, ok := payload.(Request)
|
||||||
|
if !ok {
|
||||||
|
request = Request{Result: make(chan []byte)}
|
||||||
|
}
|
||||||
reverseParam := func(s []byte) []byte {
|
reverseParam := func(s []byte) []byte {
|
||||||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}(param)
|
}(request.Param)
|
||||||
|
|
||||||
result <- reverseParam
|
request.Result <- reverseParam
|
||||||
})
|
})
|
||||||
defer pool.Release()
|
defer pool.Release()
|
||||||
|
|
||||||
http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) {
|
||||||
req, err := ioutil.ReadAll(r.Body)
|
param, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "request error", http.StatusInternalServerError)
|
http.Error(w, "request error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
request := Request{Param: param, Result: make(chan []byte)}
|
||||||
|
|
||||||
// Throttle the requests with ants pool. This process is asynchronous and
|
// Throttle the requests with ants pool. This process is asynchronous and
|
||||||
// you can receive a result from the channel defined outside.
|
// you can receive a result from the channel defined outside.
|
||||||
if err := pool.Serve(req); err != nil {
|
if err := pool.Serve(request); err != nil {
|
||||||
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write(<-result)
|
w.Write(<-request.Result)
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
http.ListenAndServe(":8080", nil)
|
||||||
|
|
18
ants_test.go
18
ants_test.go
|
@ -33,14 +33,14 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_ = 1 << (10 * iota)
|
_ = 1 << (10 * iota)
|
||||||
KiB // 1024
|
KiB // 1024
|
||||||
MiB // 1048576
|
MiB // 1048576
|
||||||
GiB // 1073741824
|
GiB // 1073741824
|
||||||
TiB // 1099511627776 (超过了int32的范围)
|
TiB // 1099511627776 (超过了int32的范围)
|
||||||
PiB // 1125899906842624
|
PiB // 1125899906842624
|
||||||
EiB // 1152921504606846976
|
EiB // 1152921504606846976
|
||||||
ZiB // 1180591620717411303424 (超过了int64的范围)
|
ZiB // 1180591620717411303424 (超过了int64的范围)
|
||||||
YiB // 1208925819614629174706176
|
YiB // 1208925819614629174706176
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -93,7 +93,7 @@ func TestAntsPool(t *testing.T) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ants.Submit(func(){
|
ants.Submit(func() {
|
||||||
demoFunc()
|
demoFunc()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/panjf2000/ants"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Request struct {
|
||||||
|
Param []byte
|
||||||
|
Result chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pool, _ := ants.NewPoolWithFunc(100, func(payload interface{}) {
|
||||||
|
request, ok := payload.(Request)
|
||||||
|
if !ok {
|
||||||
|
request = Request{Result: make(chan []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
|
||||||
|
}(request.Param)
|
||||||
|
|
||||||
|
request.Result <- reverseParam
|
||||||
|
})
|
||||||
|
defer pool.Release()
|
||||||
|
|
||||||
|
http.HandleFunc("/reverse", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
param, err := ioutil.ReadAll(r.Body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "request error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
request := Request{Param: param, Result: make(chan []byte)}
|
||||||
|
|
||||||
|
// 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(request); err != nil {
|
||||||
|
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(<-request.Result)
|
||||||
|
})
|
||||||
|
|
||||||
|
http.ListenAndServe(":8080", nil)
|
||||||
|
}
|
Loading…
Reference in New Issue