Files
archie/internal/daemon/statestore_relay.go
T

120 lines
3.6 KiB
Go

package daemon
import (
"context"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
)
// stateStoreDialTimeout bounds one forwarded connection's dial. The target is
// local, so a dial that has not connected by then is a dead State Store rather
// than a slow one.
const stateStoreDialTimeout = 10 * time.Second
// stateStoreRelay carries one task's State Store traffic from an address on
// the host's container bridge to the target the daemon dialed. A container has
// its own network namespace, so a loopback target there names the container
// itself; the relay address is what the container is handed instead. The State
// Store keeps listening exactly where the operator bound it.
type stateStoreRelay struct {
target string
ln net.Listener
}
// openStateStoreRelay listens on gateway and forwards to addr. The listener is
// per task: it accepts only from the bridge the container joins, and closing
// it is what ends the task's access.
func openStateStoreRelay(ctx context.Context, gateway, addr string) (*stateStoreRelay, error) {
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", net.JoinHostPort(gateway, "0"))
if err != nil {
return nil, fmt.Errorf("state store relay: listen on %s: %w", gateway, err)
}
r := &stateStoreRelay{target: addr, ln: ln}
go r.serve(ctx)
return r, nil
}
// Addr is the address the task container is handed.
func (r *stateStoreRelay) Addr() string { return r.ln.Addr().String() }
// Close stops accepting. Streams already open end when the container that owns
// them goes away, and a restarted daemon leaves no listener behind: the
// sockets belong to its process.
func (r *stateStoreRelay) Close() { _ = r.ln.Close() }
func (r *stateStoreRelay) serve(ctx context.Context) {
for {
downstream, err := r.ln.Accept()
if err != nil {
return
}
go r.forward(ctx, downstream)
}
}
// forward joins one container stream to the target, half-closing each side so
// a gRPC stream drains in both directions before the pair ends.
func (r *stateStoreRelay) forward(ctx context.Context, downstream net.Conn) {
ctx, cancel := context.WithTimeout(ctx, stateStoreDialTimeout)
defer cancel()
upstream, err := (&net.Dialer{}).DialContext(ctx, "tcp", r.target)
if err != nil {
_ = downstream.Close()
return
}
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); copyAndClose(upstream, downstream) }()
go func() { defer wg.Done(); copyAndClose(downstream, upstream) }()
wg.Wait()
_ = downstream.Close()
_ = upstream.Close()
}
func copyAndClose(dst, src net.Conn) {
_, _ = io.Copy(dst, src)
if half, ok := dst.(interface{ CloseWrite() error }); ok {
_ = half.CloseWrite()
return
}
_ = dst.Close()
}
// loopbackStateStoreTarget splits target into the address to forward to, and
// reports whether it is this host's loopback: the one case a task container
// cannot reach on its own. A target already addressed for the containers'
// network needs no relay.
func loopbackStateStoreTarget(target string) (addr string, loopback bool) {
addr, host, ok := stateStoreTargetAddr(target)
if !ok || !loopbackHost(host) {
return "", false
}
return addr, true
}
// stateStoreTargetAddr is target as host:port, accepting the gRPC scheme form
// ("dns:///host:port") the configuration allows.
func stateStoreTargetAddr(target string) (addr, host string, ok bool) {
target = strings.TrimSpace(target)
if _, rest, found := strings.Cut(target, ":///"); found {
target = rest
}
host, _, err := net.SplitHostPort(target)
if err != nil {
return "", "", false
}
return target, host, true
}
func loopbackHost(host string) bool {
if host == "localhost" {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}