github.com/polarismesh/polaris@v1.17.8/service/healthcheck/client.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 healthcheck
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  
    24  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    25  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    26  
    27  	api "github.com/polarismesh/polaris/common/api/v1"
    28  	"github.com/polarismesh/polaris/plugin"
    29  )
    30  
    31  const clientPrefix = "c_"
    32  
    33  func toClientId(instanceId string) string {
    34  	return clientPrefix + instanceId
    35  }
    36  
    37  func (s *Server) doReportByClient(ctx context.Context, client *apiservice.Client) *apiservice.Response {
    38  	if len(s.checkers) == 0 {
    39  		return api.NewResponse(apimodel.Code_HealthCheckNotOpen)
    40  	}
    41  	checker, ok := s.checkers[int32(apiservice.HealthCheck_HEARTBEAT)]
    42  	if !ok {
    43  		return api.NewClientResponse(apimodel.Code_HeartbeatTypeNotFound, client)
    44  	}
    45  	request := &plugin.ReportRequest{
    46  		QueryRequest: plugin.QueryRequest{
    47  			InstanceId: toClientId(client.GetId().GetValue()),
    48  			Host:       client.GetHost().GetValue(),
    49  		},
    50  		LocalHost:  s.localHost,
    51  		CurTimeSec: time.Now().Unix() - s.timeAdjuster.GetDiff(),
    52  	}
    53  	err := checker.Report(ctx, request)
    54  	if err != nil {
    55  		log.Errorf("[Heartbeat][Server]fail to do report client for %s, id is %s, err is %v",
    56  			client.GetHost().GetValue(), client.GetId().GetValue(), err)
    57  		return api.NewClientResponse(apimodel.Code_HeartbeatException, client)
    58  	}
    59  	return api.NewClientResponse(apimodel.Code_ExecuteSuccess, client)
    60  }