本文整理汇总了Golang中github.com/lib/pq.NewListener函数的典型用法代码示例。如果您正苦于以下问题:Golang NewListener函数的具体用法?Golang NewListener怎么用?Golang NewListener使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewListener函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: initSubscribers
// initSubscribers performs one-time initialization of the Postgres listener and
// goroutine for event delivery, termination and subscription management.
func (n *Notifier) initSubscribers(dbUrl string) error {
n.l = pq.NewListener(dbUrl, 20*time.Millisecond, time.Hour, nil)
go func() {
for {
select {
case <-n.terminated:
return
case <-n.exit:
n.l.UnlistenAll()
n.l.Close()
n.db.Close()
for _, localL := range n.subscribers {
close(localL)
}
close(n.terminated)
case cmd := <-n.ch:
cmd.fun()
close(cmd.ack)
case pgn := <-n.l.Notify:
if pgn != nil {
localN, err := n.makeNotification(pgn)
if err != nil {
log.Printf("Error parsing inbound notification %v: %v", pgn, err)
} else {
for _, sub := range n.subscribers {
sub <- localN
}
}
}
}
}
}()
return nil
}
示例2: waitForNotification
func waitForNotification(dbcluter DBCluster, parition string) {
conninfo := fmt.Sprintf("postgres://%s:%[email protected]%s/%s?sslmode=require", "read", dbcluter.Password, dbcluter.Parition[parition].Write.IP, "regentmarkets")
listener := pq.NewListener(conninfo, 5*time.Second, 10*time.Second, nil)
db, _ := sql.Open("postgres", conninfo)
err := db.Ping()
if err != nil {
log.Fatal(err)
}
err = listener.Listen("transactions_watcher")
checkErr(err)
fmt.Println("Listing to", parition)
var redisdb redis.Conn
var notification *pq.Notification
for {
select {
case notification = <-listener.Notify:
if notification != nil {
redisconn(&redisdb)
publish(redisdb, regexp.MustCompile(",").Split(notification.Extra, -1))
}
case <-time.After(60 * time.Second):
fmt.Println("no notifications for 60 seconds...")
}
}
}
示例3: run
func (cmd *activityCmd) run(ctx scope.Context, args []string) error {
// Get cluster in order to load config.
_, err := getCluster(ctx)
if err != nil {
return fmt.Errorf("cluster error: %s", err)
}
listener := pq.NewListener(backend.Config.DB.DSN, 200*time.Millisecond, 5*time.Second, nil)
if err := listener.Listen("broadcast"); err != nil {
return fmt.Errorf("pq listen error: %s", err)
}
defer func() {
ctx.Cancel()
ctx.WaitGroup().Wait()
}()
// Start metrics server.
ctx.WaitGroup().Add(1)
go activity.Serve(ctx, cmd.addr)
// Start scanner.
ctx.WaitGroup().Add(1)
activity.ScanLoop(ctx, listener)
return nil
}
示例4: main
func main() {
conninfo := "dbname=gotraining user=root password=root port=7705 sslmode=disable"
_, err := sql.Open("postgres", conninfo)
if err != nil {
panic(err)
}
reportProblem := func(ev pq.ListenerEventType, err error) {
if err != nil {
fmt.Println(err.Error())
}
}
listener := pq.NewListener(conninfo, 10*time.Second, time.Minute, reportProblem)
err = listener.Listen("events")
if err != nil {
panic(err)
}
fmt.Println("Start monitoring PostgreSQL...")
for {
waitForNotification(listener)
}
}
示例5: Init
func Init() {
var err error
db, err = sql.Open("postgres", *dataSource)
if err != nil {
debug.Fatal(err)
}
create := func(name, content string) {
if err != nil {
debug.Fatal(err)
}
err = createTable(name, content)
}
// primary tables
create("user_", createUserSQL)
create("project", createProjectSQL)
create("task", createTaskSQL)
create("milestone", createMilestoneSQL)
create("member", createMemberSQL)
create("worker", createWorkerSQL)
create("friend", createFriendSQL)
create("chat", createChatSQL)
// secondary tables
create("featured_project", createFeaturedProjectSQL)
create("user_tag", createUserTagSQL)
create("task_tag", createTaskTagSQL)
create("tag", createTagSQL)
// setup listener
if *listenerEnabled {
listener = pq.NewListener(*dataSource, 1*time.Second, time.Minute, func(ev pq.ListenerEventType, err error) {
if err != nil {
log.Fatal(err)
}
})
if err := listener.Listen("chat"); err != nil {
log.Fatal(err)
}
go func() {
for {
select {
case notification := <-listener.Notify:
if Notify != nil {
Notify(notification.Channel, notification.Extra)
}
}
}
}()
} else {
debug.Warn("PostgreSQL listener is disabled")
}
}
示例6: listenMessages
func listenMessages() {
revel.TRACE.Println("Setting up db listening")
var Spec, _ = revel.Config.String("db.spec")
var listener = pq.NewListener(Spec, 10*time.Second, time.Minute, listenerEventsHandler)
var err = listener.Listen("new_message")
if err != nil {
panic(err)
}
waitForNotification(listener)
}
示例7: ensureListener
func (repo *postgresRepository) ensureListener() {
repo.m.Lock()
defer repo.m.Unlock()
if repo.listener == nil {
repo.listener = pq.NewListener(repo.connectionString, time.Second, time.Minute, nil)
kit.SafeGo(repo.listenDaemon)
}
}
示例8: run
func run(config Config) {
listener := pq.NewListener(config.PostgresURL, 10*time.Second, time.Minute, errorReporter)
err := listener.Listen("urlwork")
if err != nil {
log.Fatal(err)
}
rabbitchannel := make(chan string, 100)
go func() {
cfg := new(tls.Config)
cfg.InsecureSkipVerify = true
conn, err := amqp.DialTLS(config.RabbitMQURL, cfg)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
log.Fatal(err)
}
defer ch.Close()
for {
payload := <-rabbitchannel
log.Println(payload)
err := ch.Publish("urlwork", "todo", false, false, amqp.Publishing{
ContentType: "text/plain",
Body: []byte(payload),
})
if err != nil {
log.Fatal(err)
}
}
}()
for {
select {
case notification := <-listener.Notify:
rabbitchannel <- notification.Extra
case <-time.After(90 * time.Second):
go func() {
err := listener.Ping()
if err != nil {
log.Fatal(err)
}
}()
}
}
}
示例9: newPgListener
// newPgListener creates and returns the pglistener from the pq package.
func newPgListener(pgconninfo string) (*pq.Listener, error) {
// create a callback function to monitor connection state changes
pgEventCallback := func(ev pq.ListenerEventType, err error) {
if err != nil {
fmt.Println("pgbroadcast: ", err.Error())
}
}
// create the listener
l := pq.NewListener(pgconninfo, 10*time.Second, time.Minute, pgEventCallback)
return l, nil
}
示例10: run
func run(config Config) {
purl := fmt.Sprintf("user=%s password=%s host=%s port=%s dbname=%s sslmode=%s", config.DbUser, config.DbPassword, config.DbHost, config.DbPort, config.DbName, config.SslMode)
listener := pq.NewListener(purl, 10*time.Second, time.Minute, errorReporter)
err := listener.Listen("usertrigger")
if err != nil {
log.Fatal(err)
}
rabbitchannel := make(chan string, 100)
//Code for STOMP
go func() {
rabbitHost := fmt.Sprintf("%s:%s", config.RabbitHost, config.RabbitPort)
conn, err := stomp.Dial("tcp", rabbitHost,
stomp.ConnOpt.Login(config.RabbitUser, config.RabbitPassword),
stomp.ConnOpt.AcceptVersion(stomp.V11),
stomp.ConnOpt.AcceptVersion(stomp.V12),
stomp.ConnOpt.Host(config.RabbitVHost),
stomp.ConnOpt.Header("nonce", "B256B26D320A"))
if err != nil {
log.Fatal(err)
}
defer conn.Disconnect()
for {
payload := <-rabbitchannel
log.Println(payload)
err = conn.Send(config.RabbitQueue, "text/plain", []byte(payload))
if err != nil {
log.Fatal(err)
}
}
}()
for {
select {
case notification := <-listener.Notify:
rabbitchannel <- notification.Extra
case <-time.After(90 * time.Second):
go func() {
err := listener.Ping()
if err != nil {
log.Fatal(err)
}
}()
}
}
}
示例11: background
func (jql *jobQueueListener) background(wg *sync.WaitGroup) {
ctx := jql.Backend.ctx.Fork()
logger := jql.Backend.logger
defer ctx.WaitGroup().Done()
listener := pq.NewListener(jql.Backend.dsn, 200*time.Millisecond, 5*time.Second, nil)
if err := listener.Listen("job_item"); err != nil {
// TODO: manage this more nicely
panic("job listen: " + err.Error())
}
logger.Printf("job listener started")
// Signal to constructor that we're ready to handle operations.
wg.Done()
keepalive := time.NewTicker(30 * time.Second)
defer keepalive.Stop()
for {
select {
case <-ctx.Done():
return
case <-keepalive.C:
// Ping to make sure the database connection is still live.
if err := listener.Ping(); err != nil {
logger.Printf("job listener ping: %s\n", err)
jql.Backend.ctx.Terminate(fmt.Errorf("job listener ping: %s", err))
return
}
case notice := <-listener.Notify:
if notice == nil {
logger.Printf("job listener: received nil notification")
// A nil notice indicates a loss of connection.
// For now it's easier to just shut down and force job
// processor to restart.
jql.Backend.ctx.Terminate(ErrPsqlConnectionLost)
return
}
jql.m.Lock()
if c, ok := jql.cs[notice.Extra]; ok {
c.Signal()
}
jql.m.Unlock()
}
}
}
示例12: constructDB
func (cmd *ATCCommand) constructDB(logger lager.Logger) (*db.SQLDB, db.PipelineDBFactory, error) {
dbConn, err := migrations.LockDBAndMigrate(logger.Session("db.migrations"), "postgres", cmd.PostgresDataSource)
if err != nil {
return nil, nil, fmt.Errorf("failed to migrate database: %s", err)
}
listener := pq.NewListener(cmd.PostgresDataSource, time.Second, time.Minute, nil)
bus := db.NewNotificationsBus(listener, dbConn)
explainDBConn := db.Explain(logger, dbConn, clock.NewClock(), 500*time.Millisecond)
sqlDB := db.NewSQL(logger.Session("db"), explainDBConn, bus)
pipelineDBFactory := db.NewPipelineDBFactory(logger.Session("db"), explainDBConn, bus, sqlDB)
return sqlDB, pipelineDBFactory, err
}
示例13: waitForNotification
func waitForNotification(dbcluter DBCluster, parition string) {
conninfo := fmt.Sprintf("postgres://%s:%[email protected]%s/%s?sslmode=require", "read", dbcluter.Password, dbcluter.Parition[parition].Write.IP, "regentmarkets")
listener := pq.NewListener(conninfo, 5*time.Second, 10*time.Second, nil)
_ = listener.Listen("transactions_watcher")
redisdb, _ := redis.DialURL(os.Getenv("REDIS_URL"))
var notification *pq.Notification
for {
select {
case notification = <-listener.Notify:
if notification != nil {
publish(redisdb, regexp.MustCompile(",").Split(notification.Extra, -1))
}
}
}
}
示例14: Listen
// Listen executes `LISTEN channel`. Uses f to handle received notifications on chanel.
// On error logs error messages (if a logs exists)
func (db *Database) Listen(channel string, f func(payload ...string)) error {
// Create a new listener only if Listen is called for the first time
if db.listener == nil {
db.listenerCallbacks = make(map[string]func(...string))
reportProblem := func(ev pq.ListenerEventType, err error) {
if err != nil && db.logger != nil {
db.printLog(err.Error())
}
}
db.listener = pq.NewListener(db.connectionString, 10*time.Second, time.Minute, reportProblem)
if db.listener == nil {
return errors.New("Unable to create a new listener")
}
// detach event handler
go func() {
for {
select {
case notification := <-db.listener.Notify:
go db.listenerCallbacks[notification.Channel](notification.Extra)
case <-time.After(90 * time.Second):
go func() {
if db.listener.Ping() != nil {
db.printLog(fmt.Sprintf("Error checking server connection for channel %s\n", channel))
return
}
}()
}
}
}()
}
if _, alreadyIn := db.listenerCallbacks[channel]; alreadyIn {
return errors.New("Already subscribed to channel " + channel)
}
db.listenerCallbacks[channel] = f
if err := db.listener.Listen(channel); err != nil {
return err
}
return nil
}
示例15: NewEventService
// pgstr - строка для коннекта к постгресу
func NewEventService(pgstr string) (event *EventService) {
event = &EventService{Channels: map[string]Channel{}}
reportProblem := func(ev pq.ListenerEventType, err error) {
if err != nil {
event.handleErrorAll(err)
}
}
event.Listener = pq.NewListener(pgstr, 10*time.Second, time.Minute, reportProblem)
go func() {
var msg Message
for {
select {
case n := <-event.Listener.Notify:
if ch, ok := event.Channels[n.Channel]; ok {
ch.MessageCount++
err := json.Unmarshal([]byte(n.Extra), &msg)
// не распарсили сообщение
if err != nil {
ch.handleError(err)
continue
}
// распарсили и нашли клиента
if client, ok := ch.Clients[msg.Target]; ok {
go client.EventHandler(msg.Body)
}
}
continue
case <-time.After(60 * time.Second):
go func() {
err := event.Listener.Ping()
if err != nil {
event.handleErrorAll(err)
}
}()
continue
}
}
}()
return
}