github.com/cloudwego/kitex@v0.9.0/pkg/rpcinfo/copy.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package rpcinfo
    18  
    19  type plainRPCInfo struct {
    20  	// use anonymous structs to force objects to be read-only
    21  	from struct{ EndpointInfo }
    22  	to   struct{ EndpointInfo }
    23  	inv  struct{ Invocation }
    24  	cfg  struct{ RPCConfig }
    25  }
    26  
    27  func (p *plainRPCInfo) From() EndpointInfo {
    28  	return &p.from
    29  }
    30  
    31  func (p *plainRPCInfo) To() EndpointInfo {
    32  	return &p.to
    33  }
    34  
    35  func (p *plainRPCInfo) Invocation() Invocation {
    36  	return &p.inv
    37  }
    38  
    39  func (p *plainRPCInfo) Config() RPCConfig {
    40  	return &p.cfg
    41  }
    42  
    43  func (p *plainRPCInfo) Stats() RPCStats {
    44  	return nil
    45  }
    46  
    47  func freeze(ri RPCInfo) RPCInfo {
    48  	p := new(plainRPCInfo)
    49  	p.from.EndpointInfo = copyEndpointInfo(ri.From())
    50  	p.to.EndpointInfo = copyEndpointInfo(ri.To())
    51  	p.inv.Invocation = copyInvocation(ri.Invocation())
    52  	p.cfg.RPCConfig = copyRPCConfig(ri.Config())
    53  	return p
    54  }
    55  
    56  func copyMap(src map[string]string) map[string]string {
    57  	dst := make(map[string]string, len(src))
    58  	for k, v := range src {
    59  		dst[k] = v
    60  	}
    61  	return dst
    62  }
    63  
    64  func copyEndpointInfo(ei EndpointInfo) EndpointInfo {
    65  	if ei == nil {
    66  		return nil
    67  	}
    68  	p := &endpointInfo{
    69  		serviceName: ei.ServiceName(),
    70  		method:      ei.Method(),
    71  		address:     ei.Address(),
    72  	}
    73  	if v, ok := ei.(*endpointInfo); ok {
    74  		p.tags = copyMap(v.tags)
    75  	} else {
    76  		p.tags = make(map[string]string)
    77  	}
    78  	return p
    79  }
    80  
    81  func copyInvocation(i Invocation) Invocation {
    82  	if i == nil {
    83  		return nil
    84  	}
    85  	return &invocation{
    86  		packageName: i.PackageName(),
    87  		serviceName: i.ServiceName(),
    88  		methodName:  i.MethodName(),
    89  		seqID:       i.SeqID(),
    90  	}
    91  }
    92  
    93  func copyRPCConfig(cfg RPCConfig) RPCConfig {
    94  	if cfg == nil {
    95  		return nil
    96  	}
    97  	return &rpcConfig{
    98  		rpcTimeout:        cfg.RPCTimeout(),
    99  		connectTimeout:    cfg.ConnectTimeout(),
   100  		readWriteTimeout:  cfg.ReadWriteTimeout(),
   101  		ioBufferSize:      cfg.IOBufferSize(),
   102  		transportProtocol: cfg.TransportProtocol(),
   103  		interactionMode:   cfg.InteractionMode(),
   104  	}
   105  }