dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/directory/static/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 static
    19  
    20  import (
    21  	perrors "github.com/pkg/errors"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/cluster/directory/base"
    26  	"dubbo.apache.org/dubbo-go/v3/cluster/router/chain"
    27  	"dubbo.apache.org/dubbo-go/v3/common"
    28  	"dubbo.apache.org/dubbo-go/v3/protocol"
    29  )
    30  
    31  type directory struct {
    32  	*base.Directory
    33  	invokers []protocol.Invoker
    34  }
    35  
    36  // NewDirectory Create a new staticDirectory with invokers
    37  func NewDirectory(invokers []protocol.Invoker) *directory {
    38  	var url *common.URL
    39  
    40  	if len(invokers) > 0 {
    41  		url = invokers[0].GetURL()
    42  	}
    43  	dir := &directory{
    44  		Directory: base.NewDirectory(url),
    45  		invokers:  invokers,
    46  	}
    47  
    48  	dir.RouterChain().SetInvokers(invokers)
    49  	return dir
    50  }
    51  
    52  // for-loop invokers ,if all invokers is available ,then it means directory is available
    53  func (dir *directory) IsAvailable() bool {
    54  	if dir.Directory.IsDestroyed() {
    55  		return false
    56  	}
    57  
    58  	if len(dir.invokers) == 0 {
    59  		return false
    60  	}
    61  	for _, invoker := range dir.invokers {
    62  		if !invoker.IsAvailable() {
    63  			return false
    64  		}
    65  	}
    66  	return true
    67  }
    68  
    69  // List List invokers
    70  func (dir *directory) List(invocation protocol.Invocation) []protocol.Invoker {
    71  	l := len(dir.invokers)
    72  	invokers := make([]protocol.Invoker, l)
    73  	copy(invokers, dir.invokers)
    74  	routerChain := dir.RouterChain()
    75  
    76  	if routerChain == nil {
    77  		return invokers
    78  	}
    79  	dirUrl := dir.GetURL()
    80  	return routerChain.Route(dirUrl, invocation)
    81  }
    82  
    83  // Destroy Destroy
    84  func (dir *directory) Destroy() {
    85  	dir.Directory.DoDestroy(func() {
    86  		for _, ivk := range dir.invokers {
    87  			ivk.Destroy()
    88  		}
    89  		dir.invokers = []protocol.Invoker{}
    90  	})
    91  }
    92  
    93  // BuildRouterChain build router chain by invokers
    94  func (dir *directory) BuildRouterChain(invokers []protocol.Invoker) error {
    95  	if len(invokers) == 0 {
    96  		return perrors.Errorf("invokers == null")
    97  	}
    98  	routerChain, e := chain.NewRouterChain()
    99  	if e != nil {
   100  		return e
   101  	}
   102  	routerChain.SetInvokers(dir.invokers)
   103  	dir.SetRouterChain(routerChain)
   104  	return nil
   105  }
   106  
   107  func (dir *directory) Subscribe(url *common.URL) error {
   108  	panic("Static directory does not support subscribing to registry.")
   109  }