From 99a63d048cd29eaaa69866ed72b5c0dab52091fb Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Thu, 10 Aug 2023 12:59:45 -0600
Subject: [PATCH] listen on unix socket
---
go.mod | 1 +
go.sum | 1 +
lib/gat/configs/pgbouncer/config.go | 47 ++++++++++++++++++++++++-----
lib/gat/pooler.go | 7 +----
4 files changed, 42 insertions(+), 14 deletions(-)
diff --git a/go.mod b/go.mod
index 0b32b1df..d4e104c0 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.20
require (
github.com/google/uuid v1.3.0
github.com/xdg-go/scram v1.1.2
+ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
diff --git a/go.sum b/go.sum
index 8d7e7b62..0565953b 100644
--- a/go.sum
+++ b/go.sum
@@ -284,6 +284,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
diff --git a/lib/gat/configs/pgbouncer/config.go b/lib/gat/configs/pgbouncer/config.go
index 42857716..98ca3fc7 100644
--- a/lib/gat/configs/pgbouncer/config.go
+++ b/lib/gat/configs/pgbouncer/config.go
@@ -6,8 +6,11 @@ import (
"net"
"os"
"strconv"
+ "strings"
"time"
+ "golang.org/x/sync/errgroup"
+
"pggat2/lib/auth/credentials"
"pggat2/lib/gat"
"pggat2/lib/gat/pools/session"
@@ -360,19 +363,47 @@ func (T *Config) ListenAndServe(pooler *gat.Pooler) error {
}
}
+ var wg errgroup.Group
+
if T.PgBouncer.ListenAddr != "" {
- listenAddr := T.PgBouncer.ListenAddr
- if listenAddr == "*" {
- listenAddr = ""
- }
+ wg.Go(func() error {
+ listenAddr := T.PgBouncer.ListenAddr
+ if listenAddr == "*" {
+ listenAddr = ""
+ }
- listen := net.JoinHostPort(listenAddr, strconv.Itoa(T.PgBouncer.ListenPort))
+ listen := net.JoinHostPort(listenAddr, strconv.Itoa(T.PgBouncer.ListenPort))
- log.Println("listening on", listen)
+ log.Println("listening on", listen)
- return pooler.ListenAndServe(listen)
+ listener, err := net.Listen("tcp", listen)
+ if err != nil {
+ return err
+ }
+
+ return pooler.ListenAndServe(listener)
+ })
}
// listen on unix socket
- return nil
+ wg.Go(func() error {
+ dir := T.PgBouncer.UnixSocketDir
+ port := T.PgBouncer.ListenPort
+
+ if !strings.HasSuffix(dir, "/") {
+ dir = dir + "/"
+ }
+ dir = dir + ".s.PGSQL." + strconv.Itoa(port)
+
+ listener, err := net.Listen("unix", dir)
+ if err != nil {
+ return err
+ }
+
+ log.Printf("listening on unix:%s", dir)
+
+ return pooler.ListenAndServe(listener)
+ })
+
+ return wg.Wait()
}
diff --git a/lib/gat/pooler.go b/lib/gat/pooler.go
index a68e7c2e..cf04f0bf 100644
--- a/lib/gat/pooler.go
+++ b/lib/gat/pooler.go
@@ -69,12 +69,7 @@ func (T *Pooler) Serve(client zap.ReadWriter) {
pool.Serve(client, startupParameters)
}
-func (T *Pooler) ListenAndServe(address string) error {
- listener, err := net.Listen("tcp", address)
- if err != nil {
- return err
- }
-
+func (T *Pooler) ListenAndServe(listener net.Listener) error {
for {
conn, err := listener.Accept()
if err != nil {
--
GitLab