Skip to content

Commit

Permalink
Merge branch 'feat/dialer_name' of https://github.com/FGYFFFF/hertz i…
Browse files Browse the repository at this point in the history
…nto feat/dialer_name
  • Loading branch information
FGYFFFF committed Sep 19, 2022
2 parents c07e8d0 + 2e973df commit eee51bc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 57 deletions.
26 changes: 26 additions & 0 deletions pkg/app/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"bytes"
"context"
"fmt"
"reflect"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -540,6 +542,30 @@ func (c *Client) SetClientFactory(cf suite.ClientFactory) {
c.clientFactory = cf
}

// GetDialerName returns the name of the dialer
func (c *Client) GetDialerName() (dName string, err error) {
defer func() {
err := recover()
if err != nil {
dName = "unknown"
}
}()

opt := c.GetOptions()
if opt == nil || opt.Dialer == nil {
return "", fmt.Errorf("abnormal process: there is no client options or dialer")
}

dName = reflect.TypeOf(opt.Dialer).String()
dSlice := strings.Split(dName, ".")
dName = dSlice[0]
if dName[0] == '*' {
dName = dName[1:]
}

return
}

// NewClient return a client with options
func NewClient(opts ...config.ClientOption) (*Client, error) {
opt := config.NewClientOptions(opts)
Expand Down
38 changes: 38 additions & 0 deletions pkg/app/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1879,3 +1879,41 @@ func newMockDialerWithCustomFunc(network, address string, timeout time.Duration,
timeout: timeout,
}
}

func TestClientDialerName(t *testing.T) {
client, _ := NewClient()
dName, err := client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "netpoll" {
t.Errorf("expected 'netpoll', but get %s", dName)
}

client, _ = NewClient(WithDialer(standard.NewDialer()))
dName, err = client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "standard" {
t.Errorf("expected 'standard', but get %s", dName)
}

client, _ = NewClient(WithDialer(&mockDialer{}))
dName, err = client.GetDialerName()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if dName != "client" {
t.Errorf("expected 'client', but get %s", dName)
}

client.options.Dialer = nil
dName, err = client.GetDialerName()
if err == nil {
t.Errorf("expected an err for abnormal process")
}
if dName != "" {
t.Errorf("expected 'empty string', but get %s", dName)
}
}
20 changes: 0 additions & 20 deletions pkg/network/dialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package dialer
import (
"crypto/tls"
"net"
"reflect"
"strings"
"time"

"github.com/cloudwego/hertz/pkg/network"
Expand Down Expand Up @@ -51,21 +49,3 @@ func DialTimeout(network, address string, timeout time.Duration, tlsConfig *tls.
func AddTLS(conn network.Conn, tlsConfig *tls.Config) (network.Conn, error) {
return defaultDialer.AddTLS(conn, tlsConfig)
}

// GetDialerName returns the name of the dialer
func GetDialerName() (dName string) {
defer func() {
err := recover()
if err != nil {
dName = "unknown"
}
}()

dName = reflect.TypeOf(defaultDialer).String()
dSlice := strings.Split(dName, ".")
dName = dSlice[0]
if dName[0] == '*' {
dName = dName[1:]
}
return
}
37 changes: 0 additions & 37 deletions pkg/network/dialer/dialer_test.go

This file was deleted.

0 comments on commit eee51bc

Please sign in to comment.