From ce7f3697b0bd4a51a2a5502ad6e3f18bb6cf54b3 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Tue, 14 Nov 2023 19:44:53 -0600
Subject: [PATCH] load critics from json

---
 lib/gat/handlers/pool/pools/basic/config.go   |  5 +++++
 lib/gat/handlers/pool/pools/basic/factory.go  | 15 +++++++++++++++
 lib/gat/handlers/pool/pools/hybrid/config.go  |  7 +++++++
 lib/gat/handlers/pool/pools/hybrid/factory.go | 15 +++++++++++++++
 lib/gat/matchers/and.go                       |  6 ++++--
 lib/gat/matchers/or.go                        |  6 ++++--
 6 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/lib/gat/handlers/pool/pools/basic/config.go b/lib/gat/handlers/pool/pools/basic/config.go
index 626257a3..0af680c3 100644
--- a/lib/gat/handlers/pool/pools/basic/config.go
+++ b/lib/gat/handlers/pool/pools/basic/config.go
@@ -62,6 +62,9 @@ type Config struct {
 	// TrackedParameters are parameters which should be synced by updating the server, not the client.
 	TrackedParameters []strutil.CIString `json:"tracked_parameters,omitempty"`
 
+	RawCritics []json.RawMessage `json:"critics,omitempty" caddy:"namespace=pggat.handlers.pool.critics inline_key=critic"`
+	Critics    []pool.Critic     `json:"-"`
+
 	Logger *zap.Logger `json:"-"`
 }
 
@@ -75,6 +78,8 @@ func (T Config) Spool() spool.Config {
 		ReconnectInitialTime: time.Duration(T.ServerReconnectInitialTime),
 		ReconnectMaxTime:     time.Duration(T.ServerReconnectMaxTime),
 
+		Critics: T.Critics,
+
 		Logger: T.Logger,
 	}
 }
