Rename quic session to connection

Co-authored-by: 秋のかえで <autmaple@protonmail.com>
This commit is contained in:
yuhan6665 2022-04-09 00:48:02 -04:00
parent c6550aecfc
commit 393d211d1e
3 changed files with 83 additions and 83 deletions

View file

@ -37,7 +37,7 @@ type QUICNameServer struct {
reqID uint32
name string
destination *net.Destination
session quic.Session
connection quic.Connection
}
// NewQUICNameServer creates DNS-over-QUIC client object for local resolving
@ -194,7 +194,7 @@ func (s *QUICNameServer) sendQuery(ctx context.Context, domain string, clientIP
conn, err := s.openStream(dnsCtx)
if err != nil {
newError("failed to open quic session").Base(err).AtError().WriteToLog()
newError("failed to open quic connection").Base(err).AtError().WriteToLog()
return
}
@ -322,7 +322,7 @@ func (s *QUICNameServer) QueryIP(ctx context.Context, domain string, clientIP ne
}
}
func isActive(s quic.Session) bool {
func isActive(s quic.Connection) bool {
select {
case <-s.Context().Done():
return false
@ -331,17 +331,17 @@ func isActive(s quic.Session) bool {
}
}
func (s *QUICNameServer) getSession() (quic.Session, error) {
var session quic.Session
func (s *QUICNameServer) getConnection() (quic.Connection, error) {
var conn quic.Connection
s.RLock()
session = s.session
if session != nil && isActive(session) {
conn = s.connection
if conn != nil && isActive(conn) {
s.RUnlock()
return session, nil
return conn, nil
}
if session != nil {
// we're recreating the session, let's create a new one
_ = session.CloseWithError(0, "")
if conn != nil {
// we're recreating the connection, let's create a new one
_ = conn.CloseWithError(0, "")
}
s.RUnlock()
@ -349,42 +349,42 @@ func (s *QUICNameServer) getSession() (quic.Session, error) {
defer s.Unlock()
var err error
session, err = s.openSession()
conn, err = s.openConnection()
if err != nil {
// This does not look too nice, but QUIC (or maybe quic-go)
// doesn't seem stable enough.
// Maybe retransmissions aren't fully implemented in quic-go?
// Anyways, the simple solution is to make a second try when
// it fails to open the QUIC session.
session, err = s.openSession()
// it fails to open the QUIC connection.
conn, err = s.openConnection()
if err != nil {
return nil, err
}
}
s.session = session
return session, nil
s.connection = conn
return conn, nil
}
func (s *QUICNameServer) openSession() (quic.Session, error) {
func (s *QUICNameServer) openConnection() (quic.Connection, error) {
tlsConfig := tls.Config{}
quicConfig := &quic.Config{
HandshakeIdleTimeout: handshakeTimeout,
}
session, err := quic.DialAddrContext(context.Background(), s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
conn, err := quic.DialAddrContext(context.Background(), s.destination.NetAddr(), tlsConfig.GetTLSConfig(tls.WithNextProto("http/1.1", http2.NextProtoTLS, NextProtoDQ)), quicConfig)
if err != nil {
return nil, err
}
return session, nil
return conn, nil
}
func (s *QUICNameServer) openStream(ctx context.Context) (quic.Stream, error) {
session, err := s.getSession()
conn, err := s.getConnection()
if err != nil {
return nil, err
}
// open a new stream
return session.OpenStreamSync(ctx)
return conn.OpenStreamSync(ctx)
}