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

net: Enhance observation management with ETag tracking #469

Merged
merged 3 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions net/observation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ type Observation[C Client] struct {
waitForResponse atomic.Bool
observationHandler *Handler[C]

private struct {
private struct { // members guarded by mutex
mutex sync.Mutex
obsSequence uint32 // guarded by mutex
lastEvent time.Time // guarded by mutex
obsSequence uint32
lastEvent time.Time
etag []byte
}
}

Expand Down Expand Up @@ -199,6 +200,12 @@ func (o *Observation[C]) Request() message.Message {
return o.req
}

func (o *Observation[C]) etag() []byte {
o.private.mutex.Lock()
defer o.private.mutex.Unlock()
return o.private.etag
}

// Cancel remove observation from server. For recreate observation use Observe.
func (o *Observation[C]) Cancel(ctx context.Context, opts ...message.Option) error {
if !o.cleanUp() {
Expand All @@ -217,6 +224,10 @@ func (o *Observation[C]) Cancel(ctx context.Context, opts ...message.Option) err
}
}
req.SetToken(o.req.Token)
etag := o.etag()
if etag != nil {
_ = req.SetETag(etag) // ignore invalid etag
}
resp, err := o.observationHandler.do(req)
if err != nil {
return err
Expand All @@ -240,6 +251,9 @@ func (o *Observation[C]) wantBeNotified(r *pool.Message) bool {
if ValidSequenceNumber(o.private.obsSequence, obsSequence, o.private.lastEvent, now) {
o.private.obsSequence = obsSequence
o.private.lastEvent = now
if etag, err := r.ETag(); err == nil {
o.private.etag = etag
Copy link
Member

@jkralik jkralik Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use copy instead of assignation -> the underlayer buffer could be reused.
on new observation preallocate buffer with 8 bytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy() copies works based on length not capacity, so if the lengths do not match I have to allocate a new slice

}
return true
}

Expand Down
23 changes: 10 additions & 13 deletions tcp/clientobserve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func TestConnObserve(t *testing.T) {
var etag []byte
var errE error
if tt.args.etag != nil {
// force unexpected etag, Cancel should still succeed but with codes.Content
// instead of codes.Valid
etag = tt.args.etag
} else {
etag, errE = message.GetETag(p)
Expand Down Expand Up @@ -141,12 +143,13 @@ func TestConnObserve(t *testing.T) {
}
f()
case 1:
if tt.args.etag != nil {
if etag, errE := r.ETag(); errE == nil && bytes.Equal(tt.args.etag, etag) {
errS := w.SetResponse(codes.Valid, message.TextPlain, nil)
require.NoError(t, errS)
return
}
p := bytes.NewReader(tt.args.payload)
etag, errE := message.GetETag(p)
require.NoError(t, errE)
if retag, errE := r.ETag(); errE == nil && bytes.Equal(etag, retag) {
errS := w.SetResponse(codes.Valid, message.TextPlain, nil)
require.NoError(t, errS)
return
}
errS := w.SetResponse(codes.Content, message.TextPlain, bytes.NewReader([]byte("close")))
require.NoError(t, errS)
Expand Down Expand Up @@ -186,13 +189,7 @@ func TestConnObserve(t *testing.T) {
require.NoError(t, err)
<-obs.done

opts := make(message.Options, 0, 1)
if tt.args.etag != nil {
buf := make([]byte, len(tt.args.etag))
opts, _, err = opts.SetBytes(buf, message.ETag, tt.args.etag)
require.NoError(t, err)
}
err = got.Cancel(ctx, opts...)
err = got.Cancel(ctx)
require.NoError(t, err)
})
}
Expand Down
Loading