Documentation
¶
Overview ¶
Package retryable wraps hashicorp/go-retryablehttp and exposes it as a standard *http.Client with configurable retry and timeout. Import this sub-package only when a standard *http.Client with retry is needed; for structured service-to-service calls with JWT and JSON use the parent httpclient package (resty-based) instead.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Timeout time.Duration // default: 10s
RetryMax int // default: 3
InsecureSkipVerify bool // skip TLS certificate verification; use only for internal services with self-signed certs
}
Config holds configuration for the retryablehttp-based Client. Wait time between retries is managed by retryablehttp's built-in exponential back-off. The caller is responsible for reading values from env (or any other source) at the composition root so this package has no dependency on env or log.
type HttpClient ¶
type HttpClient struct {
// contains filtered or unexported fields
}
HttpClient wraps a retryablehttp.Client with pre-configured retry and timeout.
func NewHttpClient ¶
func NewHttpClient(cfg ...Config) *HttpClient
NewHttpClient creates and returns a new HttpClient. Pass an optional Config to override defaults; omit for all-default settings. Log output is routed through slog.Default(), which is the project logger after log.InitLog() has been called.
Example ¶
ExampleNewHttpClient shows how to create a retryable HttpClient with default settings. Log output is routed through slog.Default() — call log.InitLog() first to integrate with the project's structured logger.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient/retryable"
)
func main() {
cli := retryable.NewHttpClient()
fmt.Println(cli != nil)
}
Output: true
Example (CustomConfig) ¶
ExampleNewHttpClient_customConfig shows how to override default timeout and retry settings. Zero-value fields fall back to package defaults (Timeout=10s, RetryMax=3).
package main
import (
"fmt"
"time"
"github.com/phcp-tech/common-library-golang/httpclient/retryable"
)
func main() {
cli := retryable.NewHttpClient(retryable.Config{
Timeout: 15 * time.Second,
RetryMax: 5,
})
fmt.Println(cli != nil)
}
Output: true
Example (InsecureSkipVerify) ¶
ExampleNewHttpClient_insecureSkipVerify shows how to disable TLS certificate verification for internal services that use self-signed certificates. Do NOT use this in production for external services.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient/retryable"
)
func main() {
cli := retryable.NewHttpClient(retryable.Config{
InsecureSkipVerify: true, // only for internal services with self-signed certs
})
fmt.Println(cli != nil)
}
Output: true
func (*HttpClient) Client ¶
func (c *HttpClient) Client() *retryablehttp.Client
Client returns the underlying retryablehttp.Client for advanced configuration or for obtaining a standard *http.Client via StandardClient().
Example ¶
ExampleHttpClient_Client shows how to obtain the underlying retryablehttp.Client and its standard *http.Client for use with existing net/http code.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/httpclient/retryable"
)
func main() {
cli := retryable.NewHttpClient()
underlying := cli.Client() // *retryablehttp.Client
stdClient := underlying.StandardClient() // *http.Client with retry
fmt.Println(stdClient != nil)
}
Output: true