myhttp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 7 Imported by: 0

README

myhttp

Go Reference Go Report Card GitHub stars Support via Saweria

A lightweight, production-ready HTTP client for Go with built-in Retry, Rate Limiting, and User-Agent injection.

✨ Features

  • 🔄 Exponential Backoff with Jitter: Retries failed requests gracefully without overwhelming the server (Thundering Herd).
  • 🛡️ Safe Body Retry: Automatically buffers request bodies so POST/PUT requests don't fail on retries.
  • ⏱️ Rate Limiter: Prevents 429 Too Many Requests by strictly limiting requests per second.
  • 🤖 Custom User-Agent: Automatically injects User-Agent into every request.
  • 🪶 Zero Dependencies: Built entirely on the Go standard library.
  • ✅ Fully Tested: Comprehensive unit tests using httptest.

📦 Installation

go get github.com/MinatoAka/myhttp

🚀 Quick Start

package main

import (
	"fmt"
	"io"
	"time"

	"github.com/MinatoAka/myhttp"
)

func main() {
	// Initialize client with custom configurations
	client := myhttp.New(
		myhttp.WithRetry(3, 1*time.Second),     // Retry 3x with 1s base wait
		myhttp.WithRateLimit(5),                // Max 5 requests per second
		myhttp.WithTimeout(10*time.Second),     // 10s timeout per request
		myhttp.WithUserAgent("myapp/1.0"),      // Custom User-Agent
	)

	// Make a request
	resp, err := client.Get("https://indodax.com/api/ticker/ethidr")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}

📖 API Reference

Options

Option Description WithRetry(retries, wait) Set max retries and base wait time (uses exponential backoff + jitter). WithRateLimit(reqPerSec) Limit requests to N per second. WithTimeout(duration) Set global timeout for requests. WithUserAgent(ua) Set custom User-Agent header.

Methods

Method Description New(opts ...Option) *Client Create a new client with optional configurations. client.Get(url) Shortcut for GET requests. client.Do(req) Execute raw http.Request.

🧪 Testing

go test -v ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client — wrapper http.Client dengan retry & rate limit

func New

func New(opts ...Option) *Client

New — bikin client baru dengan options

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do — raw request

func (*Client) Get

func (c *Client) Get(url string) (*http.Response, error)

Get — shortcut untuk GET request

type Option

type Option func(*options)

Option — tipe fungsi yang memodifikasi options

func WithRateLimit

func WithRateLimit(reqPerSec int) Option

WithRateLimit — batasi request per detik

func WithRetry

func WithRetry(maxRetries int, baseWait time.Duration) Option

WithRetry — konfigurasi retry

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout — timeout per request

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent — set User-Agent

Jump to

Keyboard shortcuts

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