Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transport: enable tnet by default #56

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions client/client_linux.go
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)
}
13 changes: 13 additions & 0 deletions client/client_nolinux.go
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
}
6 changes: 5 additions & 1 deletion client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ func (cfg *BackendConfig) genOptions() (*Options, error) {
opts.CompressType = cfg.Compression
}

// Reset the transport to check if the user has specified any transport.
opts.Transport = nil
WithTransport(transport.GetClientTransport(cfg.Transport))(opts)
WithStreamTransport(transport.GetClientStreamTransport(cfg.Transport))(opts)
WithProtocol(cfg.Protocol)(opts)
WithNetwork(cfg.Network)(opts)
WithTransport(transport.GetClientTransport(cfg.Transport))(opts)
opts.Transport = attemptSwitchingTransport(opts)
WithPassword(cfg.Password)(opts)
WithTLS(cfg.TLSCert, cfg.TLSKey, cfg.CACert, cfg.TLSServerName)(opts)
if cfg.Protocol != "" && opts.Codec == nil {
Expand Down
2 changes: 2 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Options struct {
endpoint string // The same as service name if target is not set.

Network string
Protocol string
CallType codec.RequestType // Type of request, referring to transport.RequestType.
CallOptions []transport.RoundTripOption // Options for client transport to call server.
Transport transport.ClientTransport
Expand Down Expand Up @@ -361,6 +362,7 @@ func WithProtocol(s string) Option {
if s == "" {
return
}
o.Protocol = s
o.Codec = codec.GetClient(s)
if b := transport.GetFramerBuilder(s); b != nil {
o.CallOptions = append(o.CallOptions,
Expand Down
28 changes: 16 additions & 12 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,17 @@ type service struct {
// It will use transport.DefaultServerTransport unless Option WithTransport()
// is called to replace its transport.ServerTransport plugin.
var New = func(opts ...Option) Service {
const (
invalidCompressType = -1
invalidSerializationType = -1
)
o := defaultOptions()
s := &service{
opts: &Options{
protocol: "unknown-protocol",
ServiceName: "empty-name",
CurrentSerializationType: invalidSerializationType,
CurrentCompressType: invalidCompressType,
Transport: transport.DefaultServerTransport,
StreamTransport: transport.DefaultServerStreamTransport,
},
opts: o,
handlers: make(map[string]Handler),
streamHandlers: make(map[string]StreamHandler),
streamInfo: make(map[string]*StreamServerInfo),
}
for _, o := range opts {
o(s.opts)
}
o.Transport = attemptSwitchingTransport(o)
if !s.opts.handlerSet {
// if handler is not set, pass the service (which implements Handler interface)
// as handler of transport plugin.
Expand Down Expand Up @@ -589,3 +580,16 @@ func checkProcessStatus() (isGracefulRestart, isParentalProcess bool) {
}
return true, ppid == os.Getpid()
}

func defaultOptions() *Options {
const (
invalidSerializationType = -1
invalidCompressType = -1
)
return &Options{
protocol: "unknown-protocol",
ServiceName: "empty-name",
CurrentSerializationType: invalidSerializationType,
CurrentCompressType: invalidCompressType,
}
}
36 changes: 36 additions & 0 deletions server/service_linux.go
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
}
13 changes: 13 additions & 0 deletions server/service_nolinux.go
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
}
5 changes: 5 additions & 0 deletions transport/client_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"trpc.group/trpc-go/trpc-go/pool/multiplexed"
)

func init() {
RegisterClientTransport(transportName, DefaultClientTransport)
RegisterClientStreamTransport(transportName, DefaultClientStreamTransport)
}

// DefaultClientTransport is the default client transport.
var DefaultClientTransport = NewClientTransport()

Expand Down
6 changes: 6 additions & 0 deletions transport/server_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ import (
"trpc.group/trpc-go/trpc-go/log"
)

const transportName = "go-net"

func init() {
RegisterServerTransport(transportName, DefaultServerTransport)
}

const (
// EnvGraceRestart is the flag of graceful restart.
EnvGraceRestart = "TRPC_IS_GRACEFUL"
Expand Down
Loading