Alanxtl commented on code in PR #3812:
URL: https://github.com/apache/dubbo-go/pull/3812#discussion_r4596457168


##########
protocol/triple/serialization.go:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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
+
+import (
+       "fmt"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+       "dubbo.apache.org/dubbo-go/v3/serialization"
+)
+
+func init() {
+       for name, c := range tri.BuiltinSerializationCodecs() {
+               mode, wire := serialization.MessageMode, name
+               if name == "hessian2" || name == "msgpack" {
+                       mode, wire = serialization.WrappedObjectMode, "proto"
+               }
+               
extension.MustRegisterSerialization(serialization.Registration{Protocol: 
"triple", Name: name, Mode: mode, WireName: wire, New: 
func(serialization.Options) (serialization.Codec, error) { return c, nil }})
+       }
+}
+func SerializationOptions(url *common.URL, side string) (client 
[]tri.ClientOption, handler []tri.HandlerOption, isIDL bool, err error) {
+       if raw, ok := url.GetAttribute("triple.serialization." + side); ok {
+               if binding, ok := raw.(*serializationBinding); ok {
+                       binding.mu.Lock()
+                       defer binding.mu.Unlock()
+                       return binding.client, binding.handler, binding.isIDL, 
nil
+               }
+       }
+
+       name := url.GetParam("serialization", "protobuf")
+       reg, ok := extension.LookupSerialization("triple", name)
+       if !ok {
+               return nil, nil, false, fmt.Errorf("unsupported serialization: 
%s", name)
+       }
+       resources := &serialization.Resources{}
+       defer func() {
+               if err != nil {
+                       _ = resources.Close()
+               }
+       }()
+       makeCodec := func(r serialization.Registration) (tri.Codec, error) {
+               parameters := make(map[string]string)
+               for key, values := range url.GetParams() {
+                       if len(values) > 0 {
+                               parameters[key] = values[0]
+                       }
+               }
+               var config any
+               if raw, ok := 
url.GetAttribute(extension.SerializationConfigAttribute); ok {
+                       if configs, ok := raw.(map[string]any); ok {
+                               config = configs[r.ConfigPrefix]
+                       }
+               }
+               codec, buildErr := extension.BuildSerialization(r, 
serialization.Options{Protocol: "triple", Side: side, Parameters: parameters, 
Config: config})
+               if buildErr == nil {
+                       resources.Add(codec)
+               }
+               return codec, buildErr
+       }
+       selected, err := makeCodec(reg)
+       if err != nil {
+               return nil, nil, false, err
+       }
+       names := []string{"proto", "json", "hessian2", reg.Name}
+       accepted, explicit := extension.AcceptedSerializations(url)
+       if explicit {
+               names = accepted
+       }
+
+       inner := make(map[string]tri.Codec)
+       messages := make(map[string]tri.Codec)
+       for _, name := range names {
+               if name == "" {
+                       continue
+               }
+               r, ok := extension.LookupSerialization("triple", name)
+               if !ok {
+                       return nil, nil, false, fmt.Errorf("unsupported 
accepted serialization: %s", name)
+               }
+               c := selected
+               if r.Name != reg.Name {
+                       c, err = makeCodec(r)
+                       if err != nil {
+                               return nil, nil, false, err
+                       }
+               }
+               if r.Mode == serialization.WrappedObjectMode {
+                       inner[r.Name] = c
+               } else {
+                       messages[r.WireName] = c
+               }
+       }
+       if side == "consumer" && explicit && reg.Mode == 
serialization.MessageMode {
+               if _, ok := messages[reg.WireName]; !ok {
+                       return nil, nil, false, fmt.Errorf("response 
serialization %q disabled", reg.Name)
+               }
+       }
+       client, handler = tri.ConfiguredSerializationOptions(selected, inner, 
messages, reg.Mode == serialization.WrappedObjectMode, side == "consumer" && 
explicit)
+       handler = append(handler, tri.WithSerializationOwner(resources))
+       if resources.HasClosers() {
+               client = append(client, 
tri.WithSerializationResources(resources))
+               handler = append(handler, 
tri.WithSerializationResources(resources))
+       }
+       url.SetAttribute("triple.serialization.resources."+side, resources)
+       return client, handler, reg.Mode == serialization.MessageMode, nil
+}
+
+type serializationBinding struct {
+       mu        sync.Mutex
+       users     int
+       retired   bool
+       resources *serialization.Resources
+       source    *common.URL // immutable local construction settings, before 
discovery merges
+       client    []tri.ClientOption
+       handler   []tri.HandlerOption
+       isIDL     bool
+}
+
+func (tp *TripleProtocol) PrepareCodec(url *common.URL, side string) error {
+       if _, legacyService := url.GetAttribute(constant.RpcServiceKey); side 
== "consumer" && legacyService && !usesNewTripleClient(url) {
+               if err := validateLegacySerialization(url); err != nil {
+                       return err
+               }
+       }
+       if raw, ok := url.GetAttribute("triple.serialization." + side); ok {
+               if _, ok := raw.(*serializationBinding); ok {
+                       return nil
+               }
+       }
+       source := url.Clone()
+       c, h, idl, err := SerializationOptions(url, side)
+       if err != nil {
+               return err
+       }
+       resources, _ := url.GetAttribute("triple.serialization.resources." + 
side)
+       owner, _ := resources.(*serialization.Resources)
+       url.SetAttribute("triple.serialization."+side, 
&serializationBinding{client: c, handler: h, isIDL: idl, resources: owner, 
source: source})
+       return nil
+}
+
+// acquireClientSerialization leases the reference-scoped binding to a provider
+// invoker. URL clones share this binding, but no provider owns it exclusively.
+func acquireClientSerialization(url *common.URL) ([]tri.ClientOption, bool, 
*serialization.Resources, func() error, error) {
+       if _, ok := url.GetAttribute("triple.serialization.consumer"); !ok {
+               if err := (&TripleProtocol{}).PrepareCodec(url, "consumer"); 
err != nil {
+                       return nil, false, nil, nil, err
+               }
+       }
+       raw, _ := url.GetAttribute("triple.serialization.consumer")
+       binding := raw.(*serializationBinding)
+       binding.mu.Lock()
+       if binding.retired {
+               // Discovery can reconnect after all previous providers 
disappeared. Build a
+               // fresh generation from the same local configuration, not a 
closed instance.
+               fresh := binding.source.Clone()
+               fresh.DeleteAttribute("triple.serialization.consumer")
+               fresh.DeleteAttribute("triple.serialization.resources.consumer")
+               client, handler, idl, err := SerializationOptions(fresh, 
"consumer")
+               if err != nil {
+                       binding.mu.Unlock()
+                       return nil, false, nil, nil, err
+               }
+               owner, _ := 
fresh.GetAttribute("triple.serialization.resources.consumer")
+               binding.client, binding.handler, binding.isIDL = client, 
handler, idl
+               binding.resources, _ = owner.(*serialization.Resources)
+               binding.retired = false
+       }
+       binding.users++
+       client, idl, resources := binding.client, binding.isIDL, 
binding.resources
+       binding.mu.Unlock()
+       url.SetAttribute("triple.serialization.resources.consumer", resources)
+       var once sync.Once
+       var releaseErr error
+       release := func() error {
+               once.Do(func() {
+                       binding.mu.Lock()
+                       binding.users--
+                       last := binding.users == 0
+                       if last {
+                               binding.retired = true
+                       }
+                       binding.mu.Unlock()
+                       if last {
+                               releaseErr = resources.Close()
+                       }
+               })
+               return releaseErr
+       }
+       return client, idl, resources, release, nil
+}
+
+func (tp *TripleProtocol) ReleaseCodec(url *common.URL, side string) error {
+       if side == "consumer" {
+               if raw, ok := 
url.GetAttribute("triple.serialization.consumer"); ok {
+                       if binding, ok := raw.(*serializationBinding); ok {
+                               binding.mu.Lock()
+                               if binding.users > 0 {
+                                       binding.mu.Unlock()
+                                       return nil
+                               }
+                               binding.retired = true
+                               resources := binding.resources
+                               binding.mu.Unlock()
+                               return resources.Close()
+                       }
+               }
+       }
+       if raw, ok := url.GetAttribute("triple.serialization.resources." + 
side); ok {
+               if resources, ok := raw.(*serialization.Resources); ok {
+                       return resources.Close()
+               }
+       }
+       return nil
+}
+
+// usesNewTripleClient preserves legacy generated stubs while routing public
+// Connection calls to the implementation that consumes serialization bindings.
+func usesNewTripleClient(url *common.URL) bool {
+       if _, ok := url.GetAttribute(constant.ClientInfoKey); ok {
+               return true
+       }
+       if raw, ok := url.GetAttribute(constant.ClientConnectionKey); ok && raw 
== true {
+               return true
+       }
+       return url.GetParam(constant.IDLMode, "") == constant.NONIDL || 
isGenericCall(url.GetParam(constant.GenericKey, ""))
+}
+
+func validateLegacySerialization(url *common.URL) error {

Review Comment:
   这里检查白名单中的名称是否受旧客户端支持,但旧 Dubbo3Invoker 实际解码响应时不读取该策略。实测旧客户端配置 
WithAcceptedSerializations("json"),仍成功接收并解码 Hessian2 响应。应让旧路径执行策略,或在无法执行时拒绝该配置。



##########
protocol/triple/triple_protocol/handler.go:
##########
@@ -299,10 +318,55 @@ func generateBidiStreamHandlerFunc(
 
 func (h *Handler) processImplementation(identifier string, implementation 
StreamingHandlerFunc) {
        h.implementations[identifier] = implementation
+       if h.variants == nil {
+               h.variants = make(map[string]*Handler, 
defaultImplementationsSize)
+       }
+       h.variants[identifier] = h
+}
+
+// newHandlerRouter holds no service implementation or plugin binding itself.
+// Retiring its variants must allow the service's complete context to be freed.
+func newHandlerRouter(next *Handler) *Handler {

Review Comment:
   新 router 没有保留 CORS 策略。浏览器预检不会携带实际的 group/version 值,因此找不到分组 handler,回退到无 CORS 
策略的 router 并返回 405。相同预检在基线返回 204,当前版本返回 405。



##########
protocol/triple/serialization.go:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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
+
+import (
+       "fmt"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+       "dubbo.apache.org/dubbo-go/v3/serialization"
+)
+
+func init() {
+       for name, c := range tri.BuiltinSerializationCodecs() {
+               mode, wire := serialization.MessageMode, name
+               if name == "hessian2" || name == "msgpack" {
+                       mode, wire = serialization.WrappedObjectMode, "proto"
+               }
+               
extension.MustRegisterSerialization(serialization.Registration{Protocol: 
"triple", Name: name, Mode: mode, WireName: wire, New: 
func(serialization.Options) (serialization.Codec, error) { return c, nil }})
+       }
+}
+func SerializationOptions(url *common.URL, side string) (client 
[]tri.ClientOption, handler []tri.HandlerOption, isIDL bool, err error) {
+       if raw, ok := url.GetAttribute("triple.serialization." + side); ok {

Review Comment:
   注册中心重导出会克隆 provider URL;克隆保留同一个 codec binding。旧 exporter 下线时关闭了资源,新 exporter 
随后又从 URL 中取回这个已关闭的 binding。按该顺序复现时,使用 io.Closer 插件的服务重导出后返回 HTTP 
503。重导出时需要创建新的资源代次。



##########
protocol/triple/serialization.go:
##########
@@ -0,0 +1,266 @@
+/*
+ * 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
+
+import (
+       "fmt"
+       "sync"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/common/extension"
+       tri "dubbo.apache.org/dubbo-go/v3/protocol/triple/triple_protocol"
+       "dubbo.apache.org/dubbo-go/v3/serialization"
+)
+
+func init() {
+       for name, c := range tri.BuiltinSerializationCodecs() {
+               mode, wire := serialization.MessageMode, name
+               if name == "hessian2" || name == "msgpack" {
+                       mode, wire = serialization.WrappedObjectMode, "proto"
+               }
+               
extension.MustRegisterSerialization(serialization.Registration{Protocol: 
"triple", Name: name, Mode: mode, WireName: wire, New: 
func(serialization.Options) (serialization.Codec, error) { return c, nil }})
+       }
+}
+func SerializationOptions(url *common.URL, side string) (client 
[]tri.ClientOption, handler []tri.HandlerOption, isIDL bool, err error) {
+       if raw, ok := url.GetAttribute("triple.serialization." + side); ok {
+               if binding, ok := raw.(*serializationBinding); ok {
+                       binding.mu.Lock()
+                       defer binding.mu.Unlock()
+                       return binding.client, binding.handler, binding.isIDL, 
nil
+               }
+       }
+
+       name := url.GetParam("serialization", "protobuf")
+       reg, ok := extension.LookupSerialization("triple", name)
+       if !ok {
+               return nil, nil, false, fmt.Errorf("unsupported serialization: 
%s", name)
+       }
+       resources := &serialization.Resources{}
+       defer func() {
+               if err != nil {
+                       _ = resources.Close()
+               }
+       }()
+       makeCodec := func(r serialization.Registration) (tri.Codec, error) {
+               parameters := make(map[string]string)
+               for key, values := range url.GetParams() {
+                       if len(values) > 0 {
+                               parameters[key] = values[0]
+                       }
+               }
+               var config any
+               if raw, ok := 
url.GetAttribute(extension.SerializationConfigAttribute); ok {
+                       if configs, ok := raw.(map[string]any); ok {
+                               config = configs[r.ConfigPrefix]
+                       }
+               }
+               codec, buildErr := extension.BuildSerialization(r, 
serialization.Options{Protocol: "triple", Side: side, Parameters: parameters, 
Config: config})
+               if buildErr == nil {
+                       resources.Add(codec)
+               }
+               return codec, buildErr
+       }
+       selected, err := makeCodec(reg)
+       if err != nil {
+               return nil, nil, false, err
+       }
+       names := []string{"proto", "json", "hessian2", reg.Name}
+       accepted, explicit := extension.AcceptedSerializations(url)
+       if explicit {
+               names = accepted
+       }
+
+       inner := make(map[string]tri.Codec)
+       messages := make(map[string]tri.Codec)
+       for _, name := range names {
+               if name == "" {
+                       continue
+               }
+               r, ok := extension.LookupSerialization("triple", name)
+               if !ok {
+                       return nil, nil, false, fmt.Errorf("unsupported 
accepted serialization: %s", name)
+               }
+               c := selected
+               if r.Name != reg.Name {
+                       c, err = makeCodec(r)
+                       if err != nil {
+                               return nil, nil, false, err
+                       }
+               }
+               if r.Mode == serialization.WrappedObjectMode {
+                       inner[r.Name] = c
+               } else {
+                       messages[r.WireName] = c
+               }
+       }
+       if side == "consumer" && explicit && reg.Mode == 
serialization.MessageMode {
+               if _, ok := messages[reg.WireName]; !ok {
+                       return nil, nil, false, fmt.Errorf("response 
serialization %q disabled", reg.Name)
+               }
+       }
+       client, handler = tri.ConfiguredSerializationOptions(selected, inner, 
messages, reg.Mode == serialization.WrappedObjectMode, side == "consumer" && 
explicit)
+       handler = append(handler, tri.WithSerializationOwner(resources))

Review Comment:
   只有资源包含 closer 时才跟踪 handler 的活动调用。插件未实现 Close 时,`Unexport` 可以在请求仍处于解码阶段时销毁 
invoker。我用阻塞解码的插件复现后,在途 RPC 返回 cannot find service ... 
invoker-destroyed。服务退役应等待在途 handler,与插件是否需要关闭无关。



##########
protocol/triple/triple_protocol/codec.go:
##########
@@ -641,16 +676,37 @@ func (s *tripleServerCodecSession) Name() string { return 
s.delegate.Name() }
 // hessian2 is always allowed (Non-IDL interop default); any other name must
 // match the provider's configured serialization.
 func (s *tripleServerCodecSession) checkAllowed(codecName string) error {
+       if s.runtimeCodecs != nil {
+               _, err := resolveRuntimeInner(s.runtimeCodecs, codecName)
+               return err
+       }
        if codecName == codecNameHessian2 || codecName == 
s.allowedSerializeType {
                return nil
        }
        return fmt.Errorf("serialize type %q not allowed by provider (allowed: 
%s, %s)",
                codecName, codecNameHessian2, s.allowedSerializeType)
 }
 
+func (s *tripleServerCodecSession) unmarshalEmpty(message any) error {
+       return s.Unmarshal(nil, message)
+}
+
 func (s *tripleServerCodecSession) Unmarshal(data []byte, message any) error {
+       if s.runtimeCodecs != nil && s.delegate.Name() != "proto" {

Review Comment:
   旧客户端发送 `application/grpc+hessian2`,服务端把它当成消息格式检查,而 `messageCodecs` 不包含 
wrapped-object 格式 Hessian2,于是返回 `message serialization "hessian2" 
disabled`。同一份真实 RPC 测试在 PR 基线成功,在当前版本失败。需要识别旧版 wrapped codec 的 wire 格式。



-- 
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]

Reply via email to