Call a hook between dialling the printer and writing to it

The job store must commit a delivery's sending mark after the printer's
connection opens and before the first byte goes out, so that a job
interrupted mid-send is never sent again after a restart, while a dial
failure writes nothing. Send now takes a beforeWrite function for that
step. An error from it closes the connection with nothing written.

The write deadline is computed after beforeWrite returns, so a slow
synced commit does not use up printer.write_timeout. The queue passes
nil for now, so nothing changes in what is sent.

Part of stage 1c of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 12:29:52 -06:00
1 parent 66531e7314
commit 50db21190b
4 files changed
+96 -9

No files matched your search

+13 -2
View File
@@ -56,14 +56,25 @@ func (c *Client) Addr() string {
return c.addr
}
// Send writes data to the printer in one connection.
func (c *Client) Send(ctx context.Context, data []byte) error {
// Send writes data to the printer in one connection. Once the connection
// is open, and before the first byte is written, it calls beforeWrite, if
// not nil; an error from it closes the connection with nothing written and
// is returned. The write deadline starts after beforeWrite returns, so a
// slow beforeWrite does not shorten the write. ctx bounds the wait for the
// connection and the dial, and its deadline bounds the write; cancelling
// it once the connection is open does not stop the write.
func (c *Client) Send(ctx context.Context, data []byte, beforeWrite func() error) error {
conn, release, err := c.connect(ctx)
if err != nil {
return err
}
defer release()
if beforeWrite != nil {
if err := beforeWrite(); err != nil {
return err
}
}
writeDeadline, _ := c.deadline(ctx, c.writeTimeout)
if err := conn.SetWriteDeadline(writeDeadline); err != nil {
return fmt.Errorf("setting printer write deadline: %w", err)