network

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package network provides network diagnostics and health checking capabilities.

Index

Constants

View Source
const (
	// DefaultTimeout for diagnostic tests.
	DefaultTimeout = 30 * time.Second

	// DefaultDNSTimeout for DNS resolution.
	DefaultDNSTimeout = 5 * time.Second

	// DefaultConnTimeout for connectivity tests.
	DefaultConnTimeout = 10 * time.Second

	// BandwidthTestDuration for bandwidth tests.
	BandwidthTestDuration = 10 * time.Second

	// BandwidthTestSize for bandwidth tests (1MB).
	BandwidthTestSize = 1024 * 1024
)

Default constants.

Variables

View Source
var GlobalAdvancedPool = NewAdvancedConnectionPool(100, 100)

GlobalAdvancedPool is the global advanced connection pool

View Source
var GlobalPool = NewConnectionPool(100, 100)

GlobalPool is a shared connection pool for the application

Functions

func CreateLightweightClient added in v1.3.0

func CreateLightweightClient() *http.Client

CreateLightweightClient creates a minimal HTTP client for small files with reduced overhead and connection pooling

func CreateOptimizedClient added in v1.3.0

func CreateOptimizedClient(timeout time.Duration) *http.Client

CreateOptimizedClient creates an HTTP client with optimized transport settings

func CreateOptimizedTransport added in v1.3.0

func CreateOptimizedTransport() *http.Transport

CreateOptimizedTransport creates an HTTP transport optimized for file downloads with improved connection pooling, timeouts, and HTTP/2 support

Types

type AdvancedConnectionPool added in v1.3.0

type AdvancedConnectionPool struct {
	*ConnectionPool
	// contains filtered or unexported fields
}

AdvancedConnectionPool extends the basic connection pool with advanced features

func NewAdvancedConnectionPool added in v1.3.0

func NewAdvancedConnectionPool(maxIdle, maxConns int) *AdvancedConnectionPool

NewAdvancedConnectionPool creates an advanced connection pool

func (*AdvancedConnectionPool) GetClientWithDNSCache added in v1.3.0

func (acp *AdvancedConnectionPool) GetClientWithDNSCache(host string, timeout time.Duration) *http.Client

GetClientWithDNSCache returns an HTTP client with DNS caching

func (*AdvancedConnectionPool) GetTLSConfig added in v1.3.0

func (acp *AdvancedConnectionPool) GetTLSConfig(host string) *tls.Config

GetTLSConfig returns a cached TLS configuration for a host

func (*AdvancedConnectionPool) OptimizeForCDN added in v1.3.0

func (acp *AdvancedConnectionPool) OptimizeForCDN(cdnHost string)

OptimizeForCDN configures the pool specifically for CDN downloads

func (*AdvancedConnectionPool) PrewarmCDNs added in v1.3.0

func (acp *AdvancedConnectionPool) PrewarmCDNs()

PrewarmCDNs pre-warms connections to common CDNs

func (*AdvancedConnectionPool) PrewarmConnection added in v1.3.0

func (acp *AdvancedConnectionPool) PrewarmConnection(host string) error

PrewarmConnection establishes a connection to a host in advance

type BandwidthResult

type BandwidthResult struct {
	DownloadSpeed  float64       `json:"download_speed_mbps"`
	UploadSpeed    float64       `json:"upload_speed_mbps"`
	Latency        time.Duration `json:"latency"`
	PacketLoss     float64       `json:"packet_loss_percent"`
	TestDuration   time.Duration `json:"test_duration"`
	TestServerHost string        `json:"test_server_host"`
	RecommendedUse string        `json:"recommended_use"`
}

BandwidthResult represents bandwidth test results.

type ConnectionPool added in v1.3.0

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

ConnectionPool manages a pool of HTTP clients for connection reuse

func NewConnectionPool added in v1.3.0

func NewConnectionPool(maxIdle, maxConns int) *ConnectionPool

NewConnectionPool creates a new connection pool

func (*ConnectionPool) Close added in v1.3.0

func (cp *ConnectionPool) Close()

Close closes all clients in the pool

func (*ConnectionPool) GetClient added in v1.3.0

func (cp *ConnectionPool) GetClient(host string, timeout time.Duration) *http.Client

GetClient returns an HTTP client for the given host

func (*ConnectionPool) Stats added in v1.3.0

func (cp *ConnectionPool) Stats() map[string]interface{}

Stats returns pool statistics

type DNSCache added in v1.3.0

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

DNSCache caches DNS resolutions to reduce lookup overhead

func NewDNSCache added in v1.3.0

func NewDNSCache() *DNSCache

NewDNSCache creates a new DNS cache

func (*DNSCache) Clear added in v1.3.0

func (dc *DNSCache) Clear()

Clear removes all entries from the DNS cache

func (*DNSCache) Resolve added in v1.3.0

func (dc *DNSCache) Resolve(host string) ([]net.IP, error)

Resolve performs a cached DNS lookup

type DiagnosticOptions

