github.com/polarismesh/polaris@v1.17.8/plugin/healthchecker.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package plugin 19 20 import ( 21 "context" 22 "os" 23 "sync" 24 ) 25 26 // ReportRequest report heartbeat request 27 type ReportRequest struct { 28 QueryRequest 29 LocalHost string 30 CurTimeSec int64 31 Count int64 32 } 33 34 // CheckRequest check heartbeat request 35 type CheckRequest struct { 36 QueryRequest 37 ExpireDurationSec uint32 38 CurTimeSec func() int64 39 } 40 41 // CheckResponse check heartbeat response 42 type CheckResponse struct { 43 Healthy bool 44 LastHeartbeatTimeSec int64 45 StayUnchanged bool 46 Regular bool 47 } 48 49 // QueryRequest query heartbeat request 50 type QueryRequest struct { 51 InstanceId string 52 Host string 53 Port uint32 54 Healthy bool 55 } 56 57 // QueryResponse query heartbeat response 58 type QueryResponse struct { 59 Server string 60 Exists bool 61 LastHeartbeatSec int64 62 Count int64 63 } 64 65 // AddCheckRequest add check request 66 type AddCheckRequest struct { 67 Instances []string 68 LocalHost string 69 } 70 71 // HealthCheckType health check type 72 type HealthCheckType int32 73 74 const ( 75 HealthCheckerHeartbeat HealthCheckType = iota + 1 76 ) 77 78 var ( 79 healthCheckOnce = &sync.Once{} 80 ) 81 82 // HealthChecker health checker plugin interface 83 type HealthChecker interface { 84 Plugin 85 // Type for health check plugin, only one same type plugin is allowed 86 Type() HealthCheckType 87 // Report process heartbeat info report 88 Report(ctx context.Context, request *ReportRequest) error 89 // Check process the instance check 90 Check(request *CheckRequest) (*CheckResponse, error) 91 // Query queries the heartbeat time 92 Query(ctx context.Context, request *QueryRequest) (*QueryResponse, error) 93 // Suspend health checker for entire expired duration manually 94 Suspend() 95 // SuspendTimeSec get the suspend time in seconds 96 SuspendTimeSec() int64 97 // Delete delete the id 98 Delete(ctx context.Context, id string) error 99 // DebugHandlers return debug handlers 100 DebugHandlers() []DebugHandler 101 } 102 103 // GetHealthChecker get the health checker by name 104 func GetHealthChecker(name string, cfg *ConfigEntry) HealthChecker { 105 plugin, exist := pluginSet[name] 106 if !exist { 107 return nil 108 } 109 110 healthCheckOnce.Do(func() { 111 if err := plugin.Initialize(cfg); err != nil { 112 log.Errorf("HealthChecker plugin init err: %s", err.Error()) 113 os.Exit(-1) 114 } 115 }) 116 117 return plugin.(HealthChecker) 118 }