bouncer/internal/setup/queue_layer.go

46 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-06-23 19:08:35 +02:00
package setup
import (
"time"
"forge.cadoles.com/cadoles/bouncer/internal/config"
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
2023-06-24 01:53:56 +02:00
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/queue"
queueRedis "forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/queue/redis"
2023-06-23 19:08:35 +02:00
"github.com/pkg/errors"
2023-06-24 01:53:56 +02:00
"github.com/redis/go-redis/v9"
2023-06-23 19:08:35 +02:00
)
func init() {
2023-06-24 01:53:56 +02:00
RegisterLayer(queue.LayerType, setupQueueLayer, queue.RawLayerOptionsSchema)
2023-06-23 19:08:35 +02:00
}
2023-06-24 01:53:56 +02:00
func setupQueueLayer(conf *config.Config) (director.Layer, error) {
adapter, err := newQueueAdapter(conf.Redis)
2023-06-23 19:08:35 +02:00
if err != nil {
return nil, errors.WithStack(err)
}
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
}
2023-06-24 01:53:56 +02:00
func newQueueAdapter(redisConf config.RedisConfig) (queue.Adapter, error) {
rdb := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: redisConf.Adresses,
MasterName: string(redisConf.Master),
})
return queueRedis.NewAdapter(rdb, 2), nil
}