github.com/polarismesh/polaris@v1.17.8/plugin/healthchecker/leader/debug.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 leader
    19  
    20  import (
    21  	"encoding/json"
    22  	"net/http"
    23  
    24  	"github.com/polarismesh/polaris/common/utils"
    25  )
    26  
    27  func handleDescribeLeaderInfo(checker *LeaderHealthChecker) func(http.ResponseWriter, *http.Request) {
    28  	return func(resp http.ResponseWriter, req *http.Request) {
    29  		if !checker.isInitialize() {
    30  			resp.WriteHeader(http.StatusTooEarly)
    31  			_, _ = resp.Write([]byte("LeaderChecker not initialize"))
    32  			return
    33  		}
    34  
    35  		ret := map[string]interface{}{}
    36  		if checker.isLeader() {
    37  			ret["leader"] = utils.LocalHost
    38  		} else {
    39  			if checker.remote != nil {
    40  				ret["leader"] = checker.remote.Host()
    41  			}
    42  		}
    43  		ret["self"] = utils.LocalHost
    44  		ret["lastLeaderRefreshTimeSec"] = checker.LeaderChangeTimeSec()
    45  
    46  		data, _ := json.Marshal(ret)
    47  		resp.WriteHeader(http.StatusOK)
    48  		_, _ = resp.Write(data)
    49  	}
    50  }
    51  
    52  func handleDescribeBeatCache(checker *LeaderHealthChecker) func(http.ResponseWriter, *http.Request) {
    53  	return func(resp http.ResponseWriter, req *http.Request) {
    54  		if !checker.isInitialize() {
    55  			resp.WriteHeader(http.StatusTooEarly)
    56  			_, _ = resp.Write([]byte("LeaderChecker not initialize"))
    57  			return
    58  		}
    59  
    60  		ret := map[string]interface{}{}
    61  		ret["self"] = utils.LocalHost
    62  		if checker.isLeader() {
    63  			ret["data"] = checker.self.(*LocalPeer).Cache.Snapshot()
    64  		} else {
    65  			ret["data"] = "Not Leader"
    66  		}
    67  
    68  		data, _ := json.Marshal(ret)
    69  		resp.WriteHeader(http.StatusOK)
    70  		_, _ = resp.Write(data)
    71  	}
    72  }