add task
This commit is contained in:
parent
2fcdc91587
commit
489bc792e1
|
@ -1,60 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.tuxpa.in/a/lambda"
|
||||
"git.tuxpa.in/a/lambda/parallel"
|
||||
)
|
||||
|
||||
type RequestContext[T any] struct {
|
||||
req http.Request
|
||||
ans T
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *RequestContext[T]) String() string {
|
||||
return fmt.Sprintf("%+v", r.ans)
|
||||
}
|
||||
|
||||
func (_ *RequestContext[T]) Do() func(r *RequestContext[T]) *RequestContext[T] {
|
||||
return func(r *RequestContext[T]) *RequestContext[T] {
|
||||
r.ans = *new(T)
|
||||
res, err := http.DefaultClient.Do(&r.req)
|
||||
if err != nil {
|
||||
r.err = err
|
||||
return r
|
||||
}
|
||||
defer res.Body.Close()
|
||||
r.err = json.NewDecoder(res.Body).Decode(&r.ans)
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
func (_ *RequestContext[T]) FilterErr() func(r *RequestContext[T]) bool {
|
||||
return func(r *RequestContext[T]) bool {
|
||||
return r.err == nil
|
||||
}
|
||||
}
|
||||
|
||||
type someResult struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var requests = make([]*RequestContext[someResult], 20)
|
||||
for i := range requests {
|
||||
r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i+1), nil)
|
||||
requests[i] = &RequestContext[someResult]{
|
||||
req: *r,
|
||||
}
|
||||
}
|
||||
results := lambda.Filter(parallel.Map(requests, requests[0].Do(), 4), requests[0].FilterErr())
|
||||
for _, v := range results {
|
||||
log.Printf("%s", v)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.tuxpa.in/a/lambda"
|
||||
"git.tuxpa.in/a/lambda/parallel"
|
||||
"git.tuxpa.in/a/lambda/task"
|
||||
)
|
||||
|
||||
type someResult struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var requests = make([]*task.HttpRequest[someResult], 20)
|
||||
for i := range requests {
|
||||
r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i+1), nil)
|
||||
requests[i] = &task.HttpRequest[someResult]{
|
||||
Req: *r,
|
||||
}
|
||||
}
|
||||
results := lambda.Filter(parallel.Map(requests, requests[0].Json(), 4), requests[0].NoErr())
|
||||
for _, v := range results {
|
||||
log.Printf("%s", v)
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.tuxpa.in/a/lambda/parallel"
|
||||
)
|
||||
|
||||
type someResult struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
var requests = make([]*http.Request, 20)
|
||||
|
||||
for i := range requests {
|
||||
r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i), nil)
|
||||
requests[i] = r
|
||||
}
|
||||
|
||||
results, errs := parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) {
|
||||
out := &someResult{}
|
||||
res, err := http.DefaultClient.Do(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
err = json.NewDecoder(res.Body).Decode(out)
|
||||
return out, err
|
||||
}, 4)
|
||||
|
||||
for i, v := range results {
|
||||
if errs[i] != nil {
|
||||
log.Printf("err: %s", errs[i])
|
||||
} else {
|
||||
log.Printf("result: %+v", v)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DecoderFunc func(r io.Reader) Decoder
|
||||
|
||||
type Decoder interface {
|
||||
Decode(any) error
|
||||
}
|
||||
|
||||
type HttpRequest[T any] struct {
|
||||
Req http.Request
|
||||
ans T
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *HttpRequest[T]) String() string {
|
||||
return fmt.Sprintf("%+v", r.ans)
|
||||
}
|
||||
|
||||
func (_ *HttpRequest[T]) With(df DecoderFunc) func(r *HttpRequest[T]) *HttpRequest[T] {
|
||||
return func(r *HttpRequest[T]) *HttpRequest[T] {
|
||||
r.ans = *new(T)
|
||||
res, err := http.DefaultClient.Do(&r.Req)
|
||||
if err != nil {
|
||||
r.err = err
|
||||
return r
|
||||
}
|
||||
defer res.Body.Close()
|
||||
r.err = df(res.Body).Decode(&r.ans)
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
func (z *HttpRequest[T]) Json() func(r *HttpRequest[T]) *HttpRequest[T] {
|
||||
return z.With(func(r io.Reader) Decoder {
|
||||
return json.NewDecoder(r)
|
||||
})
|
||||
}
|
||||
|
||||
func (_ *HttpRequest[T]) NoErr() func(r *HttpRequest[T]) bool {
|
||||
return func(r *HttpRequest[T]) bool {
|
||||
return r.err == nil
|
||||
}
|
||||
}
|
||||
|
||||
type someResult struct {
|
||||
Title string `json:"title"`
|
||||
Id int `json:"id"`
|
||||
}
|
Loading…
Reference in New Issue