dubbo.apache.org/dubbo-go/v3@v3.1.1/config/service.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 config
    19  
    20  import (
    21  	"sync"
    22  )
    23  
    24  import (
    25  	"github.com/dubbogo/gost/log/logger"
    26  )
    27  
    28  import (
    29  	"dubbo.apache.org/dubbo-go/v3/common"
    30  )
    31  
    32  var (
    33  	conServicesLock              = sync.Mutex{}                   // used to guard conServices map.
    34  	conServices                  = map[string]common.RPCService{} // service name -> service
    35  	proServicesLock              = sync.Mutex{}                   // used to guard proServices map
    36  	proServices                  = map[string]common.RPCService{} // service name -> service
    37  	interfaceNameConServicesLock = sync.Mutex{}                   // used to guard interfaceNameConServices map
    38  	interfaceNameConServices     = map[string]common.RPCService{} // interfaceName -> service
    39  )
    40  
    41  // SetConsumerService is called by init() of implement of RPCService
    42  func SetConsumerService(service common.RPCService) {
    43  	ref := common.GetReference(service)
    44  	conServicesLock.Lock()
    45  	defer func() {
    46  		conServicesLock.Unlock()
    47  		logger.Debugf("A consumer service %s was registered successfully.", ref)
    48  	}()
    49  	conServices[ref] = service
    50  }
    51  
    52  // SetProviderService is called by init() of implement of RPCService
    53  func SetProviderService(service common.RPCService) {
    54  	ref := common.GetReference(service)
    55  	proServicesLock.Lock()
    56  	defer func() {
    57  		proServicesLock.Unlock()
    58  		logger.Debugf("A provider service %s was registered successfully.", ref)
    59  	}()
    60  	proServices[ref] = service
    61  }
    62  
    63  // GetConsumerService gets ConsumerService by @name
    64  func GetConsumerService(name string) common.RPCService {
    65  	conServicesLock.Lock()
    66  	defer conServicesLock.Unlock()
    67  	return conServices[name]
    68  }
    69  
    70  // GetProviderService gets ProviderService by @name
    71  func GetProviderService(name string) common.RPCService {
    72  	proServicesLock.Lock()
    73  	defer proServicesLock.Unlock()
    74  	return proServices[name]
    75  }
    76  
    77  // GetProviderServiceMap gets ProviderServiceMap
    78  func GetProviderServiceMap() map[string]common.RPCService {
    79  	return proServices
    80  }
    81  
    82  // GetConsumerServiceMap gets ProviderServiceMap
    83  func GetConsumerServiceMap() map[string]common.RPCService {
    84  	return conServices
    85  }
    86  
    87  // SetConsumerServiceByInterfaceName is used by pb serialization
    88  func SetConsumerServiceByInterfaceName(interfaceName string, srv common.RPCService) {
    89  	interfaceNameConServicesLock.Lock()
    90  	defer interfaceNameConServicesLock.Unlock()
    91  	interfaceNameConServices[interfaceName] = srv
    92  }
    93  
    94  // GetConsumerServiceByInterfaceName is used by pb serialization
    95  func GetConsumerServiceByInterfaceName(interfaceName string) common.RPCService {
    96  	interfaceNameConServicesLock.Lock()
    97  	defer interfaceNameConServicesLock.Unlock()
    98  	return interfaceNameConServices[interfaceName]
    99  }
   100  
   101  // GetCallback gets CallbackResponse by @name
   102  func GetCallback(name string) func(response common.CallbackResponse) {
   103  	service := GetConsumerService(name)
   104  	if sv, ok := service.(common.AsyncCallbackService); ok {
   105  		return sv.CallBack
   106  	}
   107  	return nil
   108  }