type DiagnosticOptions struct {
	Timeout          time.Duration
	TestHosts        []string
	DNSServers       []string
	BandwidthServers []string
	IncludeBandwidth bool
	IncludeProxy     bool
	Verbose          bool
}

DiagnosticOptions configures diagnostic behavior.

type DiagnosticResult

type DiagnosticResult struct {
	TestName    string                 `json:"test_name"`
	Success     bool                   `json:"success"`
	Duration    time.Duration          `json:"duration"`
	Error       error                  `json:"error,omitempty"`
	Details     map[string]interface{} `json:"details,omitempty"`
	Timestamp   time.Time              `json:"timestamp"`
	Suggestions []string               `json:"suggestions,omitempty"`
}

DiagnosticResult represents the result of a network diagnostic test.

type Diagnostics

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

Diagnostics provides network diagnostic capabilities.

func NewDiagnostics

func NewDiagnostics() *Diagnostics

NewDiagnostics creates a new network diagnostics instance.

func (*Diagnostics) DetectProxy

func (d *Diagnostics) DetectProxy(ctx context.Context) interface{}

DetectProxy detects proxy configuration and validates it.

func (*Diagnostics) RunFullDiagnostics

func (d *Diagnostics) RunFullDiagnostics(
	ctx context.Context,
	options *DiagnosticOptions,
) (*NetworkHealth, error)

RunFullDiagnostics performs a comprehensive network health check.

func (*Diagnostics) TestBandwidth

func (d *Diagnostics) TestBandwidth(ctx context.Context, testServer string) interface{}

TestBandwidth performs a bandwidth test.

func (*Diagnostics) TestConnectivity

func (d *Diagnostics) TestConnectivity(ctx context.Context, testHosts []string) DiagnosticResult

TestConnectivity tests basic connectivity to multiple hosts.

func (*Diagnostics) TestDNSResolution

func (d *Diagnostics) TestDNSResolution(
	ctx context.Context,
	dnsServers, testHosts []string,
) DiagnosticResult

TestDNSResolution tests DNS resolution for multiple hosts and DNS servers.

func (*Diagnostics) WithDNSServers

func (d *Diagnostics) WithDNSServers(servers []string) *Diagnostics

WithDNSServers sets the DNS servers to test.

func (*Diagnostics) WithTestHosts

func (d *Diagnostics) WithTestHosts(hosts []string) *Diagnostics

WithTestHosts sets the hosts to test connectivity against.

func (*Diagnostics) WithTimeout

func (d *Diagnostics) WithTimeout(timeout time.Duration) *Diagnostics

WithTimeout sets the diagnostic timeout.

func (*Diagnostics) WithVerbose

func (d *Diagnostics) WithVerbose(verbose bool) *Diagnostics

WithVerbose enables verbose output.

type HealthStatus

type HealthStatus int
const (
	// HealthGood indicates good network health.
	HealthGood HealthStatus = iota

	// HealthWarning indicates some network issues.
	HealthWarning

	// HealthPoor indicates significant network problems.
	HealthPoor

	// HealthCritical indicates critical network issues.
	HealthCritical
)

func (HealthStatus) String

func (hs HealthStatus) String() string

String returns the string representation of HealthStatus.

type NetworkHealth

type NetworkHealth struct {
	OverallStatus HealthStatus       `json:"overall_status"`
	DNSHealth     *DiagnosticResult  `json:"dns_health"`
	ConnHealth    *DiagnosticResult  `json:"connectivity_health"`
	BandwidthInfo *BandwidthResult   `json:"bandwidth_info"`
	ProxyInfo     *ProxyInfo         `json:"proxy_info"`
	Results       []DiagnosticResult `json:"results"`
	TestDuration  time.Duration      `json:"test_duration"`
}

NetworkHealth represents overall network health status.

type ProxyInfo

type ProxyInfo struct {
	Detected       bool              `json:"detected"`
	Type           ProxyType         `json:"type"`
	Address        string            `json:"address"`
	Port           int               `json:"port"`
	Authentication bool              `json:"authentication_required"`
	Environment    map[string]string `json:"environment_variables"`
	SystemProxy    bool              `json:"system_proxy"`
	Working        bool              `json:"working"`
	Details        map[string]string `json:"details"`
}

ProxyInfo represents proxy configuration information.

type ProxyType

type ProxyType int

ProxyType represents the type of proxy.

const (
	// ProxyHTTP represents HTTP proxy.
	ProxyHTTP ProxyType = iota

	// ProxyHTTPS represents HTTPS proxy.
	ProxyHTTPS

	// ProxySOCKS4 represents SOCKS4 proxy.
	ProxySOCKS4

	// ProxySOCKS5 represents SOCKS5 proxy.
	ProxySOCKS5

	// ProxyUnknown represents unknown proxy type.
	ProxyUnknown
)

func (ProxyType) String

func (pt ProxyType) String() string

String returns the string representation of ProxyType.

Jump to

Keyboard shortcuts

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