- 新增 managed/external/nested 三种传输保护模式 - 新增 peer attach 显式认证、抗重放、channel binding 和可选前向保密协商 - 明确单连接注入与可重拨连接源的语义边界 - 禁止 ConnectByConn 场景下 dedicated bulk 走 sidecar,auto 模式自动回退 shared - 修正 dedicated attach 在 bootstrap/steady profile 切换下的处理逻辑 - 优化 shared bulk super-batch 与批量 framed write 路径 - 降低 stream/bulk fast path 的复制和分发损耗 - 补齐 benchmark、回归测试、运行时快照和 README 文档
133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package notify
|
|
|
|
import (
|
|
"b612.me/notify/internal/transport"
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
clientConnectSourceConn = "conn"
|
|
clientConnectSourceNetwork = "network"
|
|
clientConnectSourceTimeout = "timeout"
|
|
clientConnectSourceFactory = "factory"
|
|
)
|
|
|
|
var errClientReconnectSourceUnavailable = errors.New("client reconnect source is unavailable")
|
|
|
|
type clientConnectSource struct {
|
|
kind string
|
|
network string
|
|
addr string
|
|
dialFn func(context.Context) (net.Conn, error)
|
|
supportsAdditional bool
|
|
}
|
|
|
|
func newClientConnConnectSource(conn net.Conn) *clientConnectSource {
|
|
source := &clientConnectSource{
|
|
kind: clientConnectSourceConn,
|
|
supportsAdditional: false,
|
|
}
|
|
if conn == nil {
|
|
return source
|
|
}
|
|
if remoteAddr := conn.RemoteAddr(); remoteAddr != nil {
|
|
source.network = remoteAddr.Network()
|
|
source.addr = remoteAddr.String()
|
|
}
|
|
if source.network == "" {
|
|
if localAddr := conn.LocalAddr(); localAddr != nil {
|
|
source.network = localAddr.Network()
|
|
}
|
|
}
|
|
return source
|
|
}
|
|
|
|
func newClientNetworkConnectSource(network string, addr string) *clientConnectSource {
|
|
return &clientConnectSource{
|
|
kind: clientConnectSourceNetwork,
|
|
network: network,
|
|
addr: addr,
|
|
supportsAdditional: true,
|
|
dialFn: func(context.Context) (net.Conn, error) {
|
|
return transport.Dial(network, addr)
|
|
},
|
|
}
|
|
}
|
|
|
|
func newClientTimeoutConnectSource(network string, addr string, timeout time.Duration) *clientConnectSource {
|
|
return &clientConnectSource{
|
|
kind: clientConnectSourceTimeout,
|
|
network: network,
|
|
addr: addr,
|
|
supportsAdditional: true,
|
|
dialFn: func(context.Context) (net.Conn, error) {
|
|
return transport.DialTimeout(network, addr, timeout)
|
|
},
|
|
}
|
|
}
|
|
|
|
func newClientFactoryConnectSource(dialFn func(context.Context) (net.Conn, error)) *clientConnectSource {
|
|
return &clientConnectSource{
|
|
kind: clientConnectSourceFactory,
|
|
dialFn: dialFn,
|
|
supportsAdditional: true,
|
|
}
|
|
}
|
|
|
|
func (s *clientConnectSource) clone() *clientConnectSource {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
out := *s
|
|
return &out
|
|
}
|
|
|
|
func (s *clientConnectSource) canReconnect() bool {
|
|
return s != nil && s.dialFn != nil
|
|
}
|
|
|
|
func (s *clientConnectSource) supportsAdditionalConn() bool {
|
|
return s != nil && s.supportsAdditional
|
|
}
|
|
|
|
func (s *clientConnectSource) isUDP() bool {
|
|
if s == nil {
|
|
return false
|
|
}
|
|
return transport.IsUDPNetwork(s.network)
|
|
}
|
|
|
|
func (s *clientConnectSource) dial(ctx context.Context) (net.Conn, error) {
|
|
if s == nil || s.dialFn == nil {
|
|
return nil, errClientReconnectSourceUnavailable
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return s.dialFn(ctx)
|
|
}
|
|
|
|
func (c *ClientCommon) setClientConnectSource(source *clientConnectSource) {
|
|
if c == nil {
|
|
return
|
|
}
|
|
if source == nil {
|
|
c.connectSource.Store(nil)
|
|
return
|
|
}
|
|
c.connectSource.Store(source.clone())
|
|
}
|
|
|
|
func (c *ClientCommon) clientConnectSourceSnapshot() *clientConnectSource {
|
|
if c == nil {
|
|
return nil
|
|
}
|
|
if source := c.connectSource.Load(); source != nil {
|
|
return source.clone()
|
|
}
|
|
return nil
|
|
}
|