Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
postgres listen/notifiy with golang and `go-pg`
package main
import (
"log"
"time"
"github.com/go-pg/pg"
)
const maxWorkers = 5
var (
limiter = make(chan int, maxWorkers)
)
func main() {
db := pg.Connect(&pg.Options{
User: "seba",
ApplicationName: "test_notify",
})
ln := db.Listen("mychan")
defer ln.Close()
ch := ln.Channel()
for notifyOutput := range ch {
limiter <- 1
go processMessage(notifyOutput.Payload)
}
}
func processMessage(message string) {
log.Printf("[ %d / %d ] added to queue: %s\n", cap(limiter), maxWorkers, message)
time.Sleep(1 * time.Second)
<-limiter
}
package main
import (
"log"
"github.com/go-pg/pg"
)
func main() {
db := pg.Connect(&pg.Options{
User: "seba",
ApplicationName: "test_notify",
})
ln := db.Listen("mychan")
defer ln.Close()
ch := ln.Channel()
for val := range ch {
processMessage(val.Payload)
}
}
func processMessage(message string) {
log.Println("received message:", message)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.