Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions frontendConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/johto/notifyutils/notifydispatcher"
"github.com/lib/pq"

"bytes"
"bufio"
"bytes"
"crypto/rand"
"errors"
"fmt"
Expand Down Expand Up @@ -59,6 +59,7 @@ type FrontendConnection struct {

stream *fbcore.MessageStream
dispatcher *notifydispatcher.NotifyDispatcher
dispatchers map[string]*notifydispatcher.NotifyDispatcher

connStatusNotifier chan struct{}
notify chan *pq.Notification
Expand Down Expand Up @@ -92,11 +93,11 @@ func RejectFrontendConnection(c net.Conn) {
_ = c.Close()
}

func (c FrontendConnection) String() string {
func (c *FrontendConnection) String() string {
return c.remoteAddr
}

func NewFrontendConnection(c net.Conn, dispatcher *notifydispatcher.NotifyDispatcher, connStatusNotifier chan struct{}) *FrontendConnection {
func NewFrontendConnection(c net.Conn, dispatchers map[string]*notifydispatcher.NotifyDispatcher, connStatusNotifier chan struct{}) *FrontendConnection {
io := &frontendConnectionIO{
c: c,
bufw: bufio.NewWriterSize(c, 128),
Expand All @@ -106,7 +107,8 @@ func NewFrontendConnection(c net.Conn, dispatcher *notifydispatcher.NotifyDispat
remoteAddr: c.RemoteAddr().String(),

stream: fbcore.NewFrontendStream(io),
dispatcher: dispatcher,
dispatcher: nil,
dispatchers: dispatchers,

connStatusNotifier: connStatusNotifier,
notify: make(chan *pq.Notification, 256),
Expand Down Expand Up @@ -172,6 +174,8 @@ func (c *FrontendConnection) auth(dbcfg VirtualDatabaseConfiguration, sm *fbprot
return authFailed("3D000", "database %q does not exist", dbname)
}

c.dispatcher = c.dispatchers[dbname]

switch authMethod {
case "trust":
return true
Expand Down
82 changes: 45 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,47 +132,55 @@ func main() {
var m sync.Mutex
var connStatusNotifier chan struct{}

listenerStateChange := func(ev pq.ListenerEventType, err error) {
switch ev {
case pq.ListenerEventConnectionAttemptFailed:
elog.Warningf("Listener: could not connect to the database: %s", err.Error())

case pq.ListenerEventDisconnected:
elog.Warningf("Listener: lost connection to the database: %s", err.Error())
m.Lock()
close(connStatusNotifier)
connStatusNotifier = nil
m.Unlock()
// make sure pq.Listener doesn't pick up any env variables
os.Clearenv()

case pq.ListenerEventReconnected,
pq.ListenerEventConnected:
elog.Logf("Listener: connected to the database")
m.Lock()
connStatusNotifier = make(chan struct{})
m.Unlock()
dispatchers := make(map[string]*notifydispatcher.NotifyDispatcher)

for _, value := range Config.Databases {
dbname := value.name

listenerStateChange := func(ev pq.ListenerEventType, err error) {
switch ev {
case pq.ListenerEventConnectionAttemptFailed:
elog.Warningf("Listener: could not connect to database %s: %s", dbname, err.Error())

case pq.ListenerEventDisconnected:
elog.Warningf("Listener: lost connection to database %s: %s", dbname, err.Error())
m.Lock()
close(connStatusNotifier)
connStatusNotifier = nil
m.Unlock()

case pq.ListenerEventReconnected,
pq.ListenerEventConnected:
elog.Logf("Listener: connected to database %s", dbname)
m.Lock()
connStatusNotifier = make(chan struct{})
m.Unlock()
}
}
}

// make sure pq.Listener doesn't pick up any env variables
os.Clearenv()
clientConnectionString := fmt.Sprintf("fallback_application_name=allas %s dbname=%s", Config.ClientConnInfo, dbname)
listener := pq.NewListener(
clientConnectionString,
250*time.Millisecond, 3*time.Second,
listenerStateChange,
)
listenerWrapper, err := newPqListenerWrapper(listener)
if err != nil {
elog.Fatalf("%s", err)
}
nd := notifydispatcher.NewNotifyDispatcher(listenerWrapper)
nd.SetBroadcastOnConnectionLoss(false)
nd.SetSlowReaderEliminationStrategy(notifydispatcher.NeglectSlowReaders)

clientConnectionString := fmt.Sprintf("fallback_application_name=allas %s", Config.ClientConnInfo)
listener := pq.NewListener(
clientConnectionString,
250*time.Millisecond, 3*time.Second,
listenerStateChange,
)
listenerWrapper, err := newPqListenerWrapper(listener)
if err != nil {
elog.Fatalf("%s", err)
}
nd := notifydispatcher.NewNotifyDispatcher(listenerWrapper)
nd.SetBroadcastOnConnectionLoss(false)
nd.SetSlowReaderEliminationStrategy(notifydispatcher.NeglectSlowReaders)
dispatchers[value.name] = nd

// We don't strictly speaking need to be pinging the server; this is a
// workaround for PostgreSQL BUG #14830.
go listenerPinger(listener)
// We don't strictly speaking need to be pinging the server; this is a
// workaround for PostgreSQL BUG #14830.
go listenerPinger(listener)
}

for {
c, err := l.Accept()
Expand All @@ -194,7 +202,7 @@ func main() {
}
m.Unlock()

newConn := NewFrontendConnection(c, nd, myConnStatusNotifier)
newConn := NewFrontendConnection(c, dispatchers, myConnStatusNotifier)
go newConn.mainLoop(Config.StartupParameters, Config.Databases)
}
}