github.com/polarismesh/polaris@v1.17.8/test/outlier/backend/main.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 main
    19  
    20  import (
    21  	"fmt"
    22  	"net/http"
    23  	"os"
    24  	"time"
    25  )
    26  
    27  func main() {
    28  	status := 0
    29  	start := time.Now()
    30  	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    31  		fmt.Printf("%v / request\n", time.Now().Format("2006-01-02 15:04:05"))
    32  		host, _ := os.Hostname()
    33  		if status == 1 {
    34  			w.WriteHeader(http.StatusInternalServerError)
    35  			fmt.Fprintf(w, "[%v] Internal Server Error, interval: %v", host,
    36  				time.Now().Sub(start)/time.Nanosecond)
    37  			start = time.Now()
    38  		} else {
    39  			fmt.Fprintln(w, fmt.Sprintf("[%v] hello", host))
    40  		}
    41  	})
    42  	http.HandleFunc("/fail", func(w http.ResponseWriter, r *http.Request) {
    43  		status = 1
    44  		w.Write([]byte("ok"))
    45  	})
    46  	http.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
    47  		status = 0
    48  		w.Write([]byte("ok"))
    49  	})
    50  	http.HandleFunc("/healthCheck", func(w http.ResponseWriter, r *http.Request) {
    51  		fmt.Printf("%v /healthCheck request\n", time.Now().Format("2006-01-02 15:04:05"))
    52  		if status == 1 {
    53  			time.Sleep(5 * time.Second)
    54  		} else {
    55  			w.Write([]byte("ok"))
    56  		}
    57  	})
    58  
    59  	http.ListenAndServe(":8090", nil)
    60  }