dubbo.apache.org/dubbo-go/v3@v3.1.1/common/extension/registry_directory.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package extension
    19  
    20  import (
    21  	"github.com/dubbogo/gost/log/logger"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/cluster/directory"
    26  	"dubbo.apache.org/dubbo-go/v3/common"
    27  	"dubbo.apache.org/dubbo-go/v3/registry"
    28  )
    29  
    30  type registryDirectory func(url *common.URL, registry registry.Registry) (directory.Directory, error)
    31  
    32  var directories = make(map[string]registryDirectory)
    33  var defaultDirectory registryDirectory
    34  
    35  // SetDefaultRegistryDirectory sets the default registryDirectory
    36  func SetDefaultRegistryDirectory(v registryDirectory) {
    37  	defaultDirectory = v
    38  }
    39  
    40  // SetDirectory sets the default registryDirectory
    41  func SetDirectory(key string, v registryDirectory) {
    42  	directories[key] = v
    43  }
    44  
    45  // GetDefaultRegistryDirectory finds the registryDirectory with url and registry
    46  func GetDefaultRegistryDirectory(config *common.URL, registry registry.Registry) (directory.Directory, error) {
    47  	if defaultDirectory == nil {
    48  		panic("registry directory is not existing, make sure you have import the package.")
    49  	}
    50  	return defaultDirectory(config, registry)
    51  }
    52  
    53  // GetDirectoryInstance finds the registryDirectory with url and registry
    54  func GetDirectoryInstance(config *common.URL, registry registry.Registry) (directory.Directory, error) {
    55  	key := config.Protocol
    56  	if key == "" {
    57  		return GetDefaultRegistryDirectory(config, registry)
    58  	}
    59  	if directories[key] == nil {
    60  		logger.Warn("registry directory " + key + " does not exist, make sure you have import the package, will use the default directory type.")
    61  		return GetDefaultRegistryDirectory(config, registry)
    62  	}
    63  	return directories[key](config, registry)
    64  }