Documentation
¶
Overview ¶
Package batch provides utilities for batch processing HTTP requests
- supports concurrent execution with configurable concurrency and timeout
- provides methods for adding requests and executing them in batches
Example ¶
package main
import (
"fmt"
"time"
"github.com/gookit/greq/ext/batch"
)
var testApiURL string
func main() {
// Execute all requests
bp := batch.NewProcessor(
batch.WithMaxConcurrency(5),
batch.WithBatchTimeout(10*time.Second),
)
bp.AddGet("req1", testApiURL+"/list")
bp.AddPost("req2", testApiURL+"/submit", map[string]string{"key": "value"})
results := bp.ExecuteAll()
fmt.Println("Results: ", len(results))
// Execute any (first success)
bp2 := batch.NewProcessor()
bp2.AddGet("mirror1", testApiURL+"/file1")
bp2.AddGet("mirror2", testApiURL+"/file2")
bp2.AddGet("mirror3", testApiURL+"/file3")
result := bp2.ExecuteAny()
fmt.Println("First successful result: ", result.ID)
// Convenience functions
urls := []string{testApiURL + "/api1", testApiURL + "/api2", testApiURL + "/api3"}
allResults := batch.GetAll(urls)
fmt.Println("All results: ", len(allResults))
firstResult := batch.GetAny(urls)
fmt.Println("First successful result: ", firstResult.ID)
}
Index ¶
- type ProcessOptionFn
- type Processor
- func (bp *Processor) Add(id, method, url string, body any, optFns ...greq.OptionFn) *Processor
- func (bp *Processor) AddDelete(id, url string, optFns ...greq.OptionFn) *Processor
- func (bp *Processor) AddGet(id, url string, optFns ...greq.OptionFn) *Processor
- func (bp *Processor) AddPost(id, url string, body any, optFns ...greq.OptionFn) *Processor
- func (bp *Processor) AddPut(id, url string, body any, optFns ...greq.OptionFn) *Processor
- func (bp *Processor) AddRequest(req *Request) *Processor
- func (bp *Processor) Client() *greq.Client
- func (bp *Processor) Close()
- func (bp *Processor) Context() context.Context
- func (bp *Processor) ExecuteAll() Results
- func (bp *Processor) ExecuteAny() *Result
- func (bp *Processor) MaxConcurrency() int
- func (bp *Processor) Timeout() time.Duration
- type Request
- type Result
- type Results
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ProcessOptionFn ¶
type ProcessOptionFn func(bp *Processor)
ProcessOptionFn is a function to configure batch processor
func WithBatchTimeout ¶
func WithBatchTimeout(timeout time.Duration) ProcessOptionFn
WithBatchTimeout sets the timeout for the batch operation
func WithClient ¶
func WithClient(client *greq.Client) ProcessOptionFn
WithClient sets a custom client for batch processing
func WithContext ¶
func WithContext(ctx context.Context) ProcessOptionFn
WithContext sets a custom context for batch processing
func WithMaxConcurrency ¶
func WithMaxConcurrency(n int) ProcessOptionFn
WithMaxConcurrency sets the maximum number of concurrent requests
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor handles batch request processing
func NewProcessor ¶
func NewProcessor(optFns ...ProcessOptionFn) *Processor
NewProcessor creates a new batch processor
func (*Processor) AddRequest ¶
AddRequest adds a request to the batch
func (*Processor) ExecuteAll ¶
ExecuteAll executes all requests and waits for all to complete
func (*Processor) ExecuteAny ¶
ExecuteAny executes requests and returns when any one completes successfully
func (*Processor) MaxConcurrency ¶
MaxConcurrency returns the maximum number of concurrent requests
type Request ¶
type Request struct {
// ID is an optional identifier for the request
ID string
// Method is the HTTP method (GET, POST, etc.)
Method string
// URL is the full request URL
URL string
// Body is the request body (optional)
Body any
// Options are the request options
Options []greq.OptionFn
}
Request represents a single request in a batch
type Result ¶
type Result struct {
// ID is the request identifier
ID string
// Request is the original request
Request *Request
// Response is the HTTP response (nil if error occurred)
Response *greq.Response
// Error is any error that occurred during the request
Error error
// Duration is the time taken to complete the request
Duration time.Duration
}
Result represents the result of a single batch request
func ExecuteAny ¶
func ExecuteAny(requests []*Request, optFns ...ProcessOptionFn) *Result
ExecuteAny executes requests using the standard client and returns first successful result
type Results ¶
Results is a map of request ID to Result
func ExecuteAll ¶
func ExecuteAll(requests []*Request, optFns ...ProcessOptionFn) Results
ExecuteAll executes all requests using the standard client and returns results