mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 13:44:03 +03:00
FILE / ScuroNeko/mtg
network/v2/network.go
Исходный файл и его история в репозитории.
97 lines
2.0 KiB
Go
97 lines
2.0 KiB
Go
package network
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/9seconds/mtg/v2/essentials"
|
|
"github.com/9seconds/mtg/v2/mtglib"
|
|
)
|
|
|
|
type network struct {
|
|
net.Dialer
|
|
|
|
keepAliveConfig net.KeepAliveConfig
|
|
httpTimeout time.Duration
|
|
idleTimeout time.Duration
|
|
userAgent string
|
|
}
|
|
|
|
func (n *network) Dial(network, address string) (essentials.Conn, error) {
|
|
return n.DialContext(context.Background(), network, address)
|
|
}
|
|
|
|
func (n *network) DialContext(ctx context.Context, network, address string) (essentials.Conn, error) {
|
|
switch network {
|
|
case "tcp", "tcp4", "tcp6":
|
|
default:
|
|
return nil, fmt.Errorf("unsupported network %s", network)
|
|
}
|
|
|
|
conn, err := n.Dialer.DialContext(ctx, network, address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tcpConn := conn.(*net.TCPConn)
|
|
|
|
return tcpConn, setCommonSocketOptions(tcpConn, n.keepAliveConfig)
|
|
}
|
|
|
|
func (n *network) MakeHTTPClient(
|
|
dialFunc func(context.Context, string, string) (essentials.Conn, error),
|
|
) *http.Client {
|
|
if dialFunc == nil {
|
|
dialFunc = n.DialContext
|
|
}
|
|
|
|
return &http.Client{
|
|
Timeout: n.httpTimeout,
|
|
Transport: networkHTTPTransport{
|
|
userAgent: n.userAgent,
|
|
next: &http.Transport{
|
|
IdleConnTimeout: n.idleTimeout,
|
|
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
return dialFunc(ctx, network, address)
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (n *network) NativeDialer() *net.Dialer {
|
|
return &n.Dialer
|
|
}
|
|
|
|
func New(
|
|
dnsResolver *net.Resolver,
|
|
userAgent string,
|
|
tcpTimeout,
|
|
httpTimeout,
|
|
idleTimeout time.Duration,
|
|
keepAliveConfig net.KeepAliveConfig,
|
|
) mtglib.Network {
|
|
if dnsResolver == nil {
|
|
dnsResolver = net.DefaultResolver
|
|
}
|
|
|
|
if userAgent == "" {
|
|
userAgent = UserAgent
|
|
}
|
|
|
|
return &network{
|
|
Dialer: net.Dialer{
|
|
Timeout: tcpTimeout,
|
|
Resolver: dnsResolver,
|
|
FallbackDelay: -1,
|
|
},
|
|
userAgent: userAgent,
|
|
idleTimeout: idleTimeout,
|
|
httpTimeout: httpTimeout,
|
|
keepAliveConfig: keepAliveConfig,
|
|
}
|
|
}
|