-
Notifications
You must be signed in to change notification settings - Fork 94
/
client_transport.go
91 lines (77 loc) · 2.67 KB
/
client_transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package transport
import (
"context"
"fmt"
"trpc.group/trpc-go/trpc-go/errs"
"trpc.group/trpc-go/trpc-go/pool/connpool"
"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()
// NewClientTransport creates a new ClientTransport.
func NewClientTransport(opt ...ClientTransportOption) ClientTransport {
r := newClientTransport(opt...)
return &r
}
// newClientTransport creates a new clientTransport.
func newClientTransport(opt ...ClientTransportOption) clientTransport {
// the default options.
opts := &ClientTransportOptions{}
// use opt to modify the opts.
for _, o := range opt {
o(opts)
}
return clientTransport{opts: opts}
}
// clientTransport is the implementation details of client transport, such as tcp/udp roundtrip.
type clientTransport struct {
// Transport has two kinds of options.
// One is ClientTransportOptions, which is the option for transport, and is valid for all
// RoundTrip requests. The framework does not care about the parameters required for specific
// implementation.
// The other is RoundTripOptions, which is the option of the current request, such as address,
// which has different values for different requests. It can be configured and passed in by the
// upper layer of the framework.
opts *ClientTransportOptions
}
// RoundTrip sends client requests.
func (c *clientTransport) RoundTrip(ctx context.Context, req []byte,
roundTripOpts ...RoundTripOption) (rsp []byte, err error) {
// default value.
opts := &RoundTripOptions{
Pool: connpool.DefaultConnectionPool,
Multiplexed: multiplexed.DefaultMultiplexedPool,
}
// Use roundTripOpts to modify opts.
for _, o := range roundTripOpts {
o(opts)
}
if opts.EnableMultiplexed {
return c.multiplexed(ctx, req, opts)
}
switch opts.Network {
case "tcp", "tcp4", "tcp6", "unix":
return c.tcpRoundTrip(ctx, req, opts)
case "udp", "udp4", "udp6":
return c.udpRoundTrip(ctx, req, opts)
default:
return nil, errs.NewFrameError(errs.RetClientConnectFail,
fmt.Sprintf("client transport: network %s not support", opts.Network))
}
}