-
Notifications
You must be signed in to change notification settings - Fork 837
/
Copy pathcopy.go
105 lines (93 loc) · 2.41 KB
/
copy.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rpcinfo
type plainRPCInfo struct {
// use anonymous structs to force objects to be read-only
from struct{ EndpointInfo }
to struct{ EndpointInfo }
inv struct{ Invocation }
cfg struct{ RPCConfig }
}
func (p *plainRPCInfo) From() EndpointInfo {
return &p.from
}
func (p *plainRPCInfo) To() EndpointInfo {
return &p.to
}
func (p *plainRPCInfo) Invocation() Invocation {
return &p.inv
}
func (p *plainRPCInfo) Config() RPCConfig {
return &p.cfg
}
func (p *plainRPCInfo) Stats() RPCStats {
return nil
}
func freeze(ri RPCInfo) RPCInfo {
p := new(plainRPCInfo)
p.from.EndpointInfo = copyEndpointInfo(ri.From())
p.to.EndpointInfo = copyEndpointInfo(ri.To())
p.inv.Invocation = copyInvocation(ri.Invocation())
p.cfg.RPCConfig = copyRPCConfig(ri.Config())
return p
}
func copyMap(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
func copyEndpointInfo(ei EndpointInfo) EndpointInfo {
if ei == nil {
return nil
}
p := &endpointInfo{
serviceName: ei.ServiceName(),
method: ei.Method(),
address: ei.Address(),
}
if v, ok := ei.(*endpointInfo); ok {
p.tags = copyMap(v.tags)
} else {
p.tags = make(map[string]string)
}
return p
}
func copyInvocation(i Invocation) Invocation {
if i == nil {
return nil
}
return &invocation{
packageName: i.PackageName(),
serviceName: i.ServiceName(),
methodName: i.MethodName(),
seqID: i.SeqID(),
}
}
func copyRPCConfig(cfg RPCConfig) RPCConfig {
if cfg == nil {
return nil
}
return &rpcConfig{
rpcTimeout: cfg.RPCTimeout(),
connectTimeout: cfg.ConnectTimeout(),
readWriteTimeout: cfg.ReadWriteTimeout(),
ioBufferSize: cfg.IOBufferSize(),
transportProtocol: cfg.TransportProtocol(),
interactionMode: cfg.InteractionMode(),
}
}