github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/corev1alpha1/pipeline/actor_factory.go (about)

     1  package pipeline
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	core "github.com/awesome-flow/flow/pkg/corev1alpha1"
     8  	"github.com/awesome-flow/flow/pkg/corev1alpha1/actor"
     9  	"github.com/awesome-flow/flow/pkg/types"
    10  	flowplugin "github.com/awesome-flow/flow/pkg/util/plugin"
    11  )
    12  
    13  var CoreBuilders map[string]core.Builder = map[string]core.Builder{
    14  	"core.receiver":   actor.ReceiverFactory,
    15  	"core.buffer":     actor.NewBuffer,
    16  	"core.compressor": actor.NewCompressor,
    17  	"core.mux":        actor.NewMux,
    18  	"core.replicator": actor.NewReplicator,
    19  	"core.router":     actor.NewRouter,
    20  	"core.throttler":  actor.NewThrottler,
    21  	"core.sink":       actor.NewSink,
    22  }
    23  
    24  type ActorFactory interface {
    25  	Build(name string, ctx *core.Context, cfg *types.CfgBlockActor) (core.Actor, error)
    26  }
    27  
    28  type CoreActorFactory struct {
    29  	builders map[string]core.Builder
    30  }
    31  
    32  var _ ActorFactory = (*CoreActorFactory)(nil)
    33  
    34  func NewCoreActorFactory() *CoreActorFactory {
    35  	return NewCoreActorFactoryWithBuilders(CoreBuilders)
    36  }
    37  
    38  func NewCoreActorFactoryWithBuilders(builders map[string]core.Builder) *CoreActorFactory {
    39  	return &CoreActorFactory{
    40  		builders: builders,
    41  	}
    42  }
    43  
    44  func (f *CoreActorFactory) Build(name string, ctx *core.Context, cfg *types.CfgBlockActor) (core.Actor, error) {
    45  	module := cfg.Module
    46  	if _, ok := f.builders[module]; !ok {
    47  		return nil, fmt.Errorf("unrecognised core module %[2]s for actor %[1]s", name, module)
    48  	}
    49  
    50  	return (f.builders[module])(name, ctx, core.Params(cfg.Params))
    51  }
    52  
    53  type PluginActorFactory struct {
    54  	loader flowplugin.Loader
    55  }
    56  
    57  var _ ActorFactory = (*PluginActorFactory)(nil)
    58  
    59  func NewPluginActorFactory() *PluginActorFactory {
    60  	return NewPluginActorFactoryWithLoader(flowplugin.GoPluginLoader)
    61  }
    62  
    63  func NewPluginActorFactoryWithLoader(loader flowplugin.Loader) *PluginActorFactory {
    64  	return &PluginActorFactory{
    65  		loader: loader,
    66  	}
    67  }
    68  
    69  func (f *PluginActorFactory) Build(name string, ctx *core.Context, cfg *types.CfgBlockActor) (core.Actor, error) {
    70  	module := cfg.Module
    71  	module = strings.Replace(module, "plugin.", "", 1)
    72  	path, ok := ctx.Config().Get(types.NewKey("plugin.path"))
    73  	if !ok {
    74  		return nil, fmt.Errorf("failed to fetch config for `plugin.path`")
    75  	}
    76  	plugin, err := f.loader(path.(string), module)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	builder, err := plugin.Lookup(cfg.Builder)
    81  	if err != nil {
    82  		return nil, fmt.Errorf("lookup for method `%s` failed for plugin %s", cfg.Builder, cfg.Module)
    83  	}
    84  	return builder.(func(string, *core.Context, core.Params) (core.Actor, error))(name, ctx, core.Params(cfg.Params))
    85  }