1
- package database
1
+ package pubsub
2
2
3
3
import (
4
4
"context"
@@ -48,7 +48,7 @@ type msgOrErr struct {
48
48
type msgQueue struct {
49
49
ctx context.Context
50
50
cond * sync.Cond
51
- q [PubsubBufferSize ]msgOrErr
51
+ q [BufferSize ]msgOrErr
52
52
front int
53
53
size int
54
54
closed bool
@@ -82,7 +82,7 @@ func (q *msgQueue) run() {
82
82
return
83
83
}
84
84
item := q .q [q .front ]
85
- q .front = (q .front + 1 ) % PubsubBufferSize
85
+ q .front = (q .front + 1 ) % BufferSize
86
86
q .size --
87
87
q .cond .L .Unlock ()
88
88
@@ -111,20 +111,20 @@ func (q *msgQueue) enqueue(msg []byte) {
111
111
q .cond .L .Lock ()
112
112
defer q .cond .L .Unlock ()
113
113
114
- if q .size == PubsubBufferSize {
114
+ if q .size == BufferSize {
115
115
// queue is full, so we're going to drop the msg we got called with.
116
116
// We also need to record that messages are being dropped, which we
117
117
// do at the last message in the queue. This potentially makes us
118
118
// lose 2 messages instead of one, but it's more important at this
119
119
// point to warn the subscriber that they're losing messages so they
120
120
// can do something about it.
121
- back := (q .front + PubsubBufferSize - 1 ) % PubsubBufferSize
121
+ back := (q .front + BufferSize - 1 ) % BufferSize
122
122
q .q [back ].msg = nil
123
123
q .q [back ].err = ErrDroppedMessages
124
124
return
125
125
}
126
126
// queue is not full, insert the message
127
- next := (q .front + q .size ) % PubsubBufferSize
127
+ next := (q .front + q .size ) % BufferSize
128
128
q .q [next ].msg = msg
129
129
q .q [next ].err = nil
130
130
q .size ++
@@ -143,17 +143,17 @@ func (q *msgQueue) dropped() {
143
143
q .cond .L .Lock ()
144
144
defer q .cond .L .Unlock ()
145
145
146
- if q .size == PubsubBufferSize {
146
+ if q .size == BufferSize {
147
147
// queue is full, but we need to record that messages are being dropped,
148
148
// which we do at the last message in the queue. This potentially drops
149
149
// another message, but it's more important for the subscriber to know.
150
- back := (q .front + PubsubBufferSize - 1 ) % PubsubBufferSize
150
+ back := (q .front + BufferSize - 1 ) % BufferSize
151
151
q .q [back ].msg = nil
152
152
q .q [back ].err = ErrDroppedMessages
153
153
return
154
154
}
155
155
// queue is not full, insert the error
156
- next := (q .front + q .size ) % PubsubBufferSize
156
+ next := (q .front + q .size ) % BufferSize
157
157
q .q [next ].msg = nil
158
158
q .q [next ].err = ErrDroppedMessages
159
159
q .size ++
@@ -171,9 +171,9 @@ type pgPubsub struct {
171
171
queues map [string ]map [uuid.UUID ]* msgQueue
172
172
}
173
173
174
- // PubsubBufferSize is the maximum number of unhandled messages we will buffer
174
+ // BufferSize is the maximum number of unhandled messages we will buffer
175
175
// for a subscriber before dropping messages.
176
- const PubsubBufferSize = 2048
176
+ const BufferSize = 2048
177
177
178
178
// Subscribe calls the listener when an event matching the name is received.
179
179
func (p * pgPubsub ) Subscribe (event string , listener Listener ) (cancel func (), err error ) {
@@ -295,8 +295,8 @@ func (p *pgPubsub) recordReconnect() {
295
295
}
296
296
}
297
297
298
- // NewPubsub creates a new Pubsub implementation using a PostgreSQL connection.
299
- func NewPubsub (ctx context.Context , database * sql.DB , connectURL string ) (Pubsub , error ) {
298
+ // New creates a new Pubsub implementation using a PostgreSQL connection.
299
+ func New (ctx context.Context , database * sql.DB , connectURL string ) (Pubsub , error ) {
300
300
// Creates a new listener using pq.
301
301
errCh := make (chan error )
302
302
listener := pq .NewListener (connectURL , time .Second , time .Minute , func (_ pq.ListenerEventType , err error ) {
0 commit comments