diff --git a/lib/gat/handlers/pool/pools/basic/factory.go b/lib/gat/handlers/pool/pools/basic/factory.go
index be4a0594..44d58c95 100644
--- a/lib/gat/handlers/pool/pools/basic/factory.go
+++ b/lib/gat/handlers/pool/pools/basic/factory.go
@@ -1,6 +1,8 @@
 package basic
 
 import (
+	"fmt"
+
 	"github.com/caddyserver/caddy/v2"
 
 	"gfx.cafe/gfx/pggat/lib/gat/handlers/pool"
@@ -26,6 +28,19 @@ func (T *Factory) CaddyModule() caddy.ModuleInfo {
 func (T *Factory) Provision(ctx caddy.Context) error {
 	T.Logger = ctx.Logger()
 
+	if T.RawCritics != nil {
+		raw, err := ctx.LoadModule(T, "RawCritics")
+		if err != nil {
+			return fmt.Errorf("loading critic module: %v", err)
+		}
+
+		val := raw.([]any)
+		T.Critics = make([]pool.Critic, 0, len(val))
+		for _, vv := range val {
+			T.Critics = append(T.Critics, vv.(pool.Critic))
+		}
+	}
+
 	raw, err := ctx.LoadModule(T, "RawPoolerFactory")
 	if err != nil {
 		return err
diff --git a/lib/gat/handlers/pool/pools/hybrid/config.go b/lib/gat/handlers/pool/pools/hybrid/config.go
index 4253c626..0b53ce2f 100644
--- a/lib/gat/handlers/pool/pools/hybrid/config.go
+++ b/lib/gat/handlers/pool/pools/hybrid/config.go
@@ -1,11 +1,13 @@
 package hybrid
 
 import (
+	"encoding/json"
 	"time"
 
 	"github.com/caddyserver/caddy/v2"
 	"go.uber.org/zap"
 
+	"gfx.cafe/gfx/pggat/lib/gat/handlers/pool"
 	"gfx.cafe/gfx/pggat/lib/gat/handlers/pool/poolers/rob"
 	"gfx.cafe/gfx/pggat/lib/gat/handlers/pool/spool"
 	"gfx.cafe/gfx/pggat/lib/util/strutil"
@@ -19,6 +21,9 @@ type Config struct {
 
 	TrackedParameters []strutil.CIString `json:"tracked_parameters,omitempty"`
 
+	RawCritics []json.RawMessage `json:"critics,omitempty" caddy:"namespace=pggat.handlers.pool.critics inline_key=critic"`
+	Critics    []pool.Critic     `json:"-"`
+
 	Logger *zap.Logger `json:"-"`
 }
 
@@ -31,6 +36,8 @@ func (T Config) Spool() spool.Config {
 		ReconnectInitialTime: time.Duration(T.ServerReconnectInitialTime),
 		ReconnectMaxTime:     time.Duration(T.ServerReconnectMaxTime),
 
+		Critics: T.Critics,
+
 		Logger: T.Logger,
 	}
 }
diff --git a/lib/gat/handlers/pool/pools/hybrid/factory.go b/lib/gat/handlers/pool/pools/hybrid/factory.go
index 3feae210..d189be6c 100644
--- a/lib/gat/handlers/pool/pools/hybrid/factory.go
+++ b/lib/gat/handlers/pool/pools/hybrid/factory.go
@@ -1,6 +1,8 @@
 package hybrid
 
 import (
+	"fmt"
+
 	"github.com/caddyserver/caddy/v2"
 
 	"gfx.cafe/gfx/pggat/lib/gat/handlers/pool"
@@ -26,6 +28,19 @@ func (T *Factory) CaddyModule() caddy.ModuleInfo {
 func (T *Factory) Provision(ctx caddy.Context) error {
 	T.Logger = ctx.Logger()
 
+	if T.RawCritics != nil {
+		raw, err := ctx.LoadModule(T, "RawCritics")
+		if err != nil {
+			return fmt.Errorf("loading critic module: %v", err)
+		}
+
+		val := raw.([]any)
+		T.Critics = make([]pool.Critic, 0, len(val))
+		for _, vv := range val {
+			T.Critics = append(T.Critics, vv.(pool.Critic))
+		}
+	}
+
 	return nil
 }
 
diff --git a/lib/gat/matchers/and.go b/lib/gat/matchers/and.go
index e4106ed9..5f782d01 100644
--- a/lib/gat/matchers/and.go
+++ b/lib/gat/matchers/and.go
@@ -32,12 +32,14 @@ func (T *And) CaddyModule() caddy.ModuleInfo {
 func (T *And) Provision(ctx caddy.Context) error {
 	T.and = make([]gat.Matcher, 0, len(T.And))
 	if T.And != nil {
-		val, err := ctx.LoadModule(T, "And")
+		raw, err := ctx.LoadModule(T, "And")
 		if err != nil {
 			return fmt.Errorf("loading matcher module: %v", err)
 		}
 
-		for _, vv := range val.([]any) {
+		val := raw.([]any)
+		T.and = make([]gat.Matcher, 0, len(val))
+		for _, vv := range val {
 			T.and = append(T.and, vv.(gat.Matcher))
 		}
 	}
diff --git a/lib/gat/matchers/or.go b/lib/gat/matchers/or.go
index baf7e89f..2812c504 100644
--- a/lib/gat/matchers/or.go
+++ b/lib/gat/matchers/or.go
@@ -32,12 +32,14 @@ func (T *Or) CaddyModule() caddy.ModuleInfo {
 func (T *Or) Provision(ctx caddy.Context) error {
 	T.or = make([]gat.Matcher, 0, len(T.Or))
 	if T.Or != nil {
-		val, err := ctx.LoadModule(T, "Or")
+		raw, err := ctx.LoadModule(T, "Or")
 		if err != nil {
 			return fmt.Errorf("loading matcher module: %v", err)
 		}
 
-		for _, vv := range val.([]any) {
+		val := raw.([]any)
+		T.or = make([]gat.Matcher, 0, len(val))
+		for _, vv := range val {
 			T.or = append(T.or, vv.(gat.Matcher))
 		}
 	}
-- 
GitLab