Skip to content
Snippets Groups Projects
base_directory.go 3.93 KiB
Newer Older
AlexStocks's avatar
AlexStocks committed
/*
 * 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.
 */
fangyincheng's avatar
fangyincheng committed

vito.he's avatar
vito.he committed
package directory

邹毅贤's avatar
邹毅贤 committed
import (
	"sync"
)
邹毅贤's avatar
邹毅贤 committed

vito.he's avatar
vito.he committed
	"go.uber.org/atomic"
vito.he's avatar
vito.he committed
)
邹毅贤's avatar
邹毅贤 committed

vito.he's avatar
vito.he committed
import (
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/cluster/router"
	"github.com/apache/dubbo-go/cluster/router/chain"
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/common/constant"
	"github.com/apache/dubbo-go/common/extension"
	"github.com/apache/dubbo-go/common/logger"
vito.he's avatar
vito.he committed
)
邹毅贤's avatar
邹毅贤 committed
// BaseDirectory Abstract implementation of Directory: Invoker list returned from this Directory's list method have been filtered by Routers
vito.he's avatar
vito.he committed
type BaseDirectory struct {
邹毅贤's avatar
邹毅贤 committed
	url       *common.URL
	destroyed *atomic.Bool
	// this mutex for change the properties in BaseDirectory, like routerChain , destroyed etc
邹毅贤's avatar
邹毅贤 committed
	mutex       sync.Mutex
	routerChain router.Chain
}

邹毅贤's avatar
邹毅贤 committed
// NewBaseDirectory Create BaseDirectory with URL
func NewBaseDirectory(url *common.URL) BaseDirectory {
	return BaseDirectory{
		url:         url,
		destroyed:   atomic.NewBool(false),
		routerChain: &chain.RouterChain{},
	}
}

邹毅贤's avatar
邹毅贤 committed
// RouterChain Return router chain in directory
邹毅贤's avatar
邹毅贤 committed
func (dir *BaseDirectory) RouterChain() router.Chain {
	return dir.routerChain
邹毅贤's avatar
邹毅贤 committed
// SetRouterChain Set router chain in directory
邹毅贤's avatar
邹毅贤 committed
func (dir *BaseDirectory) SetRouterChain(routerChain router.Chain) {
邹毅贤's avatar
邹毅贤 committed
	dir.mutex.Lock()
	defer dir.mutex.Unlock()
邹毅贤's avatar
邹毅贤 committed
	dir.routerChain = routerChain
}

// GetUrl Get URL
haohongfan's avatar
haohongfan committed
func (dir *BaseDirectory) GetUrl() *common.URL {
	return dir.url
vito.he's avatar
vito.he committed
}
// GetDirectoryUrl Get URL instance
vito.he's avatar
vito.he committed
func (dir *BaseDirectory) GetDirectoryUrl() *common.URL {
	return dir.url
}
邹毅贤's avatar
邹毅贤 committed
// SetRouters Convert url to routers and add them into dir.routerChain
邹毅贤's avatar
邹毅贤 committed
func (dir *BaseDirectory) SetRouters(urls []*common.URL) {
	if len(urls) == 0 {
		return
	}

邹毅贤's avatar
邹毅贤 committed
	routers := make([]router.PriorityRouter, 0, len(urls))
邹毅贤's avatar
邹毅贤 committed

	for _, url := range urls {
		routerKey := url.GetParam(constant.ROUTER_KEY, "")

		if len(routerKey) == 0 {
			continue
		}
		if url.Protocol == constant.CONDITION_ROUTE_PROTOCOL {
			if !dir.isProperRouter(url) {
				continue
邹毅贤's avatar
邹毅贤 committed
			}
aliiohs's avatar
aliiohs committed
		}
		factory := extension.GetRouterFactory(url.Protocol)
		r, err := factory.NewPriorityRouter(url)
		if err != nil {
			logger.Errorf("Create router fail. router key: %s, url:%s, error: %+v", routerKey, url.Service(), err)
			return
		}
		routers = append(routers, r)
aliiohs's avatar
aliiohs committed
	}
aliiohs's avatar
aliiohs committed

邹毅贤's avatar
邹毅贤 committed
	logger.Infof("Init file condition router success, size: %v", len(routers))
邹毅贤's avatar
邹毅贤 committed
	dir.mutex.Lock()
	rc := dir.routerChain
	dir.mutex.Unlock()
邹毅贤's avatar
邹毅贤 committed

邹毅贤's avatar
邹毅贤 committed
	rc.AddRouters(routers)
aliiohs's avatar
aliiohs committed
}
func (dir *BaseDirectory) isProperRouter(url *common.URL) bool {
	app := url.GetParam(constant.APPLICATION_KEY, "")
	dirApp := dir.GetUrl().GetParam(constant.APPLICATION_KEY, "")
	if len(dirApp) == 0 && dir.GetUrl().SubURL != nil {
		dirApp = dir.GetUrl().SubURL.GetParam(constant.APPLICATION_KEY, "")
	}
	serviceKey := dir.GetUrl().ServiceKey()
	if len(serviceKey) == 0 {
		serviceKey = dir.GetUrl().SubURL.ServiceKey()
	}
	if len(app) > 0 && app == dirApp {
	if url.ServiceKey() == serviceKey {
		return true
// Destroy Destroy
func (dir *BaseDirectory) Destroy(doDestroy func()) {
vito.he's avatar
vito.he committed
	if dir.destroyed.CAS(false, true) {
		dir.mutex.Lock()
		doDestroy()
		dir.mutex.Unlock()
邹毅贤's avatar
邹毅贤 committed
// IsAvailable Once directory init finish, it will change to true
func (dir *BaseDirectory) IsAvailable() bool {
vito.he's avatar
vito.he committed
	return !dir.destroyed.Load()