github.com/cloudwego/hertz@v0.9.3/pkg/network/netpoll/dial.go (about)

     1  // Copyright 2022 CloudWeGo Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package netpoll
    17  
    18  import (
    19  	"crypto/tls"
    20  	"net"
    21  	"time"
    22  
    23  	"github.com/cloudwego/hertz/pkg/common/errors"
    24  	"github.com/cloudwego/hertz/pkg/network"
    25  	"github.com/cloudwego/netpoll"
    26  )
    27  
    28  var errNotSupportTLS = errors.NewPublic("not support tls")
    29  
    30  type dialer struct {
    31  	netpoll.Dialer
    32  }
    33  
    34  func (d dialer) DialConnection(n, address string, timeout time.Duration, tlsConfig *tls.Config) (conn network.Conn, err error) {
    35  	if tlsConfig != nil {
    36  		// https
    37  		return nil, errNotSupportTLS
    38  	}
    39  	c, err := d.Dialer.DialConnection(n, address, timeout)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	conn = newConn(c)
    44  	return
    45  }
    46  
    47  func (d dialer) DialTimeout(network, address string, timeout time.Duration, tlsConfig *tls.Config) (conn net.Conn, err error) {
    48  	if tlsConfig != nil {
    49  		return nil, errNotSupportTLS
    50  	}
    51  	conn, err = d.Dialer.DialTimeout(network, address, timeout)
    52  	return
    53  }
    54  
    55  func (d dialer) AddTLS(conn network.Conn, tlsConfig *tls.Config) (network.Conn, error) {
    56  	return nil, errNotSupportTLS
    57  }
    58  
    59  func NewDialer() network.Dialer {
    60  	return dialer{Dialer: netpoll.NewDialer()}
    61  }