|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "net" |
| 25 | + "net/http" |
| 26 | + "strings" |
| 27 | + "time" |
| 28 | + |
| 29 | + "dubbo.apache.org/dubbo-go/v3/client" |
| 30 | + |
| 31 | + _ "dubbo.apache.org/dubbo-go/v3/imports" |
| 32 | + "github.com/dubbogo/gost/log/logger" |
| 33 | + |
| 34 | + greet "github.com/apache/dubbo-go-samples/direct/proto" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + tripleAddr = "127.0.0.1:20000" |
| 39 | + probeBaseURL = "http://127.0.0.1:22222" |
| 40 | + |
| 41 | + tcpReadyTimeout = 20 * time.Second |
| 42 | + probeReadyTimeout = 40 * time.Second |
| 43 | + requestTimeout = 2 * time.Second |
| 44 | +) |
| 45 | + |
| 46 | +func main() { |
| 47 | + ctx := context.Background() |
| 48 | + |
| 49 | + if err := waitTCPReady(tripleAddr, tcpReadyTimeout); err != nil { |
| 50 | + logger.Fatalf("triple port is not ready: %v", err) |
| 51 | + panic("triple port is not ready") |
| 52 | + } |
| 53 | + logger.Infof("triple port is ready: %s", tripleAddr) |
| 54 | + |
| 55 | + if err := waitTCPReady("127.0.0.1:22222", tcpReadyTimeout); err != nil { |
| 56 | + logger.Fatalf("probe port is not ready: %v", err) |
| 57 | + panic("probe port is not ready") |
| 58 | + } |
| 59 | + logger.Info("probe port is ready: 127.0.0.1:22222") |
| 60 | + |
| 61 | + if err := expectHTTPStatus(probeBaseURL+"/live", http.StatusOK, requestTimeout); err != nil { |
| 62 | + logger.Fatalf("/live check failed: %v", err) |
| 63 | + panic("/live check failed") |
| 64 | + } |
| 65 | + logger.Info("/live is healthy") |
| 66 | + |
| 67 | + if err := waitHTTPStatus(probeBaseURL+"/ready", http.StatusOK, probeReadyTimeout, requestTimeout); err != nil { |
| 68 | + logger.Fatalf("/ready did not become healthy: %v", err) |
| 69 | + panic("/ready did not become healthy") |
| 70 | + } |
| 71 | + logger.Info("/ready is healthy") |
| 72 | + |
| 73 | + if err := waitHTTPStatus(probeBaseURL+"/startup", http.StatusOK, probeReadyTimeout, requestTimeout); err != nil { |
| 74 | + logger.Fatalf("/startup did not become healthy: %v", err) |
| 75 | + panic("/startup did not become healthy") |
| 76 | + } |
| 77 | + logger.Info("/startup is healthy") |
| 78 | + |
| 79 | + if err := callGreet(ctx); err != nil { |
| 80 | + logger.Fatalf("greet rpc check failed: %v", err) |
| 81 | + panic("greet rpc check failed") |
| 82 | + } |
| 83 | + logger.Info("probe sample integration checks passed") |
| 84 | +} |
| 85 | + |
| 86 | +func callGreet(ctx context.Context) error { |
| 87 | + cli, err := client.NewClient( |
| 88 | + client.WithClientURL("tri://" + tripleAddr), |
| 89 | + ) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("create client: %w", err) |
| 92 | + } |
| 93 | + |
| 94 | + svc, err := greet.NewGreetService(cli) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("create greet service: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + rpcCtx, cancel := context.WithTimeout(ctx, 3*time.Second) |
| 100 | + defer cancel() |
| 101 | + |
| 102 | + resp, err := svc.Greet(rpcCtx, &greet.GreetRequest{Name: "probe-check"}) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("invoke greet: %w", err) |
| 105 | + } |
| 106 | + if strings.TrimSpace(resp.Greeting) == "" { |
| 107 | + return fmt.Errorf("empty greet response") |
| 108 | + } |
| 109 | + logger.Infof("greet rpc succeeded: %s", resp.Greeting) |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func waitTCPReady(addr string, timeout time.Duration) error { |
| 114 | + deadline := time.Now().Add(timeout) |
| 115 | + for { |
| 116 | + conn, err := net.DialTimeout("tcp", addr, 1*time.Second) |
| 117 | + if err == nil { |
| 118 | + _ = conn.Close() |
| 119 | + return nil |
| 120 | + } |
| 121 | + if time.Now().After(deadline) { |
| 122 | + return fmt.Errorf("tcp %s not ready within %s: %w", addr, timeout, err) |
| 123 | + } |
| 124 | + time.Sleep(300 * time.Millisecond) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func waitHTTPStatus(url string, expected int, timeout, reqTimeout time.Duration) error { |
| 129 | + deadline := time.Now().Add(timeout) |
| 130 | + var lastErr error |
| 131 | + for { |
| 132 | + err := expectHTTPStatus(url, expected, reqTimeout) |
| 133 | + if err == nil { |
| 134 | + return nil |
| 135 | + } |
| 136 | + lastErr = err |
| 137 | + if time.Now().After(deadline) { |
| 138 | + return fmt.Errorf("url %s not ready within %s: %w", url, timeout, lastErr) |
| 139 | + } |
| 140 | + time.Sleep(500 * time.Millisecond) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func expectHTTPStatus(url string, expected int, reqTimeout time.Duration) error { |
| 145 | + client := http.Client{Timeout: reqTimeout} |
| 146 | + resp, err := client.Get(url) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + defer func() { _ = resp.Body.Close() }() |
| 151 | + |
| 152 | + body, _ := io.ReadAll(resp.Body) |
| 153 | + if resp.StatusCode != expected { |
| 154 | + return fmt.Errorf("expect status %d but got %d, body=%s", expected, resp.StatusCode, strings.TrimSpace(string(body))) |
| 155 | + } |
| 156 | + return nil |
| 157 | +} |
0 commit comments