github.com/matrixorigin/matrixone@v1.2.0/pkg/proxy/heartbeat.go (about) 1 // Copyright 2021 - 2023 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 proxy 16 17 import ( 18 "context" 19 "time" 20 21 pb "github.com/matrixorigin/matrixone/pkg/pb/logservice" 22 "go.uber.org/zap" 23 ) 24 25 func (s *Server) heartbeat(ctx context.Context) { 26 if s.config.HAKeeper.HeartbeatInterval.Duration == 0 { 27 panic("invalid heartbeat interval") 28 } 29 ticker := time.NewTicker(s.config.HAKeeper.HeartbeatInterval.Duration) 30 defer ticker.Stop() 31 for { 32 select { 33 case <-ctx.Done(): 34 return 35 case <-ticker.C: 36 s.doHeartbeat(ctx) 37 select { 38 case <-ctx.Done(): 39 return 40 default: 41 } 42 } 43 } 44 } 45 46 func (s *Server) doHeartbeat(ctx context.Context) { 47 ctx, cancel := context.WithTimeout(ctx, s.config.HAKeeper.HeartbeatTimeout.Duration) 48 defer cancel() 49 _, err := s.haKeeperClient.SendProxyHeartbeat(ctx, pb.ProxyHeartbeat{ 50 UUID: s.config.UUID, 51 ListenAddress: s.config.ListenAddress, 52 ConfigData: s.configData.GetData(), 53 }) 54 if err != nil { 55 s.runtime.Logger().Error("failed to send heartbeat", zap.Error(err)) 56 } 57 s.configData.DecrCount() 58 }