feat(layer,queue): display templatized page for queued users
All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good

This commit is contained in:
2023-06-12 19:57:13 -06:00
parent 830d4d3904
commit 2a8849493d
14 changed files with 290 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"strings"
"time"
"forge.cadoles.com/cadoles/bouncer/internal/command/common"
"forge.cadoles.com/cadoles/bouncer/internal/config"
@ -83,5 +84,16 @@ func initQueueLayer(ctx context.Context, conf *config.Config) (*queue.Queue, err
return nil, errors.WithStack(err)
}
return queue.New(adapter), nil
options := []queue.OptionFunc{
queue.WithTemplateDir(string(conf.Layers.Queue.TemplateDir)),
}
if conf.Layers.Queue.DefaultKeepAlive != nil {
options = append(options, queue.WithDefaultKeepAlive(time.Duration(*conf.Layers.Queue.DefaultKeepAlive)))
}
return queue.New(
adapter,
options...,
), nil
}

View File

@ -14,6 +14,7 @@ type Config struct {
Proxy ProxyServerConfig `yaml:"proxy"`
Redis RedisConfig `yaml:"redis"`
Logger LoggerConfig `yaml:"logger"`
Layers LayersConfig `yaml:"layers"`
}
// NewFromFile retrieves the configuration from the given file
@ -46,6 +47,7 @@ func NewDefault() *Config {
Proxy: NewDefaultProxyServerConfig(),
Logger: NewDefaultLoggerConfig(),
Redis: NewDefaultRedisConfig(),
Layers: NewDefaultLayersConfig(),
}
}

View File

