batch

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 5 Imported by: 0

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

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) Add

func (bp *Processor) Add(id, method, url string, body any, optFns ...greq.OptionFn) *Processor

Add adds a simple GET request to the batch

func (*Processor) AddDelete

func (bp *Processor) AddDelete(id, url string, optFns ...greq.OptionFn) *Processor

AddDelete adds a DELETE request to the batch

func (*Processor) AddGet

func (bp *Processor) AddGet(id, url string, optFns ...greq.OptionFn) *Processor

AddGet adds a GET request to the batch

func (*Processor) AddPost

func (bp *Processor) AddPost(id, url string, body any, optFns ...greq.OptionFn) *Processor

AddPost adds a POST request to the batch

func (*Processor) AddPut

func (bp *Processor) AddPut(id, url string, body any, optFns ...greq.OptionFn) *Processor

AddPut adds a PUT request to the batch

func (*Processor) AddRequest

func (bp *Processor) AddRequest(req *Request) *Processor

AddRequest adds a request to the batch

func (*Processor) Client

func (bp *Processor) Client() *greq.Client

Client returns the HTTP client used for requests

func (*Processor) Close

func (bp *Processor) Close()

Close cleans up resources

func (*Processor) Context

func (bp *Processor) Context() context.Context

Context returns the context for the batch operation

func (*Processor) ExecuteAll

func (bp *Processor) ExecuteAll() Results

ExecuteAll executes all requests and waits for all to complete

func (*Processor) ExecuteAny

func (bp *Processor) ExecuteAny() *Result

ExecuteAny executes requests and returns when any one completes successfully

func (*Processor) MaxConcurrency

func (bp *Processor) MaxConcurrency() int

MaxConcurrency returns the maximum number of concurrent requests

func (*Processor) Timeout

func (bp *Processor) Timeout() time.Duration

Timeout returns the timeout for the batch operation

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

func GetAny

func GetAny(urls []string, optFns ...greq.OptionFn) *Result

GetAny executes multiple GET requests and returns first successful result

  • each request will be assigned an ID as "id_<index>"

type Results

type Results map[string]*Result

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

func GetAll

func GetAll(urls []string, optFns ...greq.OptionFn) Results

GetAll executes multiple GET requests and waits for all to complete

  • each request will be assigned an ID as "id_<index>"

func PostAll

func PostAll(urls []string, bodies []any, optFns ...greq.OptionFn) Results

PostAll executes multiple POST requests and waits for all to complete

  • each request will be assigned an ID as "id_<index>"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL