github.com/polarismesh/polaris@v1.17.8/test/outlier/frontend/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 "io/ioutil" 23 "net/http" 24 "time" 25 ) 26 27 func main() { 28 interval := 500 29 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 30 data, err := request("http://outlier-backend:8090") 31 if err != nil { 32 fmt.Fprintf(w, "backend fail: %v", err) 33 return 34 } 35 values := r.URL.Query() 36 val := values.Get("interval") 37 fmt.Sscanf(val, "%v", &interval) 38 if interval == 0 { 39 interval = 500 40 } 41 w.Write(data) 42 }) 43 go func() { 44 for { 45 data, err := request("http://outlier-backend:8090") 46 if err != nil { 47 fmt.Printf("backend fail: %v\n", err) 48 } else { 49 fmt.Printf("%v response: %v\n", 50 time.Now().Format("2006-01-02 15:04:05"), 51 string(data)) 52 } 53 time.Sleep(time.Duration(interval) * time.Millisecond) 54 } 55 }() 56 57 http.ListenAndServe(":8080", nil) 58 } 59 60 func request(url string) ([]byte, error) { 61 resp, err := http.Get(url) 62 if err != nil { 63 fmt.Printf("Error making the request: %s\n", err.Error()) 64 return nil, err 65 } 66 defer resp.Body.Close() 67 68 body, err := ioutil.ReadAll(resp.Body) 69 if err != nil { 70 fmt.Printf("Error reading the response body: %s\n", err.Error()) 71 return nil, err 72 } 73 return body, nil 74 }