@ -4,6 +4,7 @@ import (
"os"
"regexp"
"strconv"
"time"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
@ -123,3 +124,37 @@ func (iss *InterpolatedStringSlice) UnmarshalYAML(value *yaml.Node) error {
return nil
}
type InterpolatedDuration time.Duration
func (id *InterpolatedDuration) UnmarshalYAML(value *yaml.Node) error {
var str string
if err := value.Decode(&str); err != nil {
return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
}
if match := reVar.FindStringSubmatch(str); len(match) > 0 {
str = os.Getenv(match[1])
}
duration, err := time.ParseDuration(str)
if err != nil {
return errors.Wrapf(err, "could not parse duration '%v', line '%d'", str, value.Line)
}
*id = InterpolatedDuration(duration)
return nil
}
func (id *InterpolatedDuration) MarshalYAML() (interface{}, error) {
duration := time.Duration(*id)
return duration.String(), nil
}
func NewInterpolatedDuration(d time.Duration) *InterpolatedDuration {
id := InterpolatedDuration(d)
return &id
}

21
internal/config/layers.go Normal file
View File

@ -0,0 +1,21 @@
package config
import "time"
type LayersConfig struct {
Queue QueueLayerConfig `yaml:"queue"`
}
func NewDefaultLayersConfig() LayersConfig {
return LayersConfig{
Queue: QueueLayerConfig{
TemplateDir: "./layers/queue/templates",
DefaultKeepAlive: NewInterpolatedDuration(time.Minute),
},
}
}
type QueueLayerConfig struct {
TemplateDir InterpolatedString `yaml:"templateDir"`
DefaultKeepAlive *InterpolatedDuration `yaml:"defaultKeepAlive"`
}

View File

@ -15,11 +15,11 @@ type LayerOptions struct {
KeepAlive time.Duration `mapstructure:"keepAlive"`
}
func fromStoreOptions(storeOptions store.LayerOptions) (*LayerOptions, error) {
func fromStoreOptions(storeOptions store.LayerOptions, defaultKeepAlive time.Duration) (*LayerOptions, error) {
layerOptions := LayerOptions{
Capacity: 1000,
Matchers: []string{"*"},
KeepAlive: 30 * time.Second,
KeepAlive: defaultKeepAlive,
}
config := mapstructure.DecoderConfig{

View File

@ -1,9 +1,29 @@
package queue
type Options struct{}
import "time"
type Options struct {
TemplateDir string
DefaultKeepAlive time.Duration
}
type OptionFunc func(*Options)
func defaultOptions() *Options {
return &Options{}
return &Options{
TemplateDir: "./templates",
DefaultKeepAlive: time.Minute,
}
}
func WithTemplateDir(templateDir string) OptionFunc {
return func(o *Options) {
o.TemplateDir = templateDir
}
}
func WithDefaultKeepAlive(keepAlive time.Duration) OptionFunc {
return func(o *Options) {
o.DefaultKeepAlive = keepAlive
}
}

View File

@ -3,13 +3,17 @@ package queue
import (
"context"
"fmt"
"html/template"
"net/http"
"path/filepath"
"sync"
"sync/atomic"
"time"
"forge.cadoles.com/Cadoles/go-proxy"
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
"forge.cadoles.com/cadoles/bouncer/internal/store"
"github.com/Masterminds/sprig/v3"
"github.com/google/uuid"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
@ -18,7 +22,14 @@ import (
const LayerType store.LayerType = "queue"
type Queue struct {
adapter Adapter
adapter Adapter
defaultKeepAlive time.Duration
templateDir string
loadOnce sync.Once
tmpl *template.Template
refreshJobRunning uint32
}
@ -33,7 +44,7 @@ func (q *Queue) Middleware(layer *store.Layer) proxy.Middleware {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
options, err := fromStoreOptions(layer.Options)
options, err := fromStoreOptions(layer.Options, q.defaultKeepAlive)
if err != nil {
logger.Error(ctx, "could not parse layer options", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@ -102,7 +113,52 @@ func (q *Queue) renderQueuePage(w http.ResponseWriter, r *http.Request, queueNam
return
}
http.Error(w, fmt.Sprintf("queued (rank: %d, status: %d/%d)", rank+1, status.Sessions, options.Capacity), http.StatusServiceUnavailable)
q.loadOnce.Do(func() {
pattern := filepath.Join(q.templateDir, "*.gohtml")
logger.Info(ctx, "loading queue page templates", logger.F("pattern", pattern))
tmpl, err := template.New("").Funcs(sprig.FuncMap()).ParseGlob(pattern)
if err != nil {
logger.Error(ctx, "could not load queue templates", logger.E(errors.WithStack(err)))
return
}
q.tmpl = tmpl
})
if q.tmpl == nil {
logger.Error(ctx, "queue page templates not loaded", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
templateData := struct {
QueueName string
LayerOptions *LayerOptions
Rank int64
CurrentSessions int64
MaxSessions int64
RefreshRate int64
}{
QueueName: queueName,
LayerOptions: options,
Rank: rank + 1,
CurrentSessions: status.Sessions,
MaxSessions: options.Capacity,
RefreshRate: 5,
}
w.WriteHeader(http.StatusServiceUnavailable)
if err := q.tmpl.ExecuteTemplate(w, "queue", templateData); err != nil {
logger.Error(ctx, "could not render queue page", logger.E(errors.WithStack(err)))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func (q *Queue) refreshQueue(queueName string, keepAlive time.Duration) {
@ -136,7 +192,9 @@ func New(adapter Adapter, funcs ...OptionFunc) *Queue {
}
return &Queue{
adapter: adapter,
adapter: adapter,
templateDir: opts.TemplateDir,
defaultKeepAlive: opts.DefaultKeepAlive,
}
}

View File

@ -10,10 +10,10 @@ import (
queueRedis "forge.cadoles.com/cadoles/bouncer/internal/queue/redis"
)
func NewQueueAdapter(ctx context.Context, conf config.RedisConfig) (queue.Adapter, error) {
func NewQueueAdapter(ctx context.Context, redisConf config.RedisConfig) (queue.Adapter, error) {
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: conf.Adresses,
MasterName: string(conf.Master),
Addrs: redisConf.Adresses,
MasterName: string(redisConf.Master),
})
return queueRedis.NewAdapter(rdb, 2), nil