github.com/matrixorigin/matrixone@v1.2.0/pkg/common/morpc/message.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package morpc 16 17 import ( 18 "time" 19 20 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 ) 22 23 const ( 24 internalTimeout = time.Second * 10 25 oneWayTimeout = time.Second * 600 26 ) 27 28 // Timeout return true if the message is timeout 29 func (m RPCMessage) Timeout() bool { 30 select { 31 case <-m.Ctx.Done(): 32 return true 33 default: 34 return false 35 } 36 } 37 38 // GetTimeoutFromContext returns the timeout duration from context. 39 func (m RPCMessage) GetTimeoutFromContext() (time.Duration, error) { 40 if m.internal { 41 return internalTimeout, nil 42 } 43 if m.oneWay { 44 return oneWayTimeout, nil 45 } 46 47 d, ok := m.Ctx.Deadline() 48 if !ok { 49 return 0, moerr.NewInvalidInputNoCtx("timeout deadline not set") 50 } 51 now := time.Now() 52 if now.After(d) { 53 return 0, moerr.NewInvalidInputNoCtx("timeout has invalid deadline") 54 } 55 return d.Sub(now), nil 56 }