diff --git a/config_data.yml b/config_data.yml index 80362a2daeb25d9d90a35b44ea253908e28d342b..f80da562639c3f924944f03dfb3a5c3cfef0857f 100644 --- a/config_data.yml +++ b/config_data.yml @@ -13,7 +13,7 @@ general: admin_password: postgres pools: simple_db: - pool_mode: session + pool_mode: transaction default_role: primary query_parser_enabled: true primary_reads_enabled: true diff --git a/lib/gat/gatling/gatling.go b/lib/gat/gatling/gatling.go index 5187f13cc2d22daa1dd058ebbb637ee5da8de7f8..52857727544a192d25f8d1a1cc04a8bae2cbf558 100644 --- a/lib/gat/gatling/gatling.go +++ b/lib/gat/gatling/gatling.go @@ -153,20 +153,25 @@ func (g *Gatling) ListenAndServe(ctx context.Context) error { } go func() { for { - var c net.Conn - c, err = ln.Accept() - if err != nil { - log.Println("failed to accept connection:", err) - } + errch := make(chan error) go func() { + c, err := ln.Accept() + if err != nil { + errch <- err + } + close(errch) err = g.handleConnection(ctx, c) if err != nil { if err != io.EOF { log.Println("disconnected:", err) } - return } }() + + err = <-errch + if err != nil { + log.Println("failed to accept connection:", err) + } } }() return nil diff --git a/lib/gat/pool/session/pool.go b/lib/gat/pool/session/pool.go index abf652b12d09c335524a23041e9db1f3caa02c36..20c56aead4d47abbc1a92c7f98b1769a7d44a2dd 100644 --- a/lib/gat/pool/session/pool.go +++ b/lib/gat/pool/session/pool.go @@ -6,6 +6,7 @@ import ( "gfx.cafe/gfx/pggat/lib/gat" "gfx.cafe/gfx/pggat/lib/gat/protocol" "runtime" + "sync" "sync/atomic" ) @@ -19,6 +20,8 @@ type Pool struct { assigned map[gat.ClientID]gat.Connection servers chan gat.Connection + + mu sync.Mutex } func New(database gat.Database, dialer gat.Dialer, conf *config.Pool, user *config.User) *Pool { @@ -52,6 +55,8 @@ func (p *Pool) returnConnection(c gat.Connection) { } func (p *Pool) getOrAssign(client gat.Client) gat.Connection { + p.mu.Lock() + defer p.mu.Unlock() cid := client.GetId() c, ok := p.assigned[cid] if !ok { @@ -71,6 +76,8 @@ func (p *Pool) EnsureConfig(c *config.Pool) { } func (p *Pool) OnDisconnect(client gat.Client) { + p.mu.Lock() + defer p.mu.Unlock() cid := client.GetId() c, ok := p.assigned[cid] if !ok {