dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/protocol.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 "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 // Protocol is the interface that wraps the basic Export, Refer and Destroy method. 33 // 34 // # Export method is to export service for remote invocation 35 // 36 // # Refer method is to refer a remote service 37 // 38 // Destroy method will destroy all invokers and exporters, so it only is called once. 39 type Protocol interface { 40 Export(invoker Invoker) Exporter 41 Refer(url *common.URL) Invoker 42 Destroy() 43 } 44 45 // Exporter is the interface that wraps the basic GetInvoker method and Destroy UnExport. 46 // 47 // GetInvoker method is to get invoker. 48 // 49 // UnExport is to un export an exported service 50 type Exporter interface { 51 GetInvoker() Invoker 52 UnExport() 53 } 54 55 // BaseProtocol is default protocol implement. 56 type BaseProtocol struct { 57 exporterMap *sync.Map 58 invokers []Invoker 59 } 60 61 // NewBaseProtocol creates a new BaseProtocol 62 func NewBaseProtocol() BaseProtocol { 63 return BaseProtocol{ 64 exporterMap: new(sync.Map), 65 } 66 } 67 68 // SetExporterMap set @exporter with @key to local memory. 69 func (bp *BaseProtocol) SetExporterMap(key string, exporter Exporter) { 70 bp.exporterMap.Store(key, exporter) 71 } 72 73 // ExporterMap gets exporter map. 74 func (bp *BaseProtocol) ExporterMap() *sync.Map { 75 return bp.exporterMap 76 } 77 78 // SetInvokers sets invoker into local memory 79 func (bp *BaseProtocol) SetInvokers(invoker Invoker) { 80 bp.invokers = append(bp.invokers, invoker) 81 } 82 83 // Invokers gets all invokers 84 func (bp *BaseProtocol) Invokers() []Invoker { 85 return bp.invokers 86 } 87 88 // Export is default export implement. 89 func (bp *BaseProtocol) Export(invoker Invoker) Exporter { 90 return NewBaseExporter("base", invoker, bp.exporterMap) 91 } 92 93 // Refer is default refer implement. 94 func (bp *BaseProtocol) Refer(url *common.URL) Invoker { 95 return NewBaseInvoker(url) 96 } 97 98 // Destroy will destroy all invoker and exporter, so it only is called once. 99 func (bp *BaseProtocol) Destroy() { 100 // destroy invokers 101 for _, invoker := range bp.invokers { 102 if invoker != nil { 103 invoker.Destroy() 104 } 105 } 106 bp.invokers = []Invoker{} 107 108 // un export exporters 109 bp.exporterMap.Range(func(key, exporter interface{}) bool { 110 if exporter != nil { 111 exporter.(Exporter).UnExport() 112 } else { 113 bp.exporterMap.Delete(key) 114 } 115 return true 116 }) 117 } 118 119 // BaseExporter is default exporter implement. 120 type BaseExporter struct { 121 key string 122 invoker Invoker 123 exporterMap *sync.Map 124 } 125 126 // NewBaseExporter creates a new BaseExporter 127 func NewBaseExporter(key string, invoker Invoker, exporterMap *sync.Map) *BaseExporter { 128 return &BaseExporter{ 129 key: key, 130 invoker: invoker, 131 exporterMap: exporterMap, 132 } 133 } 134 135 // GetInvoker gets invoker 136 func (de *BaseExporter) GetInvoker() Invoker { 137 return de.invoker 138 } 139 140 // UnExport un export service. 141 func (de *BaseExporter) UnExport() { 142 logger.Infof("Exporter unexport.") 143 de.invoker.Destroy() 144 de.exporterMap.Delete(de.key) 145 }