maybe
This commit is contained in:
@@ -3,17 +3,18 @@ package src
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
valkey "github.com/redis/go-redis/v9"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Listen string `json:"LISTEN"`
|
||||
Forwards string `json:"FORWARDS"`
|
||||
forwards []*valkey.Client
|
||||
forwards []*sync.Pool
|
||||
}
|
||||
|
||||
func NewConfig(ctx context.Context) (Config, error) {
|
||||
@@ -38,17 +39,19 @@ func NewConfig(ctx context.Context) (Config, error) {
|
||||
|
||||
forwards := strings.Split(config.Forwards, ",")
|
||||
forwards = slices.DeleteFunc(forwards, func(s string) bool { return s == "" })
|
||||
config.forwards = make([]*valkey.Client, len(forwards))
|
||||
if len(forwards) == 0 {
|
||||
return config, fmt.Errorf("at least one $FORWARD required")
|
||||
}
|
||||
config.forwards = make([]*sync.Pool, len(forwards))
|
||||
for i := range forwards {
|
||||
opt, err := valkey.ParseURL(forwards[i])
|
||||
if err != nil {
|
||||
config.Close()
|
||||
return config, err
|
||||
}
|
||||
config.forwards[i] = valkey.NewClient(opt)
|
||||
if err != nil {
|
||||
config.Close()
|
||||
return config, err
|
||||
config.forwards[i] = &sync.Pool{
|
||||
New: func() any {
|
||||
v, err := (&net.Dialer{}).DialContext(ctx, "tcp", forwards[i])
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return v
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,7 +61,13 @@ func NewConfig(ctx context.Context) (Config, error) {
|
||||
func (c Config) Close() {
|
||||
for i := range c.forwards {
|
||||
if c.forwards[i] != nil {
|
||||
c.forwards[i].Close()
|
||||
c.forwards[i].New = nil
|
||||
for {
|
||||
got := c.forwards[i].Get()
|
||||
if got != nil {
|
||||
got.(net.Conn).Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user