Copilot commented on code in PR #3550: URL: https://github.com/apache/dubbo-go/pull/3550#discussion_r3659329481
########## protocol/triple/triple_protocol/inner_codec.go: ########## @@ -0,0 +1,81 @@ +/* + * 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 triple_protocol + +import ( + "fmt" +) + +// innerCodecRegistry is the allowed-set of Triple non-IDL inner serialization +// codecs. The name key MUST match the SerializeType string the client writes +// into TripleRequestWrapper.SerializeType / TripleResponseWrapper.SerializeType +// (e.g. "hessian2", "msgpack"). An absent entry is a disabled serialization. +type innerCodecRegistry struct { + items map[string]Codec +} + +var innerCodecs = &innerCodecRegistry{items: make(map[string]Codec)} + +// SetInnerCodec registers an inner codec under the given name. +// +// SetInnerCodec is init-only: it MUST be called exclusively from package init(). +func SetInnerCodec(name string, c Codec) { + innerCodecs.items[name] = c +} + +// GetInnerCodec looks up a registered inner codec by name. +// Returns (nil, false) when the name is unknown or disabled (unregistered). +func GetInnerCodec(name string) (Codec, bool) { + c, ok := innerCodecs.items[name] + return c, ok +} + +// innerCodecNames returns the registered inner codec names, for error +// diagnostics. Order is unspecified. +func innerCodecNames() []string { + names := make([]string, 0, len(innerCodecs.items)) + for n := range innerCodecs.items { + names = append(names, n) + } + return names +} Review Comment: The error message prints `innerCodecNames()` in unspecified (map iteration) order, which makes log output and tests/debugging nondeterministic. Sorting the returned names (or formatting them deterministically) would provide stable diagnostics. ########## protocol/triple/triple_protocol/inner_codec.go: ########## @@ -0,0 +1,81 @@ +/* + * 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 triple_protocol + +import ( + "fmt" +) + +// innerCodecRegistry is the allowed-set of Triple non-IDL inner serialization +// codecs. The name key MUST match the SerializeType string the client writes +// into TripleRequestWrapper.SerializeType / TripleResponseWrapper.SerializeType +// (e.g. "hessian2", "msgpack"). An absent entry is a disabled serialization. +type innerCodecRegistry struct { + items map[string]Codec +} + +var innerCodecs = &innerCodecRegistry{items: make(map[string]Codec)} + +// SetInnerCodec registers an inner codec under the given name. +// +// SetInnerCodec is init-only: it MUST be called exclusively from package init(). +func SetInnerCodec(name string, c Codec) { + innerCodecs.items[name] = c +} Review Comment: `SetInnerCodec` doesn’t validate that `c` is non-nil and that `c.Name()` matches the registry key. That can lead to hard-to-trace failures later (e.g., `resolveInnerCodec` returns a codec whose `Name()` differs from the request `SerializeType`, and `tripleServerCodecSession.Marshal` writes `SerializeType: inner.Name()` back on the wire). Consider rejecting nil codecs and enforcing `c.Name() == name` (or explicitly documenting/encoding the key rather than `inner.Name()`). ########## protocol/triple/triple_protocol/inner_codec.go: ########## @@ -0,0 +1,81 @@ +/* + * 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 triple_protocol + +import ( + "fmt" +) + +// innerCodecRegistry is the allowed-set of Triple non-IDL inner serialization +// codecs. The name key MUST match the SerializeType string the client writes +// into TripleRequestWrapper.SerializeType / TripleResponseWrapper.SerializeType +// (e.g. "hessian2", "msgpack"). An absent entry is a disabled serialization. +type innerCodecRegistry struct { + items map[string]Codec +} + +var innerCodecs = &innerCodecRegistry{items: make(map[string]Codec)} + +// SetInnerCodec registers an inner codec under the given name. +// +// SetInnerCodec is init-only: it MUST be called exclusively from package init(). +func SetInnerCodec(name string, c Codec) { + innerCodecs.items[name] = c +} + +// GetInnerCodec looks up a registered inner codec by name. +// Returns (nil, false) when the name is unknown or disabled (unregistered). +func GetInnerCodec(name string) (Codec, bool) { + c, ok := innerCodecs.items[name] + return c, ok +} Review Comment: The global `innerCodecs.items` map is accessed without synchronization. Although the comment says `SetInnerCodec` is init-only, it’s exported and could be called at runtime (or from other packages), which would race with concurrent `GetInnerCodec`/`innerCodecNames` reads. Consider adding a `sync.RWMutex` guarding all accesses, or make registration unexported/locked after init (e.g., panic on late registration) to guarantee race-free reads in production. ########## protocol/triple/triple_protocol/inner_codec.go: ########## @@ -0,0 +1,81 @@ +/* + * 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 triple_protocol + +import ( + "fmt" +) + +// innerCodecRegistry is the allowed-set of Triple non-IDL inner serialization +// codecs. The name key MUST match the SerializeType string the client writes +// into TripleRequestWrapper.SerializeType / TripleResponseWrapper.SerializeType +// (e.g. "hessian2", "msgpack"). An absent entry is a disabled serialization. +type innerCodecRegistry struct { + items map[string]Codec +} + +var innerCodecs = &innerCodecRegistry{items: make(map[string]Codec)} + +// SetInnerCodec registers an inner codec under the given name. +// +// SetInnerCodec is init-only: it MUST be called exclusively from package init(). +func SetInnerCodec(name string, c Codec) { + innerCodecs.items[name] = c +} + +// GetInnerCodec looks up a registered inner codec by name. +// Returns (nil, false) when the name is unknown or disabled (unregistered). +func GetInnerCodec(name string) (Codec, bool) { + c, ok := innerCodecs.items[name] + return c, ok +} + +// innerCodecNames returns the registered inner codec names, for error +// diagnostics. Order is unspecified. +func innerCodecNames() []string { + names := make([]string, 0, len(innerCodecs.items)) + for n := range innerCodecs.items { + names = append(names, n) + } + return names +} + +// resolveInnerCodec looks up the inner codec registered under serializeType +// in the inner codec registry. An empty serializeType defaults to hessian2 for +// backward compatibility. +// +// Dubbo Java writes "hessian4" into the wrapper (TripleConstants.HESSIAN4) +// while its on-wire encoding is Hessian2-compatible; the Java receiver maps it +// back to "hessian2" in ReflectionPackableMethod.convertHessianFromWrapper. Go +// mirrors that single alias so a Java non-IDL client is not rejected. +func resolveInnerCodec(serializeType string) (Codec, error) { + if serializeType == "" || serializeType == "hessian4" { + serializeType = codecNameHessian2 + } + c, ok := GetInnerCodec(serializeType) + if !ok { + return nil, fmt.Errorf("unsupported or disabled serialize type %q (registered: %v)", + serializeType, innerCodecNames()) + } Review Comment: The error message prints `innerCodecNames()` in unspecified (map iteration) order, which makes log output and tests/debugging nondeterministic. Sorting the returned names (or formatting them deterministically) would provide stable diagnostics. -- 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]
