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

feat(pubsub): add AckWithResult and NackWithResult to message #6201

Merged
merged 12 commits into from
Jun 29, 2022
Merged
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
6 changes: 3 additions & 3 deletions pubsub/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cloud.google.com/go/pubsub
go 1.15

require (
cloud.google.com/go v0.102.0
cloud.google.com/go v0.102.1-0.20220615234935-19a5d218ec4d
cloud.google.com/go/iam v0.3.0
cloud.google.com/go/kms v1.4.0
github.com/golang/protobuf v1.5.2
Expand All @@ -13,8 +13,8 @@ require (
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
golang.org/x/time v0.0.0-20220609170525-579cf78fd858
google.golang.org/api v0.83.0
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8
google.golang.org/api v0.84.0
google.golang.org/genproto v0.0.0-20220615141314-f1464d18c36b
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
)
25 changes: 12 additions & 13 deletions pubsub/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Ud
cloud.google.com/go v0.99.0/go.mod h1:w0Xx2nLzqWJPuozYQX+hFfCSI8WioryfRDzkoI/Y2ZA=
cloud.google.com/go v0.100.1/go.mod h1:fs4QogzfH5n2pBXBP9vRiU+eCny7lD2vmFZy79Iuw1U=
cloud.google.com/go v0.100.2/go.mod h1:4Xra9TjzAeYHrl5+oeLlzbM2k3mjVhZh4UqTZ//w99A=
cloud.google.com/go v0.102.0 h1:DAq3r8y4mDgyB/ZPJ9v/5VJNqjgJAxTn6ZYLlUywOu8=
cloud.google.com/go v0.102.0/go.mod h1:oWcCzKlqJ5zgHQt9YsaeTY9KzIvjyy0ArmiBUgpQ+nc=
cloud.google.com/go v0.102.1-0.20220615234935-19a5d218ec4d h1:29ZuPdUkhuJw9mwHi6zVYkJRHq956wJ5G9OQUbWl5Ew=
cloud.google.com/go v0.102.1-0.20220615234935-19a5d218ec4d/go.mod h1:mqs3bFXrt/gPc6aOZpchX8DEdQhuJluA/7LZNutd2Nc=
cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o=
cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE=
cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc=
Expand Down Expand Up @@ -165,6 +165,8 @@ github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa h1:7MYGT2XEMam7Mtzv1yDUYXANedWvwk3HKkR3MyGowy8=
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
Expand Down Expand Up @@ -310,7 +312,6 @@ golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb h1:8tDJ3aechhddbdPAxpycgXHJRMLpk/Ab+aa4OgdN5/g=
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb/go.mod h1:jaDAt6Dkxork7LmZnYtzbRWj0W47D86a3TGe0YHBvmE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -381,9 +382,9 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d h1:Zu/JngovGLVi6t2J3nmAf3AoTDwuzw85YZ3b9o4yU7s=
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -457,7 +458,7 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220411194840-2f41105eb62f/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down Expand Up @@ -495,9 +496,8 @@ google.golang.org/api v0.71.0/go.mod h1:4PyU6e6JogV1f9eA4voyrTY2batOLdgZ5qZ5HOCc
google.golang.org/api v0.74.0/go.mod h1:ZpfMZOVRMywNyvJFeqL9HRWBgAuRfSjJFpe9QtRRyDs=
google.golang.org/api v0.75.0/go.mod h1:pU9QmyHLnzlpar1Mjt4IbapUCy8J+6HD6GeELN69ljA=
google.golang.org/api v0.78.0/go.mod h1:1Sg78yoMLOhlQTeF+ARBoytAcH1NNyyl390YMy6rKmw=
google.golang.org/api v0.80.0/go.mod h1:xY3nI94gbvBrE0J6NHXhxOmW97HG7Khjkku6AFB3Hyg=
google.golang.org/api v0.83.0 h1:pMvST+6v+46Gabac4zlJlalxZjCeRcepwg2EdBU+nCc=
google.golang.org/api v0.83.0/go.mod h1:CNywQoj/AfhTw26ZWAa6LwOv+6WFxHmeLPZq2uncLZk=
google.golang.org/api v0.84.0 h1:NMB9J4cCxs9xEm+1Z9QiO3eFvn7EnQj3Eo3hN6ugVlg=
google.golang.org/api v0.84.0/go.mod h1:NTsGnUFJMYROtiquksZHBWtHfeMC7iYthki7Eq3pa8o=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down Expand Up @@ -581,9 +581,9 @@ google.golang.org/genproto v0.0.0-20220421151946-72621c1f0bd3/go.mod h1:8w6bsBMX
google.golang.org/genproto v0.0.0-20220429170224-98d788798c3e/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20220518221133-4f43b3371335/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20220523171625-347a074981d8/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8 h1:qRu95HZ148xXw+XeZ3dvqe85PxH4X8+jIo0iRPKcEnM=
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8/go.mod h1:yKyY4AMRwFiC8yMMNaMi+RkCnjZJt9LoWuvhXjMs+To=
google.golang.org/genproto v0.0.0-20220608133413-ed9918b62aac/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA=
google.golang.org/genproto v0.0.0-20220615141314-f1464d18c36b h1:2LXbOcxY7BehyA9yu5hxYzaY67bLaJQhBX9O1zxxVis=
google.golang.org/genproto v0.0.0-20220615141314-f1464d18c36b/go.mod h1:KEWEmljWE5zPzLBa/oHl6DaEt9LmfH6WtH1OHIvleBA=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down Expand Up @@ -613,7 +613,6 @@ google.golang.org/grpc v1.40.1/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9K
google.golang.org/grpc v1.44.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ=
google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.46.2/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc v1.47.0 h1:9n77onPX5F3qfFCqjy9dhn8PbNQsIKeVU04J9G7umt8=
google.golang.org/grpc v1.47.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
Expand Down
65 changes: 44 additions & 21 deletions pubsub/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package pubsub

import (
"context"
"errors"
"io"
"strings"
"sync"
"time"

ipubsub "cloud.google.com/go/internal/pubsub"
vkit "cloud.google.com/go/pubsub/apiv1"
"cloud.google.com/go/pubsub/internal/distribution"
gax "github.com/googleapis/gax-go/v2"
Expand Down Expand Up @@ -69,10 +71,16 @@ type messageIterator struct {
// to update ack deadlines (via modack), we'll consult this table and only include IDs
// that are not beyond their deadline.
keepAliveDeadlines map[string]time.Time
pendingAcks map[string]bool
pendingNacks map[string]bool
pendingModAcks map[string]bool // ack IDs whose ack deadline is to be modified
err error // error from stream failure
pendingAcks map[string]*AckResult
pendingNacks map[string]*AckResult
// ack IDs whose ack deadline is to be modified
// This technically does not need to be an AckResult, since it is just a set,
// but allows reuse of iterator.sendAckIDRPC.
pendingModAcks map[string]*AckResult
// This stores pending AckResults for cleaner shutdown when sub.Receive's ctx is cancelled.
// If exactly once delivery is not enabled, this map should not be populated.
pendingAckResults map[string]*AckResult
err error // error from stream failure

eoMu sync.RWMutex
enableExactlyOnceDelivery bool
Expand Down Expand Up @@ -119,9 +127,9 @@ func newMessageIterator(subc *vkit.SubscriberClient, subName string, po *pullOpt
drained: make(chan struct{}),
ackTimeDist: distribution.New(int(maxDurationPerLeaseExtension/time.Second) + 1),
keepAliveDeadlines: map[string]time.Time{},
pendingAcks: map[string]bool{},
pendingNacks: map[string]bool{},
pendingModAcks: map[string]bool{},
pendingAcks: map[string]*AckResult{},
pendingNacks: map[string]*AckResult{},
pendingModAcks: map[string]*AckResult{},
}
it.wg.Add(1)
go it.sender()
Expand Down Expand Up @@ -170,15 +178,16 @@ func (it *messageIterator) addToDistribution(receiveTime time.Time) {
}

// Called when a message is acked/nacked.
func (it *messageIterator) done(ackID string, ack bool, receiveTime time.Time) {
func (it *messageIterator) done(ackID string, ack bool, r *AckResult, receiveTime time.Time) {
it.addToDistribution(receiveTime)
it.mu.Lock()
defer it.mu.Unlock()
delete(it.keepAliveDeadlines, ackID)
delete(it.pendingAckResults, ackID)
if ack {
it.pendingAcks[ackID] = true
it.pendingAcks[ackID] = r
} else {
it.pendingNacks[ackID] = true
it.pendingNacks[ackID] = r
}
it.checkDrained()
}
Expand Down Expand Up @@ -235,16 +244,29 @@ func (it *messageIterator) receive(maxToPull int32) ([]*Message, error) {
// We received some messages. Remember them so we can keep them alive. Also,
// do a receipt mod-ack when streaming.
maxExt := time.Now().Add(it.po.maxExtension)
ackIDs := map[string]bool{}
ackIDs := map[string]*AckResult{}
it.mu.Lock()
for _, m := range msgs {
ackID := msgAckID(m)
addRecv(m.ID, ackID, now)
it.keepAliveDeadlines[ackID] = maxExt
// Don't change the mod-ack if the message is going to be nacked. This is
// possible if there are retries.
if !it.pendingNacks[ackID] {
ackIDs[ackID] = true
if _, ok := it.pendingNacks[ackID]; !ok {
// Don't use the message's AckResult here.
// These ids are used for modacks which require an empty AckResult.
// Calling m.AckWithResult() (or NackWithResult) prematurely locks
// the message's ack/nack status.
ackIDs[ackID] = &ipubsub.AckResult{}
}
// If exactly once is enabled, keep track of all pending AckResults
// so we can cleanly close them all at shutdown.
if it.enableExactlyOnceDelivery {
ackh, ok := ipubsub.MessageAckHandler(m).(*psAckHandler)
if !ok {
it.fail(errors.New("failed to assert type as psAckHandler"))
}
it.pendingAckResults[ackID] = ackh.ackResult
}
}
deadline := it.ackDeadline()
Expand Down Expand Up @@ -354,18 +376,18 @@ func (it *messageIterator) sender() {
sendPing = !it.po.synchronous
}
// Lock is held here.
var acks, nacks, modAcks map[string]bool
var acks, nacks, modAcks map[string]*AckResult
if sendAcks {
acks = it.pendingAcks
it.pendingAcks = map[string]bool{}
it.pendingAcks = map[string]*AckResult{}
}
if sendNacks {
nacks = it.pendingNacks
it.pendingNacks = map[string]bool{}
it.pendingNacks = map[string]*AckResult{}
}
if sendModAcks {
modAcks = it.pendingModAcks
it.pendingModAcks = map[string]bool{}
it.pendingModAcks = map[string]*AckResult{}
}
it.mu.Unlock()
// Make Ack and ModAck RPCs.
Expand Down Expand Up @@ -406,13 +428,14 @@ func (it *messageIterator) handleKeepAlives() {
delete(it.keepAliveDeadlines, id)
} else {
// This will not conflict with a nack, because nacking removes the ID from keepAliveDeadlines.
it.pendingModAcks[id] = true
// Use an empty AckResult here since we don't propagate ModAcks back to the user.
it.pendingModAcks[id] = &ipubsub.AckResult{}
}
}
it.checkDrained()
}

func (it *messageIterator) sendAck(m map[string]bool) bool {
func (it *messageIterator) sendAck(m map[string]*AckResult) bool {
// Account for the Subscription field.
overhead := calcFieldSizeString(it.subName)
return it.sendAckIDRPC(m, maxPayload-overhead, func(ids []string) error {
Expand Down Expand Up @@ -460,7 +483,7 @@ func (it *messageIterator) sendAck(m map[string]bool) bool {
// on the time it takes to process messages. The percentile chosen is the 99%th
// percentile in order to capture the highest amount of time necessary without
// considering 1% outliers.
func (it *messageIterator) sendModAck(m map[string]bool, deadline time.Duration) bool {
func (it *messageIterator) sendModAck(m map[string]*AckResult, deadline time.Duration) bool {
deadlineSec := int32(deadline / time.Second)
// Account for the Subscription and AckDeadlineSeconds fields.
overhead := calcFieldSizeString(it.subName) + calcFieldSizeInt(int(deadlineSec))
Expand Down Expand Up @@ -517,7 +540,7 @@ func (it *messageIterator) sendModAck(m map[string]bool, deadline time.Duration)
})
}

func (it *messageIterator) sendAckIDRPC(ackIDSet map[string]bool, maxSize int, call func([]string) error) bool {
func (it *messageIterator) sendAckIDRPC(ackIDSet map[string]*AckResult, maxSize int, call func([]string) error) bool {
ackIDs := make([]string, 0, len(ackIDSet))
for k := range ackIDSet {
ackIDs = append(ackIDs, k)
Expand Down
78 changes: 74 additions & 4 deletions pubsub/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,29 @@ import (
// fails, the Message will be redelivered. Nack indicates that the client will
// not or cannot process a Message. Nack will result in the Message being
// redelivered more quickly than if it were allowed to expire.
//
// If using exactly once delivery, you should call Message.AckWithResult and
// Message.NackWithResult instead. These methods will return an AckResult,
// which you should wait on to obtain the status of the Ack/Nack to ensure
// these were properly processed by the server. If not,
type Message = ipubsub.Message

// msgAckHandler performs a safe cast of the message's ack handler to psAckHandler.
func msgAckHandler(m *Message) (*psAckHandler, bool) {
func msgAckHandler(m *Message, eod bool) (*psAckHandler, bool) {
ackh, ok := ipubsub.MessageAckHandler(m).(*psAckHandler)
ackh.exactlyOnceDelivery = eod
return ackh, ok
}

func msgAckID(m *Message) string {
if ackh, ok := msgAckHandler(m); ok {
if ackh, ok := msgAckHandler(m, false); ok {
return ackh.ackID
}
return ""
}

// The done method of the iterator that created a Message.
type iterDoneFunc func(string, bool, time.Time)
type iterDoneFunc func(string, bool, *AckResult, time.Time)

func convertMessages(rms []*pb.ReceivedMessage, receiveTime time.Time, doneFunc iterDoneFunc) ([]*Message, error) {
msgs := make([]*Message, 0, len(rms))
Expand Down Expand Up @@ -87,9 +93,36 @@ func toMessage(resp *pb.ReceivedMessage, receiveTime time.Time, doneFunc iterDon
msg.OrderingKey = resp.Message.OrderingKey
ackh.receiveTime = receiveTime
ackh.doneFunc = doneFunc
ackh.ackResult = ipubsub.NewAckResult()
return msg, nil
}

// AckResult holds the result from a call to Ack or Nack.
//
// Call Get to obtain the result of the Ack/NackWithResult call. Example:
// // Get blocks until Ack/NackWithResult completes or ctx is done.
// ackStatus, err := r.Get(ctx)
// if err != nil {
// // TODO: Handle error.
// }
type AckResult = ipubsub.AckResult

// AcknowledgeStatus represents the status of an Ack or Nack request.
type AcknowledgeStatus = ipubsub.AcknowledgeStatus

const (
// AcknowledgeStatusSuccess indicates the request was a success.
pradn marked this conversation as resolved.
Show resolved Hide resolved
AcknowledgeStatusSuccess AcknowledgeStatus = iota
// AcknowledgeStatusPermissionDenied indicates the caller does not have sufficient permissions.
AcknowledgeStatusPermissionDenied
// AcknowledgeStatusFailedPrecondition indicates the request encountered a FailedPrecondition error.
AcknowledgeStatusFailedPrecondition
// AcknowledgeStatusInvalidAckID indicates one or more of the ack IDs sent were invalid.
AcknowledgeStatusInvalidAckID
// AcknowledgeStatusOther indicates another unknown error was returned.
AcknowledgeStatusOther
)

// psAckHandler handles ack/nack for the pubsub package.
type psAckHandler struct {
// ackID is the identifier to acknowledge this message.
Expand All @@ -102,6 +135,14 @@ type psAckHandler struct {

// The done method of the iterator that created this Message.
doneFunc iterDoneFunc

// the ack result that will be returned for this ack handler
// if AckWithResult or NackWithResult is called.
ackResult *AckResult

// exactlyOnceDelivery determines if the message needs to be delivered
// exactly once.
exactlyOnceDelivery bool
}

func (ah *psAckHandler) OnAck() {
Expand All @@ -112,12 +153,41 @@ func (ah *psAckHandler) OnNack() {
ah.done(false)
}

func (ah *psAckHandler) OnAckWithResult() *AckResult {
if !ah.exactlyOnceDelivery {
return newSuccessAckResult()
}
// call done with true to indicate ack.
ah.done(true)
return ah.ackResult
}

func (ah *psAckHandler) OnNackWithResult() *AckResult {
if !ah.exactlyOnceDelivery {
return newSuccessAckResult()
}
// call done with false to indicate nack.
ah.done(false)
pradn marked this conversation as resolved.
Show resolved Hide resolved
return ah.ackResult
}

func (ah *psAckHandler) AckResult() *AckResult {
return ah.ackResult
}

func (ah *psAckHandler) done(ack bool) {
if ah.calledDone {
return
}
ah.calledDone = true
if ah.doneFunc != nil {
ah.doneFunc(ah.ackID, ack, ah.receiveTime)
ah.doneFunc(ah.ackID, ack, ah.ackResult, ah.receiveTime)
}
}

// newSuccessAckResult returns an AckResult that resolves to success immediately.
func newSuccessAckResult() *AckResult {
ar := ipubsub.NewAckResult()
ipubsub.SetAckResult(ar, ipubsub.AckResponseSuccess, nil)
return ar
}
Loading