gahttp

package
v0.0.0-...-aea6560 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: MIT Imports: 5 Imported by: 0

README

gahttp

Async/concurrent HTTP requests for Go with rate-limiting.

Work in progress.

Example

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/tomnomnom/gahttp"
)

func printStatus(req *http.Request, resp *http.Response, err error) {
    if err != nil {
        return
    }
    fmt.Printf("%s: %s\n", req.URL, resp.Status)
}

func main() {
    p := gahttp.NewPipeline()
    p.SetConcurrency(20)
    p.SetRateLimit(time.Second * 1)

    urls := []string{
        "http://example.com",
        "http://example.com",
        "http://example.com",
        "http://example.net",
        "http://example.org",
    }

    for _, u := range urls {
        p.Get(u, gahttp.Wrap(printStatus, gahttp.CloseBody))
    }
    p.Done()

    p.Wait()
}

TODO

  • DoneAndWait() func?
  • Helper for writing responses to channel? (e.g. func ChanWriter() (chan *Response, procFn))
    • For when you don't want to do the work concurrently
  • Actually handle timeouts / provide context interface for cancellation etc?

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(opts ClientOptions) *http.Client

NewClient returns a new client with the specified options

func NewDefaultClient

func NewDefaultClient() *http.Client

NewDefaultClient returns the default HTTP client

Types

type ClientOptions

type ClientOptions int

ClientOptions are a bitmask of options for HTTP clients

const (
	// Don't follow redirects
	NoRedirects ClientOptions = 1 << iota

	// Skip verification of TLS certificates
	SkipVerify
)

type Pipeline

type Pipeline struct {
	// contains filtered or unexported fields
}

a Pipeline is the main component of the gahttp package. It orchestrates making requests, optionally rate limiting them

func NewPipeline

func NewPipeline() *Pipeline

New returns a new *Pipeline for the provided concurrency level

func NewPipelineWithClient

func NewPipelineWithClient(client *http.Client) *Pipeline

NewWithClient returns a new *Pipeline for the provided concurrency level, and uses the provided *http.Client to make requests

func (*Pipeline) Do

func (p *Pipeline) Do(r *http.Request, fn ProcFn)

Do is the pipeline's generic request function; similar to http.DefaultClient.Do(), but it also accepts a ProcFn which will be called when the request has been executed

func (*Pipeline) Done

func (p *Pipeline) Done()

Done should be called to signal to the pipeline that all requests that will be made have been enqueued. This closes the internal channel used to send requests to the workers that are executing the HTTP requests.

func (*Pipeline) Get

func (p *Pipeline) Get(u string, fn ProcFn) error

Get is a convenience wrapper around the Do() function for making HTTP GET requests. It accepts a URL and the ProcFn to process the response.

func (*Pipeline) Post

func (p *Pipeline) Post(u string, body io.Reader, fn ProcFn) error

Post is a convenience wrapper around the Do() function for making HTTP POST requests. It accepts a URL, an io.Reader for the POST body, and a ProcFn to process the response.

func (*Pipeline) Run

func (p *Pipeline) Run()

Run puts the pipeline into a running state. It launches the worker processes that execute the HTTP requests. Run() is called automatically by Do(), Get() and Post(), so it's often not necessary to call it directly.

func (*Pipeline) SetClient

func (p *Pipeline) SetClient(c *http.Client)

SetClient sets the HTTP client used by the pipeline to make HTTP requests. It can only be set before the pipeline is running

func (*Pipeline) SetConcurrency

func (p *Pipeline) SetConcurrency(c int)

SetConcurrency sets the concurrency level for the pipeline. It can only be set before the pipeline is running

func (*Pipeline) SetRateLimit

func (p *Pipeline) SetRateLimit(d time.Duration)

SetRateLimit sets the delay between requests to a given hostname

func (*Pipeline) SetRateLimitMillis

func (p *Pipeline) SetRateLimitMillis(m int)

SetRateLimitMillis sets the delay between request to a given hostname in milliseconds. This function is provided as a convenience, to make it easy to accept integer values as command line arguments.

func (*Pipeline) Wait

func (p *Pipeline) Wait()

Wait blocks until all requests in the pipeline have been executed

type ProcFn

type ProcFn func(*http.Request, *http.Response, error)

a ProcFn is a function that processes an HTTP response. The HTTP request is provided for context, along with any error that occurred.

func CloseBody

func CloseBody(fn ProcFn) ProcFn

CloseBody wraps a ProcFn and returns a version of it that automatically closed the response body

func IfNoError

func IfNoError(fn ProcFn) ProcFn

IfNoError only calls the provided ProcFn if there was no error when executing the HTTP request

func Wrap

func Wrap(fn ProcFn, middleware ...func(ProcFn) ProcFn) ProcFn

Wrap accepts a ProcFn and wraps it in any number of 'middleware' functions (e.g. the CloseBody function).

Jump to

Keyboard shortcuts

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