Skip to content

Commit

Permalink
optimize metadata init from registry
Browse files Browse the repository at this point in the history
  • Loading branch information
FoghostCn committed Dec 13, 2024
1 parent 688086c commit 8c93d9d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 135 deletions.
47 changes: 1 addition & 46 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ package client

import (
"context"
"strconv"
"time"
)

import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/metadata"
"dubbo.apache.org/dubbo-go/v3/protocol"
invocation_impl "dubbo.apache.org/dubbo-go/v3/protocol/invocation"
Expand Down Expand Up @@ -122,7 +119,7 @@ func (cli *Client) DialWithDefinition(interfaceName string, definition *ClientDe
}

func (cli *Client) dial(interfaceName string, info *ClientInfo, opts ...ReferenceOption) (*Connection, error) {
if err := cli.initRegistryMetadataReport(); err != nil {
if err := metadata.InitRegistryMetadataReport(cli.cliOpts.Registries); err != nil {
return nil, err
}
newRefOpts := defaultReferenceOptions()
Expand All @@ -145,48 +142,6 @@ func (cli *Client) dial(interfaceName string, info *ClientInfo, opts ...Referenc
return &Connection{refOpts: newRefOpts}, nil
}

func (cli *Client) initRegistryMetadataReport() error {
if len(cli.cliOpts.Registries) > 0 {
for id, reg := range cli.cliOpts.Registries {
ok, err := strconv.ParseBool(reg.UseAsMetaReport)
if err != nil {
return err
}
if ok {
opts, err := registryToReportOptions(id, reg)
if err != nil {
return err
}
if err := opts.Init(); err != nil {
return err
}
}
}
}
return nil
}

func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) {
opts := metadata.NewReportOptions(
metadata.WithRegistryId(id),
metadata.WithProtocol(rc.Protocol),
metadata.WithAddress(rc.Address),
metadata.WithUsername(rc.Username),
metadata.WithPassword(rc.Password),
metadata.WithGroup(rc.Group),
metadata.WithNamespace(rc.Namespace),
metadata.WithParams(rc.Params),
)
if rc.Timeout != "" {
timeout, err := time.ParseDuration(rc.Timeout)
if err != nil {
return nil, err
}
metadata.WithTimeout(timeout)(opts)
}
return opts, nil
}

func generateInvocation(methodName string, reqs []interface{}, resp interface{}, callType string, opts *CallOptions) (protocol.Invocation, error) {
var paramsRawVals []interface{}
for _, req := range reqs {
Expand Down
9 changes: 9 additions & 0 deletions global/registry_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package global

import "strconv"

// todo(DMwangnima): finish refactoring and move related logic from config package to this file.
// This RegistryConfig is a copy of /config/RegistryConfig right now.
// Please refer to issue(https://github.com/apache/dubbo-go/issues/2377).
Expand Down Expand Up @@ -75,3 +77,10 @@ func (c *RegistryConfig) Clone() *RegistryConfig {
UseAsConfigCenter: c.UseAsConfigCenter,
}
}

func (c *RegistryConfig) UseAsMetadataReport() (bool, error) {
if c.UseAsMetaReport == "" {
return true, nil
}
return strconv.ParseBool(c.UseAsMetaReport)
}
40 changes: 40 additions & 0 deletions metadata/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,46 @@ type ReportOptions struct {
*global.MetadataReportConfig
}

func InitRegistryMetadataReport(registries map[string]*global.RegistryConfig) error {
if len(registries) > 0 {
for id, reg := range registries {
ok, err := reg.UseAsMetadataReport()
if err != nil {
return err
}
if ok {
opts := fromRegistry(id, reg)
if err := opts.Init(); err != nil {
return err
}
}
}
}
return nil
}

func fromRegistry(id string, rc *global.RegistryConfig) *ReportOptions {
opts := NewReportOptions(
WithRegistryId(id),
WithProtocol(rc.Protocol),
WithAddress(rc.Address),
WithUsername(rc.Username),
WithPassword(rc.Password),
WithGroup(rc.Group),
WithNamespace(rc.Namespace),
WithParams(rc.Params),
)
if rc.Timeout != "" {
timeout, err := time.ParseDuration(rc.Timeout)
if err != nil {
logger.Errorf("parse registry timeout config error %v", rc.Timeout)
} else {
WithTimeout(timeout)(opts)
}
}
return opts
}

func (opts *ReportOptions) Init() error {
url, err := opts.toUrl()
if err != nil {
Expand Down
44 changes: 1 addition & 43 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error {
if err := rc.initMetadataReport(); err != nil {
return err
}
if err := rc.initRegistryMetadataReport(); err != nil {
if err := metadata.InitRegistryMetadataReport(rc.Registries); err != nil {
return err
}

Expand Down Expand Up @@ -201,48 +201,6 @@ func reportConfigToReportOptions(mc *global.MetadataReportConfig) (*metadata.Rep
return opts, nil
}

func (rc *InstanceOptions) initRegistryMetadataReport() error {
if len(rc.Registries) > 0 {
for id, reg := range rc.Registries {
ok, err := strconv.ParseBool(reg.UseAsMetaReport)
if err != nil {
return err
}
if ok {
opts, err := registryToReportOptions(id, reg)
if err != nil {
return err
}
if err := opts.Init(); err != nil {
return err
}
}
}
}
return nil
}

func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) {
opts := metadata.NewReportOptions(
metadata.WithRegistryId(id),
metadata.WithProtocol(rc.Protocol),
metadata.WithAddress(rc.Address),
metadata.WithUsername(rc.Username),
metadata.WithPassword(rc.Password),
metadata.WithGroup(rc.Group),
metadata.WithNamespace(rc.Namespace),
metadata.WithParams(rc.Params),
)
if rc.Timeout != "" {
timeout, err := time.ParseDuration(rc.Timeout)
if err != nil {
return nil, err
}
metadata.WithTimeout(timeout)(opts)
}
return opts, nil
}

func (rc *InstanceOptions) Prefix() string {
return constant.Dubbo
}
Expand Down
48 changes: 2 additions & 46 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sort"
"strconv"
"sync"
"time"
)

import (
Expand All @@ -34,8 +33,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
dubboutil "dubbo.apache.org/dubbo-go/v3/common/dubboutil"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/common/dubboutil"
"dubbo.apache.org/dubbo-go/v3/metadata"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/registry/exposed_tmp"
Expand Down Expand Up @@ -149,7 +147,7 @@ func (s *Server) exportServices() (err error) {
func (s *Server) Serve() error {
// the registryConfig in ServiceOptions and ServerOptions all need to init a metadataReporter,
// when ServiceOptions.init() is called we don't know if a new registry config is set in the future use serviceOption
if err := s.initRegistryMetadataReport(); err != nil {
if err := metadata.InitRegistryMetadataReport(s.cfg.Registries); err != nil {
return err
}
opts := metadata.NewOptions(
Expand Down Expand Up @@ -266,48 +264,6 @@ func getMetadataPort(opts *ServerOptions) int {
return p
}

func (s *Server) initRegistryMetadataReport() error {
if len(s.cfg.Registries) > 0 {
for id, reg := range s.cfg.Registries {
ok, err := strconv.ParseBool(reg.UseAsMetaReport)
if err != nil {
return err
}
if ok {
opts, err := registryToReportOptions(id, reg)
if err != nil {
return err
}
if err := opts.Init(); err != nil {
return err
}
}
}
}
return nil
}

func registryToReportOptions(id string, rc *global.RegistryConfig) (*metadata.ReportOptions, error) {
opts := metadata.NewReportOptions(
metadata.WithRegistryId(id),
metadata.WithProtocol(rc.Protocol),
metadata.WithAddress(rc.Address),
metadata.WithUsername(rc.Username),
metadata.WithPassword(rc.Password),
metadata.WithGroup(rc.Group),
metadata.WithNamespace(rc.Namespace),
metadata.WithParams(rc.Params),
)
if rc.Timeout != "" {
timeout, err := time.ParseDuration(rc.Timeout)
if err != nil {
return nil, err
}
metadata.WithTimeout(timeout)(opts)
}
return opts, nil
}

func NewServer(opts ...ServerOption) (*Server, error) {
newSrvOpts := defaultServerOptions()
if err := newSrvOpts.init(opts...); err != nil {
Expand Down

0 comments on commit 8c93d9d

Please sign in to comment.