knutsel closed pull request #2350: Add Grove configurable plugin 
enable/disabling
URL: https://github.com/apache/incubator-trafficcontrol/pull/2350
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/grove/README.md b/grove/README.md
index f8f669e72..80062d2d0 100644
--- a/grove/README.md
+++ b/grove/README.md
@@ -66,6 +66,7 @@ The config file is JSON of the following format:
   "port": 8080,
   "cache_size_bytes": 50000,
   "remap_rules_file": "./remap.json",
+  "plugins": ["ats_log", "http_stats", "if_modified_since", "record_stats"],
 ```
 
 The config file has the following fields:
@@ -96,6 +97,7 @@ The config file has the following fields:
 | `server_write_timeout_ms` | The length of time in milliseconds to allow a 
client to write data, before the connection is terminated. This value should be 
carefully considered, as too short a timeout will result in terminating 
legitimate clients with slow connections, while too long a timeout will make 
the server vulnerable to SlowLoris attacks.|
 | `cache_files` | Groups of cache files to use for disk caching. See [Disk 
Cache](#disk-cache) |
 | `file_mem_bytes` | The size in bytes of the memory cache to use for each 
group of cache files. Note this size is used for each group, and thus the total 
memory used is `file_mem_bytes*len(cache_files)+cache_size_bytes`.  See [Disk 
Cache](#disk-cache) |
+| `plugins` | An array of plugins to enable |
 
 # Remap Rules
 
diff --git a/grove/config/config.go b/grove/config/config.go
index c282e476a..857d4f6e0 100644
--- a/grove/config/config.go
+++ b/grove/config/config.go
@@ -47,6 +47,8 @@ type Config struct {
        LogLocationDebug   string `json:"log_location_debug"`
        LogLocationEvent   string `json:"log_location_event"`
 
+       Plugins []string `json:"plugins"`
+
        ReqTimeoutMS         int `json:"parent_request_timeout_ms"` // TODO 
rename "parent_request" to distinguish from client requests
        ReqKeepAliveMS       int `json:"parent_request_keep_alive_ms"`
        ReqMaxIdleConns      int `json:"parent_request_max_idle_connections"`
diff --git a/grove/grove.cfg b/grove/grove.cfg
index 3f0a3fedb..25ad0cb2e 100644
--- a/grove/grove.cfg
+++ b/grove/grove.cfg
@@ -2,7 +2,8 @@
   "rfc_compliant": false,
   "port": 8080,
   "cache_size_bytes": 50000,
-  "remap_rules_file": "./remap.json"
+  "remap_rules_file": "./remap.json",
+  "plugins": ["ats_log", "http_stats", "if_modified_since", "record_stats"],
   "log_location_error": "/var/log/grove/grove.log",
   "log_location_event": "/var/log/grove/access.log",
   "log_location_warn": "/var/log/grove/grove.log"
diff --git a/grove/grove.go b/grove/grove.go
index a610d466d..55dc2eea1 100644
--- a/grove/grove.go
+++ b/grove/grove.go
@@ -93,7 +93,7 @@ func main() {
        reqIdleConnTimeout := time.Duration(cfg.ReqIdleConnTimeoutMS) * 
time.Millisecond
        baseTransport := remap.NewRemappingTransport(reqTimeout, reqKeepAlive, 
reqMaxIdleConns, reqIdleConnTimeout)
 
-       plugins := plugin.Get()
+       plugins := plugin.Get(cfg.Plugins)
        remapper, err := remap.LoadRemapper(cfg.RemapRulesFile, 
plugins.LoadFuncs(), caches, baseTransport)
        if err != nil {
                log.Errorf("starting service: loading remap rules: %v\n", err)
@@ -193,6 +193,7 @@ func main() {
                        log.Warnln("reloading config: caches changed in new 
config! Dynamic cache reloading is not supported! Old cache files and sizes 
will be used, and new cache config will NOT be loaded! Restart service to apply 
cache changes!")
                }
 
+               plugins = plugin.Get(cfg.Plugins)
                oldRemapper := remapper
                remapper, err = remap.LoadRemapper(cfg.RemapRulesFile, 
plugins.LoadFuncs(), caches, baseTransport)
                if err != nil {
@@ -254,6 +255,8 @@ func main() {
                )
                httpsHandler.Set(httpsCacheHandler)
 
+               plugins.OnStartup(remapper.PluginCfg(), pluginContext, 
plugin.StartupData{Config: cfg, Shared: remapper.PluginSharedCfg()})
+
                if cfg.Port != oldCfg.Port {
                        ctx, cancel := 
context.WithTimeout(context.Background(), ShutdownTimeout)
                        defer cancel()
diff --git a/grove/plugin/hello_context.go b/grove/plugin/hello_context.go
index eb162233c..69ffce552 100644
--- a/grove/plugin/hello_context.go
+++ b/grove/plugin/hello_context.go
@@ -19,7 +19,7 @@ import (
 )
 
 func init() {
-       // AddPlugin(10000, Funcs{startup: helloCtxStart, afterRespond: 
helloCtxAfterResp})
+       AddPlugin(10000, Funcs{startup: helloCtxStart, afterRespond: 
helloCtxAfterResp})
 }
 
 func helloCtxStart(icfg interface{}, d StartupData) {
diff --git a/grove/plugin/hello_world.go b/grove/plugin/hello_world.go
index 9682a0866..6e3f8112d 100644
--- a/grove/plugin/hello_world.go
+++ b/grove/plugin/hello_world.go
@@ -19,7 +19,7 @@ import (
 )
 
 func init() {
-       // AddPlugin(10000, Funcs{startup: hello})
+       AddPlugin(10000, Funcs{startup: hello})
 }
 
 func hello(icfg interface{}, d StartupData) {
diff --git a/grove/plugin/plugin.go b/grove/plugin/plugin.go
index 140c4e4f8..b084fefe2 100644
--- a/grove/plugin/plugin.go
+++ b/grove/plugin/plugin.go
@@ -132,9 +132,20 @@ func (p pluginsSlice) Swap(i, j int)      { p[i], p[j] = 
p[j], p[i] }
 
 var plugins = pluginsSlice{}
 
-func Get() Plugins {
-       sort.Sort(plugins)
-       return pluginsSlice(plugins)
+func Get(enabled []string) Plugins {
+       enabledM := map[string]struct{}{}
+       for _, name := range enabled {
+               enabledM[name] = struct{}{}
+       }
+       enabledPlugins := pluginsSlice{}
+       for _, plugin := range plugins {
+               if _, ok := enabledM[plugin.name]; !ok {
+                       continue
+               }
+               enabledPlugins = append(enabledPlugins, plugin)
+       }
+       sort.Sort(enabledPlugins)
+       return enabledPlugins
 }
 
 type Plugins interface {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to