github.com/Jeffail/benthos/v3@v3.65.0/internal/docs/registry.go (about) 1 package docs 2 3 import ( 4 "sync" 5 ) 6 7 // Provider stores the component spec definitions of various component 8 // implementations. 9 type Provider interface { 10 GetDocs(name string, ctype Type) (ComponentSpec, bool) 11 } 12 13 var globalProvider = NewMappedDocsProvider() 14 15 // RegisterDocs stores the documentation spec for a component. 16 func RegisterDocs(spec ComponentSpec) { 17 globalProvider.componentLock.Lock() 18 switch spec.Type { 19 case TypeBuffer: 20 globalProvider.bufferMap[spec.Name] = spec 21 case TypeCache: 22 globalProvider.cacheMap[spec.Name] = spec 23 case TypeInput: 24 globalProvider.inputMap[spec.Name] = spec 25 case TypeMetrics: 26 globalProvider.metricsMap[spec.Name] = spec 27 case TypeOutput: 28 globalProvider.outputMap[spec.Name] = spec 29 case TypeProcessor: 30 globalProvider.processorMap[spec.Name] = spec 31 case TypeRateLimit: 32 globalProvider.rateLimitMap[spec.Name] = spec 33 case TypeTracer: 34 globalProvider.tracerMap[spec.Name] = spec 35 } 36 globalProvider.componentLock.Unlock() 37 } 38 39 // GetDocs attempts to locate a documentation spec for a component identified by 40 // a unique name and type combination. 41 func GetDocs(prov Provider, name string, ctype Type) (ComponentSpec, bool) { 42 refreshOldPlugins() 43 if prov == nil { 44 prov = globalProvider 45 } 46 return prov.GetDocs(name, ctype) 47 } 48 49 //------------------------------------------------------------------------------ 50 51 // MappedDocsProvider stores component documentation in maps, protected by a 52 // mutex, allowing safe concurrent use. 53 type MappedDocsProvider struct { 54 bufferMap map[string]ComponentSpec 55 cacheMap map[string]ComponentSpec 56 inputMap map[string]ComponentSpec 57 metricsMap map[string]ComponentSpec 58 outputMap map[string]ComponentSpec 59 processorMap map[string]ComponentSpec 60 rateLimitMap map[string]ComponentSpec 61 tracerMap map[string]ComponentSpec 62 componentLock sync.Mutex 63 } 64 65 // NewMappedDocsProvider creates a new (empty) provider of component docs. 66 func NewMappedDocsProvider() *MappedDocsProvider { 67 return &MappedDocsProvider{ 68 bufferMap: map[string]ComponentSpec{}, 69 cacheMap: map[string]ComponentSpec{}, 70 inputMap: map[string]ComponentSpec{}, 71 metricsMap: map[string]ComponentSpec{}, 72 outputMap: map[string]ComponentSpec{}, 73 processorMap: map[string]ComponentSpec{}, 74 rateLimitMap: map[string]ComponentSpec{}, 75 tracerMap: map[string]ComponentSpec{}, 76 } 77 } 78 79 // RegisterDocs adds the documentation of a component implementation. 80 func (m *MappedDocsProvider) RegisterDocs(spec ComponentSpec) { 81 m.componentLock.Lock() 82 defer m.componentLock.Unlock() 83 84 switch spec.Type { 85 case TypeBuffer: 86 m.bufferMap[spec.Name] = spec 87 case TypeCache: 88 m.cacheMap[spec.Name] = spec 89 case TypeInput: 90 m.inputMap[spec.Name] = spec 91 case TypeMetrics: 92 m.metricsMap[spec.Name] = spec 93 case TypeOutput: 94 m.outputMap[spec.Name] = spec 95 case TypeProcessor: 96 m.processorMap[spec.Name] = spec 97 case TypeRateLimit: 98 m.rateLimitMap[spec.Name] = spec 99 case TypeTracer: 100 m.tracerMap[spec.Name] = spec 101 } 102 } 103 104 // GetDocs attempts to obtain component implementation docs. 105 func (m *MappedDocsProvider) GetDocs(name string, ctype Type) (ComponentSpec, bool) { 106 m.componentLock.Lock() 107 defer m.componentLock.Unlock() 108 109 var spec ComponentSpec 110 var ok bool 111 112 switch ctype { 113 case TypeBuffer: 114 spec, ok = m.bufferMap[name] 115 case TypeCache: 116 spec, ok = m.cacheMap[name] 117 case TypeInput: 118 spec, ok = m.inputMap[name] 119 case TypeMetrics: 120 spec, ok = m.metricsMap[name] 121 case TypeOutput: 122 spec, ok = m.outputMap[name] 123 case TypeProcessor: 124 spec, ok = m.processorMap[name] 125 case TypeRateLimit: 126 spec, ok = m.rateLimitMap[name] 127 case TypeTracer: 128 spec, ok = m.tracerMap[name] 129 } 130 131 return spec, ok 132 }