Skip to content

Go ​

Last updated 

The Go Spooler library, github.com/spoolersh/spooler-go, is a thin client over the API. The delivery contract holds unchanged: the library adds no guarantee and hides none – no automatic ack, renew, batching, or reordering, and no retry of a result it cannot prove safe.

Installation ​

sh
go get github.com/spoolersh/spooler-go

Overview ​

One spooler.Client is shared by every goroutine, and the send, receive, ack loop looks like this:

go
c := spooler.Client{
	APIKey: os.Getenv("SPOOLER_API_KEY"),
	// The local image also needs:
	// 	Host:               "127.0.0.1:8080",
	// 	InsecureDisableTLS: true,
}
res, err := c.Send(ctx, spooler.SendRequest{
	Spool: "default",
	Queue: "jobs",
	Data:  []byte("hello, world"),
})
if err != nil {
	return err
}
log.Println("sent", res.ID)

msg, ok, err := c.Recv(ctx, spooler.RecvRequest{
	Spool: "default",
	Queue: "jobs",
})
if err != nil {
	return err
}
if !ok {
	// No message became visible.
	return nil
}

if err := handle(msg.Data); err != nil {
	err1 := c.Nack(ctx, spooler.NackRequest{
		Spool: "default",
		Lease: msg.Lease,
	})
	return errors.Join(err, err1)
}

return c.Ack(ctx, spooler.AckRequest{
	Spool: "default",
	Lease: msg.Lease,
})

A worker is this loop run forever; a pool of workers is several goroutines calling Recv on the shared client. Every error the API answers is a *spooler.Error, matched by kind with errors.Is against a sentinel – one per kind in the errors table.

Further reading ​