github.com/cloudwego/kitex@v0.9.0/pkg/remote/trans/common.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 trans 18 19 import ( 20 "context" 21 "fmt" 22 "net" 23 "time" 24 25 "github.com/cloudwego/kitex/pkg/remote" 26 "github.com/cloudwego/kitex/pkg/rpcinfo" 27 "github.com/cloudwego/kitex/pkg/serviceinfo" 28 ) 29 30 var readMoreTimeout = 5 * time.Millisecond 31 32 // Extension is the interface that trans extensions need to implement, it will make the extension of trans more easily. 33 // Normally if we want to extend transport layer we need to implement the trans interfaces which are defined in trans_handler.go. 34 // In fact most code logic is similar in same mode, so the Extension interface is the the differentiated part that need to 35 // be implemented separately. 36 // The default common trans implement is in default_client_handler.go and default_server_handler.go. 37 type Extension interface { 38 SetReadTimeout(ctx context.Context, conn net.Conn, cfg rpcinfo.RPCConfig, role remote.RPCRole) 39 NewWriteByteBuffer(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer 40 NewReadByteBuffer(ctx context.Context, conn net.Conn, msg remote.Message) remote.ByteBuffer 41 ReleaseBuffer(remote.ByteBuffer, error) error 42 IsTimeoutErr(error) bool 43 // IsRemoteClosedErr is to check if the error caused by connection closed when output log or report metric 44 IsRemoteClosedErr(error) bool 45 } 46 47 // GetReadTimeout is to make the read timeout longer, it is better for proxy case to receive error resp. 48 func GetReadTimeout(cfg rpcinfo.RPCConfig) time.Duration { 49 if cfg.RPCTimeout() <= 0 { 50 return 0 51 } 52 return cfg.RPCTimeout() + readMoreTimeout 53 } 54 55 // GetMethodInfo is used to get method info from serviceinfo.MethodInfo by method name/ 56 func GetMethodInfo(ri rpcinfo.RPCInfo, svcInfo *serviceinfo.ServiceInfo) (serviceinfo.MethodInfo, error) { 57 methodName := ri.Invocation().MethodName() 58 methodInfo := svcInfo.MethodInfo(methodName) 59 if methodInfo != nil { 60 return methodInfo, nil 61 } 62 return nil, remote.NewTransErrorWithMsg(remote.UnknownMethod, fmt.Sprintf("unknown method %s", methodName)) 63 } 64 65 // MuxEnabledFlag is used to determine whether a serverHandlerFactory is multiplexing. 66 type MuxEnabledFlag interface { 67 MuxEnabled() bool 68 } 69 70 // GetDefaultSvcInfo is used to get one ServiceInfo from map which is supposed to have one ServiceInfo 71 func GetDefaultSvcInfo(svcMap map[string]*serviceinfo.ServiceInfo) *serviceinfo.ServiceInfo { 72 for _, svcInfo := range svcMap { 73 return svcInfo 74 } 75 return nil 76 }