(ctx context.Context, subID string, handler func(ctx context.Context, msgID string, data []byte) error)
| 61 | } |
| 62 | |
| 63 | func (c *Client) Subscribe(ctx context.Context, subID string, |
| 64 | handler func(ctx context.Context, msgID string, data []byte) error) error { |
| 65 | sub := c.client.Subscriber(subID) |
| 66 | |
| 67 | // Receive blocks until ctx is cancelled. |
| 68 | err := sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) { |
| 69 | // Execute the worker's handler |
| 70 | workErr := handler(ctx, msg.ID, msg.Data) |
| 71 | if workErr == nil { |
| 72 | // ACK: Success |
| 73 | msg.Ack() |
| 74 | } else if errors.Is(workErr, event.ErrTransientFailure) { |
| 75 | // NACK: Retry later |
| 76 | slog.WarnContext(ctx, "transient failure, will retry", "error", workErr) |
| 77 | msg.Nack() |
| 78 | } else { |
| 79 | // ACK: Permanent failure or unknown error, do not retry |
| 80 | slog.ErrorContext(ctx, "permanent failure", "error", workErr) |
| 81 | msg.Ack() |
| 82 | } |
| 83 | }) |
| 84 | |
| 85 | if err != nil { |
| 86 | return errors.Join(ErrFailedToReceiveMessage, err) |
| 87 | } |
| 88 | |
| 89 | return nil |
| 90 | } |
nothing calls this directly
no outgoing calls
no test coverage detected