Compare commits
6 Commits
01a699186e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d745119615 | ||
|
|
503d078b78 | ||
|
|
b867d81984 | ||
|
|
25b5218e5a | ||
|
|
5db6a4360e | ||
|
|
057c5d0d25 |
26
go.mod
Executable file
26
go.mod
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
module red-apter
|
||||||
|
|
||||||
|
go 1.24.0
|
||||||
|
|
||||||
|
toolchain go1.24.12
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/lib/pq v1.11.1
|
||||||
|
github.com/redis/go-redis/v9 v9.17.3
|
||||||
|
modernc.org/sqlite v1.44.3
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||||
|
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
|
||||||
|
golang.org/x/sys v0.37.0 // indirect
|
||||||
|
modernc.org/libc v1.67.6 // indirect
|
||||||
|
modernc.org/mathutil v1.7.1 // indirect
|
||||||
|
modernc.org/memory v1.11.0 // indirect
|
||||||
|
)
|
||||||
65
go.sum
Executable file
65
go.sum
Executable file
@@ -0,0 +1,65 @@
|
|||||||
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||||
|
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||||
|
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||||
|
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||||
|
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||||
|
github.com/lib/pq v1.11.1 h1:wuChtj2hfsGmmx3nf1m7xC2XpK6OtelS2shMY+bGMtI=
|
||||||
|
github.com/lib/pq v1.11.1/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||||
|
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||||
|
github.com/redis/go-redis/v9 v9.17.3 h1:fN29NdNrE17KttK5Ndf20buqfDZwGNgoUr9qjl1DQx4=
|
||||||
|
github.com/redis/go-redis/v9 v9.17.3/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||||
|
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||||
|
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
|
||||||
|
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
|
||||||
|
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
|
||||||
|
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
|
||||||
|
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
|
||||||
|
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
||||||
|
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
||||||
|
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
||||||
|
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
|
||||||
|
modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||||
|
modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc=
|
||||||
|
modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM=
|
||||||
|
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
|
||||||
|
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||||
|
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||||
|
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||||
|
modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE=
|
||||||
|
modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||||
|
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||||
|
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||||
|
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
|
||||||
|
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
|
||||||
|
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||||
|
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||||
|
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||||
|
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||||
|
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||||
|
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||||
|
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||||
|
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||||
|
modernc.org/sqlite v1.44.3 h1:+39JvV/HWMcYslAwRxHb8067w+2zowvFOUrOWIy9PjY=
|
||||||
|
modernc.org/sqlite v1.44.3/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
|
||||||
|
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||||
|
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||||
|
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||||
|
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||||
17
main.go
Executable file
17
main.go
Executable file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os/signal"
|
||||||
|
"red-apter/src"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
|
||||||
|
defer can()
|
||||||
|
|
||||||
|
if err := src.Main(ctx); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
144
src/adapt.go
Executable file
144
src/adapt.go
Executable file
@@ -0,0 +1,144 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func adapt(ctx context.Context, config Config, conn net.Conn) error {
|
||||||
|
reader := bufio.NewReader(conn)
|
||||||
|
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
if done, err := func() (bool, error) {
|
||||||
|
raw, message, err := readMessage(reader)
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
log.Printf("routing: %q (%+v)", raw, message)
|
||||||
|
|
||||||
|
if len(message) > 0 && message[0].(string) == "COMMAND" {
|
||||||
|
fmt.Fprintf(conn, "*0\r\n")
|
||||||
|
} else if len(raw) > 0 && len(message) > 0 {
|
||||||
|
hashKey := message[0].(string)
|
||||||
|
if len(message) > 1 {
|
||||||
|
hashKey = message[1].(string)
|
||||||
|
}
|
||||||
|
if err := config.WithConn(hashKey, func(forwardConn net.Conn) error {
|
||||||
|
//log.Printf("forwarding %q", raw)
|
||||||
|
|
||||||
|
written := 0
|
||||||
|
for written < len(raw) {
|
||||||
|
more, err := forwardConn.Write(raw[written:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
written += more
|
||||||
|
}
|
||||||
|
|
||||||
|
replyer := bufio.NewReader(forwardConn)
|
||||||
|
//log.Printf("reading reply to %q", raw)
|
||||||
|
_, err = readMessageTo(conn, replyer)
|
||||||
|
//log.Printf("read reply %q", raw)
|
||||||
|
return err
|
||||||
|
}); err != nil {
|
||||||
|
return true, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, nil
|
||||||
|
}(); err != nil {
|
||||||
|
return err
|
||||||
|
} else if done {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
func readMessage(reader *bufio.Reader) ([]byte, []any, error) {
|
||||||
|
w := bytes.NewBuffer(nil)
|
||||||
|
arr, err := readMessageTo(w, reader)
|
||||||
|
return w.Bytes(), arr, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func readMessageTo(w io.Writer, reader *bufio.Reader) ([]any, error) {
|
||||||
|
firstLine, _, err := reader.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
firstLine = bytes.TrimSuffix(firstLine, []byte("\r\n"))
|
||||||
|
if len(firstLine) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s\r\n", firstLine)
|
||||||
|
|
||||||
|
switch firstLine[0] {
|
||||||
|
case '+': // simple string, like +OK
|
||||||
|
return []any{string(firstLine[1:])}, nil
|
||||||
|
case '-': // simple error, like -message
|
||||||
|
return []any{string(firstLine)}, nil
|
||||||
|
case ':': // number, like /[+-][0-9]+/
|
||||||
|
firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+"))
|
||||||
|
n, err := strconv.Atoi(string(firstLine))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("num not a num: %q: %w", firstLine, err)
|
||||||
|
}
|
||||||
|
return []any{n}, err
|
||||||
|
case '$': // long string, like $-1 for nil, like $LEN\r\nSTRING\r\n
|
||||||
|
if firstLine[1] == '-' {
|
||||||
|
return []any{nil}, nil
|
||||||
|
}
|
||||||
|
firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+"))
|
||||||
|
n, err := strconv.Atoi(string(firstLine))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("num not a num: %q: %w", firstLine, err)
|
||||||
|
}
|
||||||
|
//log.Printf("reading %v+2 bytes for bulk string", n)
|
||||||
|
nextLine := make([]byte, n+2)
|
||||||
|
nAt := 0
|
||||||
|
for nAt < n+2 {
|
||||||
|
nMore, err := reader.Read(nextLine[nAt:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("couldnt read %v more/%v bytes for long string: %w", n+2-nAt, n+2, err)
|
||||||
|
}
|
||||||
|
nAt += nMore
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%s", nextLine)
|
||||||
|
return []any{string(nextLine)}, err
|
||||||
|
case '*': // array, like *-1 for nil, like *4 for [1,2,3,4]
|
||||||
|
n, err := strconv.Atoi(string(firstLine[1:]))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("arr not a num: %q: %w", firstLine, err)
|
||||||
|
} else if n == -1 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
var result []any
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
more, err := readMessageTo(w, reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
result = append(result, more...)
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
case '_': // nil
|
||||||
|
return nil, nil
|
||||||
|
case '#': // boolean, like #t or #f
|
||||||
|
return []any{firstLine[1] == 't'}, nil
|
||||||
|
case ',': // double
|
||||||
|
log.Fatal("not impl")
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatalf("not impl: %q", firstLine)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
130
src/config.go
Executable file
130
src/config.go
Executable file
@@ -0,0 +1,130 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"hash/crc32"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Listen string `json:"LISTEN"`
|
||||||
|
Forwards string `json:"FORWARDS"`
|
||||||
|
Hello string `json:"HELLO_B64"`
|
||||||
|
forwards []*sync.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig(ctx context.Context) (Config, error) {
|
||||||
|
config := Config{
|
||||||
|
Listen: ":10000",
|
||||||
|
Forwards: "",
|
||||||
|
Hello: base64.StdEncoding.EncodeToString([]byte("*1\r\n$4\r\nping\r\n")),
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(config)
|
||||||
|
var m map[string]any
|
||||||
|
if err := json.Unmarshal(b, &m); err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
for k := range m {
|
||||||
|
if v := os.Getenv(k); v != "" {
|
||||||
|
m[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b2, _ := json.Marshal(m)
|
||||||
|
if err := json.Unmarshal(b2, &config); err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hello, err := base64.StdEncoding.DecodeString(config.Hello)
|
||||||
|
if err != nil {
|
||||||
|
return config, err
|
||||||
|
}
|
||||||
|
|
||||||
|
forwards := strings.Split(config.Forwards, ",")
|
||||||
|
forwards = slices.DeleteFunc(forwards, func(s string) bool { return s == "" })
|
||||||
|
if len(forwards) == 0 {
|
||||||
|
return config, fmt.Errorf("at least one $FORWARD required")
|
||||||
|
}
|
||||||
|
config.forwards = make([]*sync.Pool, len(forwards))
|
||||||
|
for i := range forwards {
|
||||||
|
forward := forwards[i]
|
||||||
|
config.forwards[i] = &sync.Pool{
|
||||||
|
New: func() any {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
log.Printf("dialing %q with %q", forward, hello)
|
||||||
|
v, err := (&net.Dialer{}).DialContext(ctx, "tcp", forward)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("! failed dial %q: %v", forward, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, err := v.Write([]byte(hello)); err != nil {
|
||||||
|
log.Printf("! failed write hello %q: %v", forward, err)
|
||||||
|
v.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if raw, _, err := readMessage(bufio.NewReader(v)); err != nil {
|
||||||
|
log.Printf("! failed read hello %q: %v", forward, err)
|
||||||
|
v.Close()
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
log.Printf("dial reply: %q", raw)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) Close() {
|
||||||
|
for i := range c.forwards {
|
||||||
|
if c.forwards[i] != nil {
|
||||||
|
i := i
|
||||||
|
go func() {
|
||||||
|
c.forwards[i].New = nil
|
||||||
|
for {
|
||||||
|
got := c.forwards[i].Get()
|
||||||
|
if got != nil {
|
||||||
|
go got.(net.Conn).Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Config) WithConn(hashKey string, foo func(net.Conn) error) error {
|
||||||
|
hash := int(crc32.ChecksumIEEE([]byte(hashKey)))
|
||||||
|
hashIdx := hash % len(c.forwards)
|
||||||
|
forward := c.forwards[hashIdx]
|
||||||
|
|
||||||
|
log.Printf("acquire conn %v", hashIdx)
|
||||||
|
forwardCon := forward.Get()
|
||||||
|
if forwardCon == nil {
|
||||||
|
log.Printf("got a nil conn to %v", hashIdx)
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
forwardConn := forwardCon.(net.Conn)
|
||||||
|
|
||||||
|
if err := foo(forwardConn); err != nil {
|
||||||
|
log.Printf("errored with conn %v: %v", hashIdx, err)
|
||||||
|
forwardConn.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("release conn %v", hashIdx)
|
||||||
|
forward.Put(forwardConn)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
54
src/listen.go
Executable file
54
src/listen.go
Executable file
@@ -0,0 +1,54 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
func listen(ctx context.Context, config Config) error {
|
||||||
|
defer log.Println("/listen()")
|
||||||
|
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
defer wg.Wait()
|
||||||
|
|
||||||
|
listener, err := net.Listen("tcp", config.Listen)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
<-ctx.Done()
|
||||||
|
listener.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
conn, err := listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error accepting:", err)
|
||||||
|
} else {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer log.Println("/handle()")
|
||||||
|
defer wg.Done()
|
||||||
|
handle(ctx, config, conn)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func handle(ctx context.Context, config Config, conn net.Conn) {
|
||||||
|
if err := _handle(ctx, config, conn); err != nil {
|
||||||
|
log.Println("error handling:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func _handle(ctx context.Context, config Config, conn net.Conn) error {
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
return adapt(ctx, config, conn)
|
||||||
|
}
|
||||||
22
src/main.go
Executable file
22
src/main.go
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
package src
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Main(ctx context.Context) error {
|
||||||
|
defer log.Println("/Main()")
|
||||||
|
|
||||||
|
config, err := NewConfig(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer config.Close()
|
||||||
|
|
||||||
|
if err := listen(ctx, config); err != nil && ctx.Err() == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
17
src/testdata/redises.sh
vendored
Normal file
17
src/testdata/redises.sh
vendored
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#! /usr/bin/bash
|
||||||
|
|
||||||
|
d=$(mktemp -d)
|
||||||
|
mkdir -p $d/1
|
||||||
|
mkdir -p $d/2
|
||||||
|
valkey-server --dir $d/1 --port 60113 --logfile $d/1.log &
|
||||||
|
bg=${!}
|
||||||
|
cleanup() {
|
||||||
|
kill $bg
|
||||||
|
}
|
||||||
|
trap cleanup EXIT
|
||||||
|
(
|
||||||
|
sleep 5
|
||||||
|
echo FORWARDS=127.0.0.1:60113,127.0.0.1:60114
|
||||||
|
) &
|
||||||
|
valkey-server --dir $d/2 --port 60114 --logfile $d/2.log
|
||||||
|
|
||||||
Reference in New Issue
Block a user