dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/invoker.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 protocol
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  )
    24  
    25  import (
    26  	"github.com/dubbogo/gost/log/logger"
    27  
    28  	perrors "github.com/pkg/errors"
    29  
    30  	uatomic "go.uber.org/atomic"
    31  )
    32  
    33  import (
    34  	"dubbo.apache.org/dubbo-go/v3/common"
    35  )
    36  
    37  var (
    38  	ErrClientClosed     = perrors.New("remoting client has closed")
    39  	ErrNoReply          = perrors.New("request need @response")
    40  	ErrDestroyedInvoker = perrors.New("request Destroyed invoker")
    41  )
    42  
    43  // Invoker the service invocation interface for the consumer
    44  // Extension - Invoker
    45  //
    46  //go:generate mockgen -source invoker.go -destination mock/mock_invoker.go -self_package dubbo.apache.org/dubbo-go/v3/protocol/mock --package mock Invoker
    47  type Invoker interface {
    48  	common.Node
    49  	// Invoke the invocation and return result.
    50  	Invoke(context.Context, Invocation) Result
    51  }
    52  
    53  // BaseInvoker provides default invoker implements Invoker
    54  type BaseInvoker struct {
    55  	url       *common.URL
    56  	available uatomic.Bool
    57  	destroyed uatomic.Bool
    58  }
    59  
    60  // NewBaseInvoker creates a new BaseInvoker
    61  func NewBaseInvoker(url *common.URL) *BaseInvoker {
    62  	ivk := &BaseInvoker{
    63  		url: url,
    64  	}
    65  	ivk.available.Store(true)
    66  	ivk.destroyed.Store(false)
    67  
    68  	return ivk
    69  }
    70  
    71  // GetURL gets base invoker URL
    72  func (bi *BaseInvoker) GetURL() *common.URL {
    73  	return bi.url
    74  }
    75  
    76  // IsAvailable gets available flag
    77  func (bi *BaseInvoker) IsAvailable() bool {
    78  	return bi.available.Load()
    79  }
    80  
    81  // IsDestroyed gets destroyed flag
    82  func (bi *BaseInvoker) IsDestroyed() bool {
    83  	return bi.destroyed.Load()
    84  }
    85  
    86  // Invoke provides default invoker implement
    87  func (bi *BaseInvoker) Invoke(context context.Context, invocation Invocation) Result {
    88  	return &RPCResult{}
    89  }
    90  
    91  // Destroy changes available and destroyed flag
    92  func (bi *BaseInvoker) Destroy() {
    93  	logger.Infof("Destroy invoker: %s", bi.GetURL())
    94  	bi.destroyed.Store(true)
    95  	bi.available.Store(false)
    96  }
    97  
    98  func (bi *BaseInvoker) String() string {
    99  	if bi.url != nil {
   100  		return fmt.Sprintf("invoker{protocol: %s, host: %s:%s, path: %s}",
   101  			bi.url.Protocol, bi.url.Ip, bi.url.Port, bi.url.Path)
   102  	}
   103  	return fmt.Sprintf("%#v", bi)
   104  }