dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/directory/base/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 base
    19  
    20  import (
    21  	"sync"
    22  )
    23  
    24  import (
    25  	"go.uber.org/atomic"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/cluster/router"
    30  	"dubbo.apache.org/dubbo-go/v3/cluster/router/chain"
    31  	"dubbo.apache.org/dubbo-go/v3/common"
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  )
    34  
    35  // Directory Abstract implementation of Directory: Invoker list returned from this Directory's list method have been filtered by Routers
    36  type Directory struct {
    37  	url       *common.URL
    38  	destroyed *atomic.Bool
    39  	// this mutex for change the properties in BaseDirectory, like routerChain , destroyed etc
    40  	mutex       sync.Mutex
    41  	routerChain router.Chain
    42  }
    43  
    44  // NewDirectory Create BaseDirectory with URL
    45  func NewDirectory(url *common.URL) *Directory {
    46  	return &Directory{
    47  		url:         url,
    48  		destroyed:   atomic.NewBool(false),
    49  		routerChain: &chain.RouterChain{},
    50  	}
    51  }
    52  
    53  // RouterChain Return router chain in directory
    54  func (dir *Directory) RouterChain() router.Chain {
    55  	return dir.routerChain
    56  }
    57  
    58  // SetRouterChain Set router chain in directory
    59  func (dir *Directory) SetRouterChain(routerChain router.Chain) {
    60  	dir.mutex.Lock()
    61  	defer dir.mutex.Unlock()
    62  	dir.routerChain = routerChain
    63  }
    64  
    65  // GetURL Get URL
    66  func (dir *Directory) GetURL() *common.URL {
    67  	return dir.url
    68  }
    69  
    70  // GetDirectoryUrl Get URL instance
    71  func (dir *Directory) GetDirectoryUrl() *common.URL {
    72  	return dir.url
    73  }
    74  
    75  func (dir *Directory) isProperRouter(url *common.URL) bool {
    76  	app := url.GetParam(constant.ApplicationKey, "")
    77  	dirApp := dir.GetURL().GetParam(constant.ApplicationKey, "")
    78  	if len(dirApp) == 0 && dir.GetURL().SubURL != nil {
    79  		dirApp = dir.GetURL().SubURL.GetParam(constant.ApplicationKey, "")
    80  	}
    81  	serviceKey := dir.GetURL().ServiceKey()
    82  	if len(serviceKey) == 0 {
    83  		serviceKey = dir.GetURL().SubURL.ServiceKey()
    84  	}
    85  	if len(app) > 0 && app == dirApp {
    86  		return true
    87  	}
    88  	if url.ServiceKey() == serviceKey {
    89  		return true
    90  	}
    91  	return false
    92  }
    93  
    94  // DoDestroy stop directory
    95  func (dir *Directory) DoDestroy(doDestroy func()) {
    96  	if dir.destroyed.CAS(false, true) {
    97  		dir.mutex.Lock()
    98  		doDestroy()
    99  		dir.mutex.Unlock()
   100  	}
   101  }
   102  
   103  // IsDestroyed Once directory init finish, it will change to true
   104  func (dir *Directory) IsDestroyed() bool {
   105  	return dir.destroyed.Load()
   106  }