Repository: cloudwego/hertz
Pull request #198 (2022-08-15 08:28:12)

feat: get dialer name
#### What type of PR is this?

<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->
feat
#### What this PR does / why we need it (English/Chinese):

<!--
The description will be attached in Release Notes, 
so please describe it from user-oriented.
-->
en: Add the function to get the dialer name
zh: 添加获取 dialer 名的方法
#### Which issue(s) this PR fixes:

<!--
*Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->


codecov[bot]: # [Codecov](https://codecov.io/gh/cloudwego/hertz/pull/198?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
> Merging [#198](https://codecov.io/gh/cloudwego/hertz/pull/198?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) (c07e8d0) into [develop](https://codecov.io/gh/cloudwego/hertz/commit/86974815dd1f60b67761fa6a21b704fc50a67a19?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) (8697481) will **decrease** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@             Coverage Diff             @@
##           develop     #198      +/-   ##
===========================================
- Coverage    59.03%   59.02%   -0.01%     
===========================================
  Files           79       81       +2     
  Lines         7997     8020      +23     
===========================================
+ Hits          4721     4734      +13     
- Misses        2928     2938      +10     
  Partials       348      348              
```


| [Impacted Files](https://codecov.io/gh/cloudwego/hertz/pull/198?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) | Coverage Δ | |
|---|---|---|
| [pkg/network/dialer/dialer.go](https://codecov.io/gh/cloudwego/hertz/pull/198/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL25ldHdvcmsvZGlhbGVyL2RpYWxlci5nbw==) | `52.38% <100.00%> (ø)` | |
| [pkg/network/dialer/default.go](https://codecov.io/gh/cloudwego/hertz/pull/198/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego#diff-cGtnL25ldHdvcmsvZGlhbGVyL2RlZmF1bHQuZ28=) | `100.00% <0.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego)


Patch:
diff --git a/pkg/app/client/client.go b/pkg/app/client/client.go
index dccfede..92514ae 100644
--- a/pkg/app/client/client.go
+++ b/pkg/app/client/client.go
@@ -45,6 +45,8 @@ import (
 	"bytes"
 	"context"
 	"fmt"
+	"reflect"
+	"strings"
 	"sync"
 	"time"
 
@@ -532,6 +534,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)


Test patch:
diff --git a/pkg/app/client/client_test.go b/pkg/app/client/client_test.go
index 20c25e7..bd5464c 100644
--- a/pkg/app/client/client_test.go
+++ b/pkg/app/client/client_test.go
@@ -1879,3 +1879,52 @@ 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)
+	}
+	// Depending on the operating system,
+	// the default dialer has a different network library, either "netpoll" or "standard"
+	if !(dName == "netpoll" || dName == "standard") {
+		t.Errorf("expected 'netpoll', but get %s", dName)
+	}
+
+	client, _ = NewClient(WithDialer(netpoll.NewDialer()))
+	dName, err = client.GetDialerName()
+	if err != nil {
+		t.Fatalf("unexpected error: %v", err)
+	}
+	if dName != "netpoll" {
+		t.Errorf("expected 'standard', 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)
+	}
+}