...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/topology/tls_connection_source_1_16.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/topology

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  //go:build !go1.17
     8  // +build !go1.17
     9  
    10  package topology
    11  
    12  import (
    13  	"context"
    14  	"crypto/tls"
    15  	"net"
    16  )
    17  
    18  type tlsConn interface {
    19  	net.Conn
    20  
    21  	// Only require Handshake on the interface for Go 1.16 and less.
    22  	Handshake() error
    23  	ConnectionState() tls.ConnectionState
    24  }
    25  
    26  var _ tlsConn = (*tls.Conn)(nil)
    27  
    28  type tlsConnectionSource interface {
    29  	Client(net.Conn, *tls.Config) tlsConn
    30  }
    31  
    32  type tlsConnectionSourceFn func(net.Conn, *tls.Config) tlsConn
    33  
    34  var _ tlsConnectionSource = (tlsConnectionSourceFn)(nil)
    35  
    36  func (t tlsConnectionSourceFn) Client(nc net.Conn, cfg *tls.Config) tlsConn {
    37  	return t(nc, cfg)
    38  }
    39  
    40  var defaultTLSConnectionSource tlsConnectionSourceFn = func(nc net.Conn, cfg *tls.Config) tlsConn {
    41  	return tls.Client(nc, cfg)
    42  }
    43  
    44  // clientHandshake will perform a handshake with a goroutine and wait for its completion on Go 1.16 and less
    45  // when HandshakeContext is not available.
    46  func clientHandshake(ctx context.Context, client tlsConn) error {
    47  	errChan := make(chan error, 1)
    48  	go func() {
    49  		errChan <- client.Handshake()
    50  	}()
    51  
    52  	select {
    53  	case err := <-errChan:
    54  		return err
    55  	case <-ctx.Done():
    56  		return ctx.Err()
    57  	}
    58  }
    59  

View as plain text