Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update message ID in Send/SendTx #44

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
// Send a message to the queue.
// Note that the body is an arbitrary byte slice, so you can decide
// what kind of payload you have. You can also set a message delay.
err = q.Send(context.Background(), goqite.Message{
err = q.Send(context.Background(), &goqite.Message{
Body: []byte("yo"),
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/queue/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
// Send a message to the queue.
// Note that the body is an arbitrary byte slice, so you can decide
// what kind of payload you have. You can also set a message delay.
err = q.Send(context.Background(), goqite.Message{
err = q.Send(context.Background(), &goqite.Message{
Body: []byte("yo"),
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h3>Queue</h3>
// Send a message to the queue.
// Note that the body is an arbitrary byte slice, so you can decide
// what kind of payload you have. You can also set a message delay.
err = q.Send(context.Background(), goqite.Message{
err = q.Send(context.Background(), &goqite.Message{
Body: []byte("yo"),
})
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions goqite.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,23 @@ type Message struct {
}

// Send a Message to the queue with an optional delay.
func (q *Queue) Send(ctx context.Context, m Message) error {
// Updates the message ID of the given message.
func (q *Queue) Send(ctx context.Context, m *Message) error {
return internalsql.InTx(q.db, func(tx *sql.Tx) error {
return q.SendTx(ctx, tx, m)
})
}

// SendTx is like Send, but within an existing transaction.
func (q *Queue) SendTx(ctx context.Context, tx *sql.Tx, m Message) error {
func (q *Queue) SendTx(ctx context.Context, tx *sql.Tx, m *Message) error {
if m.Delay < 0 {
panic("delay cannot be negative")
}

timeout := time.Now().Add(m.Delay).Format(rfc3339Milli)

_, err := tx.ExecContext(ctx, `insert into goqite (queue, body, timeout) values (?, ?, ?)`, q.name, m.Body, timeout)
if err != nil {
query := `insert into goqite (queue, body, timeout) values (?, ?, ?) returning id`
if err := tx.QueryRowContext(ctx, query, q.name, m.Body, timeout).Scan(&m.ID); err != nil {
return err
}
return nil
Expand Down
23 changes: 12 additions & 11 deletions goqite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func TestQueue(t *testing.T) {
Body: []byte("yo"),
}

err = q.Send(context.Background(), *m)
err = q.Send(context.Background(), m)
is.NotError(t, err)
is.True(t, len(m.ID) > 0)

m, err = q.Receive(context.Background())
is.NotError(t, err)
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestQueue_Send(t *testing.T) {
is.Equal(t, "delay cannot be negative", r)
}()

err = q.Send(context.Background(), goqite.Message{Delay: -1})
err = q.Send(context.Background(), &goqite.Message{Delay: -1})
})
}

Expand All @@ -112,7 +113,7 @@ func TestQueue_Receive(t *testing.T) {
Delay: 2 * time.Millisecond,
}

err := q.Send(context.Background(), *m)
err := q.Send(context.Background(), m)
is.NotError(t, err)

m, err = q.Receive(context.Background())
Expand All @@ -134,7 +135,7 @@ func TestQueue_Receive(t *testing.T) {
Body: []byte("yo"),
}

err := q.Send(context.Background(), *m)
err := q.Send(context.Background(), m)
is.NotError(t, err)

m, err = q.Receive(context.Background())
Expand All @@ -154,7 +155,7 @@ func TestQueue_Receive(t *testing.T) {
Body: []byte("yo"),
}

err := q.Send(context.Background(), *m)
err := q.Send(context.Background(), m)
is.NotError(t, err)

m, err = q.Receive(context.Background())
Expand All @@ -180,7 +181,7 @@ func TestQueue_Receive(t *testing.T) {
q1 := newQ(t, goqite.NewOpts{}, "test.db")
q2 := newQ(t, goqite.NewOpts{Name: "q2"}, "test.db")

err := q1.Send(context.Background(), goqite.Message{Body: []byte("yo")})
err := q1.Send(context.Background(), &goqite.Message{Body: []byte("yo")})
is.NotError(t, err)

m, err := q2.Receive(context.Background())
Expand All @@ -197,7 +198,7 @@ func TestQueue_Extend(t *testing.T) {
Body: []byte("yo"),
}

err := q.Send(context.Background(), *m)
err := q.Send(context.Background(), m)
is.NotError(t, err)

m, err = q.Receive(context.Background())
Expand Down Expand Up @@ -228,7 +229,7 @@ func TestQueue_Extend(t *testing.T) {
Body: []byte("yo"),
}

err = q.Send(context.Background(), *m)
err = q.Send(context.Background(), m)
is.NotError(t, err)

m, err = q.Receive(context.Background())
Expand All @@ -254,7 +255,7 @@ func TestQueue_ReceiveAndWait(t *testing.T) {
t.Run("gets a message immediately if there is one", func(t *testing.T) {
q := newQ(t, goqite.NewOpts{Timeout: time.Millisecond}, ":memory:")

err := q.Send(context.Background(), goqite.Message{Body: []byte("yo")})
err := q.Send(context.Background(), &goqite.Message{Body: []byte("yo")})
is.NotError(t, err)

m, err := q.ReceiveAndWait(context.Background(), time.Millisecond)
Expand Down Expand Up @@ -290,7 +291,7 @@ func BenchmarkQueue(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := q.Send(context.Background(), goqite.Message{
err := q.Send(context.Background(), &goqite.Message{
Body: []byte("yo"),
})
is.NotError(b, err)
Expand Down Expand Up @@ -336,7 +337,7 @@ func BenchmarkQueue(b *testing.B) {

for i := 0; i < 100_000; i++ {
q := queues[rand.Intn(len(queues))]
err := q.Send(context.Background(), goqite.Message{
err := q.Send(context.Background(), &goqite.Message{
Body: []byte("yo"),
})
is.NotError(b, err)
Expand Down
4 changes: 2 additions & 2 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

type queue interface {
Send(ctx context.Context, m goqite.Message) error
Send(ctx context.Context, m *goqite.Message) error
Receive(ctx context.Context) (*goqite.Message, error)
ReceiveAndWait(ctx context.Context, interval time.Duration) (*goqite.Message, error)
Extend(ctx context.Context, id goqite.ID, delay time.Duration) error
Expand Down Expand Up @@ -103,7 +103,7 @@ func NewHandler(q queue) http.HandlerFunc {
return
}

if err := q.Send(r.Context(), req.Message); err != nil {
if err := q.Send(r.Context(), &req.Message); err != nil {
http.Error(w, "error sending message: "+err.Error(), http.StatusInternalServerError)
return
}
Expand Down
2 changes: 1 addition & 1 deletion http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type queueMock struct {
err error
}

func (q *queueMock) Send(ctx context.Context, m goqite.Message) error {
func (q *queueMock) Send(ctx context.Context, m *goqite.Message) error {
return q.err
}

Expand Down
4 changes: 2 additions & 2 deletions jobs/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func Create(ctx context.Context, q *goqite.Queue, name string, m []byte) error {
if err := gob.NewEncoder(&buf).Encode(message{Name: name, Message: m}); err != nil {
return err
}
return q.Send(ctx, goqite.Message{Body: buf.Bytes()})
return q.Send(ctx, &goqite.Message{Body: buf.Bytes()})
}

// CreateTx is like Create, but within an existing transaction.
Expand All @@ -223,7 +223,7 @@ func CreateTx(ctx context.Context, tx *sql.Tx, q *goqite.Queue, name string, m [
if err := gob.NewEncoder(&buf).Encode(message{Name: name, Message: m}); err != nil {
return err
}
return q.SendTx(ctx, tx, goqite.Message{Body: buf.Bytes()})
return q.SendTx(ctx, tx, &goqite.Message{Body: buf.Bytes()})
}

// logger matches the info level method from the slog.Logger.
Expand Down
Loading