AlexStocks commented on code in PR #1017: URL: https://github.com/apache/dubbo-go-pixiu/pull/1017#discussion_r3829387534
########## pkg/filter/http/grpcproxy/connection_manager.go: ########## @@ -0,0 +1,200 @@ +/* + * 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 grpcproxy + +import ( + "context" + "fmt" + "sync" + "time" +) + +import ( + "golang.org/x/sync/singleflight" + + "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/credentials/insecure" +) + +const defaultGRPCDialTimeout = 5 * time.Second + +type grpcConnectionDialer func(context.Context, string) (*grpc.ClientConn, error) + +// grpcConnectionManager owns long-lived backend connections for the HTTP gRPC +// proxy. A grpc.ClientConn is safe for concurrent use and multiplexes calls +// over HTTP/2, so a sync.Pool is both unnecessary and incorrect here. +type grpcConnectionManager struct { + connections sync.Map + creates singleflight.Group + dial grpcConnectionDialer + dialTimeout time.Duration + onRemove func(*grpc.ClientConn) + + mu sync.Mutex + closed bool +} + +func newGRPCConnectionManager() *grpcConnectionManager { + return &grpcConnectionManager{ + dial: dialGRPCConnection, + dialTimeout: defaultGRPCDialTimeout, + } +} + +func dialGRPCConnection(ctx context.Context, endpoint string) (*grpc.ClientConn, error) { + return grpc.DialContext( //nolint:staticcheck // SA1019: the context is required to enforce the dial timeout. + ctx, + endpoint, + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) +} + +func (m *grpcConnectionManager) Get(ctx context.Context, key, endpoint string) (*grpc.ClientConn, error) { + if key == "" || endpoint == "" { + return nil, fmt.Errorf("grpc connection key and endpoint must not be empty") + } + if ctx == nil { + ctx = context.Background() + } + + if conn, ok := m.loadHealthy(key); ok { + return conn, nil + } + + result := m.creates.DoChan(key, func() (any, error) { + if conn, ok := m.loadHealthy(key); ok { + return conn, nil + } + + m.mu.Lock() + if m.closed { + m.mu.Unlock() + return nil, fmt.Errorf("grpc connection manager is closed") + } + dial := m.dial + dialTimeout := m.dialTimeout + m.mu.Unlock() + + dialCtx, cancel := context.WithTimeout(context.Background(), dialTimeout) + defer cancel() + conn, err := dial(dialCtx, endpoint) + if err != nil { + return nil, err + } + + m.mu.Lock() + closed := m.closed + if !closed { + m.connections.Store(key, conn) Review Comment: [P1] 当前修复只覆盖“snapshot 已删除、旧 Get 仍到达”的方向,LRU 淘汰后仍会丢失事件排序水位。`notifyEndpointChanges` 在 ClusterManager 锁外执行,并发更新的回调可以乱序;如果 A 的 tombstone 被 1025 个唯一地址淘汰、当前 cluster snapshot 已重新包含 A,而较旧的 removal 回调先于新的 present 回调到达,`endpointEventVers[A]` 已被删除,因此这个旧 removal 会被重新接受并写入 `endpointRemoved[A]=true`。随后 `Get` 的 snapshot 检查虽然返回 true,仍会在 `pinEndpoint` 被拒绝为 `grpc endpoint was removed`,使当前有效实例短暂不可用。 我在当前 Head 的判别探针按 `remove A(v100) -> 1025 次 churn -> snapshot re-add A -> stale remove A(v50) -> Get` 执行;现有 `TestGRPCConnectionManagerRejectsEvictedRemovedEndpointFromSnapshot` 通过,但该序列稳定返回 removed 且 dial 次数为 0,而不是进入拨号。请让淘汰后的事件处理也以当前 snapshot 校正过期 removal,或保留不会随 tombstone 淘汰丢失的排序依据,并补上“re-add 后旧 removal 乱序到达”的回归测试。 -- 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]
