Skip to content

Commit

Permalink
Move the output extension registry to the main output module
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Feb 25, 2021
1 parent 626694d commit 5e41f5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
3 changes: 1 addition & 2 deletions cmd/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/loadimpact/k6/lib/consts"
"github.com/loadimpact/k6/loader"
"github.com/loadimpact/k6/output"
"github.com/loadimpact/k6/output/extensions"
"github.com/loadimpact/k6/output/json"
"github.com/loadimpact/k6/stats"
"github.com/loadimpact/k6/stats/cloud"
Expand Down Expand Up @@ -123,7 +122,7 @@ func getAllOutputConstructors() (map[string]func(output.Params) (output.Output,
},
}

exts := extensions.GetAll()
exts := output.GetExtensions()
for k, v := range exts {
if _, ok := result[k]; ok {
return nil, fmt.Errorf("invalid output extension %s, built-in output with the same type already exists", k)
Expand Down
35 changes: 13 additions & 22 deletions output/extensions/registry.go → output/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,38 @@
*
*/

package extensions
package output

import (
"fmt"
"sync"

"github.com/loadimpact/k6/output"
)

//nolint:gochecknoglobals
var (
modules = make(map[string]func(output.Params) (output.Output, error))
mx sync.RWMutex
extensions = make(map[string]func(Params) (Output, error))
mx sync.RWMutex
)

// GetAll returns all registered extensions.
func GetAll() map[string]func(output.Params) (output.Output, error) {
// GetExtensions returns all registered extensions.
func GetExtensions() map[string]func(Params) (Output, error) {
mx.RLock()
defer mx.RUnlock()
res := make(map[string]func(output.Params) (output.Output, error), len(modules))
for k, v := range modules {
res := make(map[string]func(Params) (Output, error), len(extensions))
for k, v := range extensions {
res[k] = v
}
return res
}

// Get returns the output module constructor with the specified name.
func Get(name string) func(output.Params) (output.Output, error) {
mx.RLock()
defer mx.RUnlock()
return modules[name]
}

// Register the given output module constructor. This function panics if a
// module with the same name is already registered.
func Register(name string, mod func(output.Params) (output.Output, error)) {
// RegisterExtension registers the given output extension constructor. This
// function panics if a module with the same name is already registered.
func RegisterExtension(name string, mod func(Params) (Output, error)) {
mx.Lock()
defer mx.Unlock()

if _, ok := modules[name]; ok {
panic(fmt.Sprintf("output module already registered: %s", name))
if _, ok := extensions[name]; ok {
panic(fmt.Sprintf("output extension already registered: %s", name))
}
modules[name] = mod
extensions[name] = mod
}

0 comments on commit 5e41f5e

Please sign in to comment.