github.com/polarismesh/polaris@v1.17.8/plugin/discoverevent.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package plugin 19 20 import ( 21 "os" 22 "sync" 23 24 "github.com/polarismesh/polaris/common/model" 25 ) 26 27 var ( 28 discoverEventOnce sync.Once 29 _discoverChannel DiscoverChannel 30 ) 31 32 // DiscoverChannel is used to receive discover events from the agent 33 type DiscoverChannel interface { 34 Plugin 35 // PublishEvent Release a service event 36 PublishEvent(event model.InstanceEvent) 37 } 38 39 // GetDiscoverEvent Get service discovery event plug -in 40 func GetDiscoverEvent() DiscoverChannel { 41 if _discoverChannel != nil { 42 return _discoverChannel 43 } 44 45 discoverEventOnce.Do(func() { 46 var ( 47 entries []ConfigEntry 48 ) 49 50 if len(config.DiscoverEvent.Entries) != 0 { 51 entries = append(entries, config.DiscoverEvent.Entries...) 52 } else { 53 entries = append(entries, ConfigEntry{ 54 Name: config.DiscoverEvent.Name, 55 Option: config.DiscoverEvent.Option, 56 }) 57 } 58 59 _discoverChannel = newCompositeDiscoverChannel(entries) 60 if err := _discoverChannel.Initialize(nil); err != nil { 61 log.Errorf("DiscoverChannel plugin init err: %s", err.Error()) 62 os.Exit(-1) 63 } 64 }) 65 66 return _discoverChannel 67 } 68 69 // newCompositeDiscoverChannel creates Composite DiscoverChannel 70 func newCompositeDiscoverChannel(options []ConfigEntry) *compositeDiscoverChannel { 71 return &compositeDiscoverChannel{ 72 chain: make([]DiscoverChannel, 0, len(options)), 73 options: options, 74 } 75 } 76 77 // compositeDiscoverChannel is used to receive discover events from the agent 78 type compositeDiscoverChannel struct { 79 chain []DiscoverChannel 80 options []ConfigEntry 81 } 82 83 func (c *compositeDiscoverChannel) Name() string { 84 return "CompositeDiscoverChannel" 85 } 86 87 func (c *compositeDiscoverChannel) Initialize(config *ConfigEntry) error { 88 for i := range c.options { 89 entry := c.options[i] 90 item, exist := pluginSet[entry.Name] 91 if !exist { 92 log.Errorf("plugin DiscoverChannel not found target: %s", entry.Name) 93 continue 94 } 95 96 discoverChannel, ok := item.(DiscoverChannel) 97 if !ok { 98 log.Errorf("plugin target: %s not DiscoverChannel", entry.Name) 99 continue 100 } 101 102 if err := discoverChannel.Initialize(&entry); err != nil { 103 return err 104 } 105 c.chain = append(c.chain, discoverChannel) 106 } 107 return nil 108 } 109 110 func (c *compositeDiscoverChannel) Destroy() error { 111 for i := range c.chain { 112 if err := c.chain[i].Destroy(); err != nil { 113 return err 114 } 115 } 116 return nil 117 } 118 119 // PublishEvent Release a service event 120 func (c *compositeDiscoverChannel) PublishEvent(event model.InstanceEvent) { 121 for i := range c.chain { 122 c.chain[i].PublishEvent(event) 123 } 124 }