-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transport: enable tnet by default (#56)
Co-authored-by: wineguo <wineguo@tencent.com>
- Loading branch information
Showing
9 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build linux && amd64 | ||
// +build linux,amd64 | ||
|
||
package client | ||
|
||
import ( | ||
"trpc.group/trpc-go/trpc-go/log" | ||
"trpc.group/trpc-go/trpc-go/transport" | ||
"trpc.group/trpc-go/trpc-go/transport/tnet" | ||
) | ||
|
||
func attemptSwitchingTransport(o *Options) transport.ClientTransport { | ||
// If the user doesn't explicitly set the transport (which is usually the case for trpc protocol), | ||
// attempt to switch to the tnet transport. | ||
if o.Transport == nil { | ||
if check(o) { | ||
cheer(o) | ||
return tnet.DefaultClientTransport | ||
} | ||
sigh(o) | ||
return transport.DefaultClientTransport | ||
} | ||
return o.Transport | ||
} | ||
|
||
func check(o *Options) bool { | ||
// Only use tnet transport with TCP and trpc. | ||
return (o.Network == "tcp" || | ||
o.Network == "tcp4" || | ||
o.Network == "tcp6") && | ||
o.Protocol == "trpc" | ||
} | ||
|
||
func cheer(o *Options) { | ||
log.Infof("client %s is empowered with tnet! 🤩 ", o.ServiceName) | ||
} | ||
|
||
func sigh(o *Options) { | ||
log.Infof("client: %s, tnet is not enabled by default 🧐 ", o.ServiceName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !(linux && amd64) | ||
// +build !linux !amd64 | ||
|
||
package client | ||
|
||
import "trpc.group/trpc-go/trpc-go/transport" | ||
|
||
func attemptSwitchingTransport(o *Options) transport.ClientTransport { | ||
if o.Transport == nil { | ||
return transport.DefaultClientTransport | ||
} | ||
return o.Transport | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build linux && amd64 | ||
// +build linux,amd64 | ||
|
||
package server | ||
|
||
import ( | ||
"trpc.group/trpc-go/trpc-go/log" | ||
"trpc.group/trpc-go/trpc-go/transport" | ||
"trpc.group/trpc-go/trpc-go/transport/tnet" | ||
) | ||
|
||
func attemptSwitchingTransport(o *Options) transport.ServerTransport { | ||
// If the user doesn't explicitly set the transport (which is usually the case for trpc protocol), | ||
// attempt to switch to the tnet transport. | ||
if o.Transport == nil { | ||
// Only use tnet transport with TCP and trpc. | ||
if (o.network == "tcp" || | ||
o.network == "tcp4" || | ||
o.network == "tcp6") && | ||
o.protocol == "trpc" { | ||
log.Infof("service %s with network %s and protocol %s is empowered with tnet! 🤩 "+ | ||
"you can always use 'go get -u trpc.group/trpc-go/trpc-go@v0.15.1' "+ | ||
"(without the '-tnet-enabled' suffix) , or set 'transport: go-net' in your "+ | ||
"trpc_go.yaml's service configuration to switch to the vanilla version", | ||
o.ServiceName, o.network, o.protocol) | ||
return tnet.DefaultServerTransport | ||
} | ||
log.Infof("service: %s, tnet is not enabled by default for the network %s and protocol %s, 🧐 "+ | ||
"fallback to go-net transport, it is either because tnet does not support them or "+ | ||
"we haven't fully test for some third-party protocols, you can set 'transport: tnet' "+ | ||
"in your service configuration to force using tnet and test it at your own risk", | ||
o.ServiceName, o.network, o.protocol) | ||
return transport.DefaultServerTransport | ||
} | ||
return o.Transport | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//go:build !(linux && amd64) | ||
// +build !linux !amd64 | ||
|
||
package server | ||
|
||
import "trpc.group/trpc-go/trpc-go/transport" | ||
|
||
func attemptSwitchingTransport(o *Options) transport.ServerTransport { | ||
if o.Transport == nil { | ||
return transport.DefaultServerTransport | ||
} | ||
return o.Transport | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters