Coder Social home page Coder Social logo

pgxlisten's Introduction

pgxlisten

pgxlisten implements a client for the LISTEN/NOTIFY feature of PostgreSQL on top of the beautiful github.com/jackc/pgx library.

Usage

pool, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
	panic(err)
}
defer pool.Close()

listener := pgxlisten.StartListener(pool)
defer listener.Stop()

channel := listener.Listen("my_channel")
defer channel.Unlisten()

for notification := range channel.Notifications() {
	if notification.ConnectionReset {
		// handle reconnect
		continue
	}
	// process `notification.Payload`
}

Alternatives

  1. Directly using WaitForNotification.
  2. Using https://github.com/jackc/pgxlisten.

The API of this library is similar to that of https://github.com/jackc/pgxlisten. The difference is outlined below:

  1. Listener uses pgxpool instead of a connection factory function, which makes it slightly easier to instantiate Listener. Should any custom connection hooks be needed, they may be added as Listener options (this is not implemented at the moment).
  2. Handlers may be added and removed dynamically, although with a penalty of reacquiring the connection.
  3. Notifications are sent using 1-buffered channels so handlers process them concurrently. Should a handler block for a long time, other handlers of course will be blocked as well.
  4. The need for backlog processing is signaled using a field in the Notification struct. This way the same handler function can process both backlog and new messages.
  5. The goroutine responsible for dispatching notifications is spawned inside Listener, which removes the burden of managing it from the caller:
- listenerCtx, listenerCtxCancel := context.WithCancel(ctx)
- defer listenerCtxCancel()
- listenerDoneChan := make(chan struct{})
- 
- go func() {
- 	listener.Listen(listenerCtx)
- 	close(listenerDoneChan)
- }()
- 
- // do other stuff
- 
- listenerCtxCancel()
- <-listenerDoneChan

+ listener := StartListener(...)
+ defer listener.Stop()
+
+ // do other stuff

Take a look at this thread for more details.

pgxlisten's People

Contributors

rnovatorov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.