XnLemon commented on code in PR #3687: URL: https://github.com/apache/dubbo-go/pull/3687#discussion_r3818792679
########## common/extension/runtime.go: ########## @@ -0,0 +1,187 @@ +/* + * 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 extension + +import ( + "fmt" + "strings" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/filter" +) + +// Scope identifies an extension lifecycle level. Definition.Scopes uses the +// values as a bit mask; Context.Scope always contains one concrete scope. +type Scope uint8 + +const ( + // InstanceScope is the lifecycle of a dubbo.Instance. + InstanceScope Scope = 1 << iota + // ClientScope is the lifecycle of a client/consumer. + ClientScope + // ServerScope is the lifecycle of a server/provider. + ServerScope +) + +// RoleNone identifies an Instance context, which has no consumer/provider +// role. It is intentionally outside common.RoleType's URL role constants. +const RoleNone common.RoleType = -1 + +// Resource identifies the service or method to which an extension is bound. +// The core constructs Resource values and extensions treat them as read-only. +type Resource struct { + ServiceKey string + Interface string + Method string + Group string + Version string +} + +// Context carries the lifecycle, role, extension configuration, and optional +// RPC resource supplied to an extension callback. A nil Resource means that +// the lifecycle context has not yet been bound to a concrete RPC resource. +type Context struct { + Scope Scope + Role common.RoleType + Config any + Resource *Resource +} + +// Validate verifies that Scope and Role form one of the supported context +// combinations. It is intentionally independent of any concrete extension. +func (c Context) Validate() error { + switch c.Scope { + case InstanceScope: + if c.Role != RoleNone { + return fmt.Errorf("extension: InstanceScope requires RoleNone, got role %d", c.Role) + } + case ClientScope: + if c.Role != common.CONSUMER { + return fmt.Errorf("extension: ClientScope requires consumer role, got role %d", c.Role) + } + case ServerScope: + if c.Role != common.PROVIDER { + return fmt.Errorf("extension: ServerScope requires provider role, got role %d", c.Role) + } + default: + return fmt.Errorf("extension: invalid context scope %d", c.Scope) + } + return nil +} Review Comment: 当前 Definition.Scopes 是 extension 的能力声明 本身就是 bitmask 多个scope也可以 但是这里初始化是 Context.Scope 只能选一个scope 就比如说server建serverscope client建clientscope 使用时多scope 同一个 extension 会被分别初始化两次,拥有独立的 Context、Config 和运行时状态 因此这里不能把 ClientScope | ServerScope 作为一个运行时 Context 传给 Init,拒绝多个scope是有意的 -- 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]
