AlexStocks commented on code in PR #158: URL: https://github.com/apache/dubbo-go-pixiu-samples/pull/158#discussion_r3488965513
########## dubbogo/simple/traffic/server/app/main.go: ########## @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "log" + "net/http" + "os" + "strings" + "sync" +) + +const ( + trafficV1AddrEnv = "TRAFFIC_V1_ADDR" + trafficV2AddrEnv = "TRAFFIC_V2_ADDR" + trafficV3AddrEnv = "TRAFFIC_V3_ADDR" + + defaultTrafficV1Addr = ":1315" + defaultTrafficV2Addr = ":1316" + defaultTrafficV3Addr = ":1317" +) + +type trafficServer struct { + addr string + label string +} + +func main() { + servers := []trafficServer{ + {addr: envOrDefault(trafficV1AddrEnv, defaultTrafficV1Addr), label: "v1"}, + {addr: envOrDefault(trafficV2AddrEnv, defaultTrafficV2Addr), label: "v2"}, + {addr: envOrDefault(trafficV3AddrEnv, defaultTrafficV3Addr), label: "v3"}, + } + + var wg sync.WaitGroup + errCh := make(chan error, len(servers)) + for _, server := range servers { + wg.Add(1) + go func(server trafficServer) { + defer wg.Done() + errCh <- startServer(server.addr, server.label) + }(server) + } + + log.Println("All traffic servers started") + go func() { + wg.Wait() + close(errCh) + }() + + for err := range errCh { + if err != nil { + log.Fatalf("traffic server stopped: %v", err) + } + } +} + +func envOrDefault(key, fallback string) string { + if value := os.Getenv(key); value != "" { + return value + } + return fallback +} + +func startServer(addr, label string) error { + mux := http.NewServeMux() + routers := []string{"/user", "/user/pixiu", "/prefix", "/health"} + for _, router := range routers { + msg := router[strings.LastIndex(router, "/")+1:] + mux.HandleFunc(router, func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, `{"server": "%s","message":"%s","status":200}`, label, msg) Review Comment: [P1] `golangci-lint run ./...` 已在这行报 `errcheck`: 新增 sample server 忽略了 `fmt.Fprintf` 的返回错误,导致当前 PR 的改动行触发静态检查失败。建议改成 `if _, err := fmt.Fprintf(...); err != nil { ... }`,或者在 sample 场景下用 `_, _ = fmt.Fprintf(...)` 明确说明这里选择忽略。 ########## dubbogo/simple/traffic/test/pixiu_header_test.go: ########## @@ -0,0 +1,75 @@ +//go:build manual + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package test + +import ( + "io" + "net/http" + "strings" + "testing" + "time" +) + +import ( + "github.com/stretchr/testify/assert" +) + +func TestHeaderGET1(t *testing.T) { + url := trafficURL("/user") + client := &http.Client{Timeout: 5 * time.Second} + req, err := http.NewRequest("GET", url, nil) + assert.NoError(t, err) + req.Header.Add("X-A", "t1") + resp, err := client.Do(req) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.Equal(t, 200, resp.StatusCode) + s, _ := io.ReadAll(resp.Body) Review Comment: [P1] 新增的 header 手工测试读取 `resp.Body` 后没有关闭,下面两个用例也有同样模式。集成/手工测试批量运行时会保留连接资源,建议在确认 `resp != nil` 后 `defer resp.Body.Close()`,并按项目 lint 规则处理或显式忽略 close error。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
