2023-04-24 20:52:12 +02:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LayerOptions struct {
|
|
|
|
Capacity int64 `mapstructure:"capacity"`
|
|
|
|
KeepAlive time.Duration `mapstructure:"keepAlive"`
|
2023-07-05 21:54:01 +02:00
|
|
|
MatchURLs []string `mapstructure:"matchURLs"`
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
2023-06-13 03:57:13 +02:00
|
|
|
func fromStoreOptions(storeOptions store.LayerOptions, defaultKeepAlive time.Duration) (*LayerOptions, error) {
|
2023-04-24 20:52:12 +02:00
|
|
|
layerOptions := LayerOptions{
|
|
|
|
Capacity: 1000,
|
2023-06-13 03:57:13 +02:00
|
|
|
KeepAlive: defaultKeepAlive,
|
2023-07-05 21:54:01 +02:00
|
|
|
MatchURLs: []string{"*"},
|
2023-04-24 20:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
config := mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: stringToDurationHook,
|
|
|
|
Result: &layerOptions,
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder, err := mapstructure.NewDecoder(&config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := decoder.Decode(storeOptions); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &layerOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringToDurationHook(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
|
|
|
|
if t == reflect.TypeOf(*new(time.Duration)) && f == reflect.TypeOf("") {
|
|
|
|
return time.ParseDuration(data.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|