This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
230 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright © 2017 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package deduplicate | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"sync" | ||
|
||
"github.com/TheThingsNetwork/gateway-connector-bridge/middleware" | ||
"github.com/TheThingsNetwork/gateway-connector-bridge/types" | ||
"github.com/TheThingsNetwork/go-utils/log" | ||
) | ||
|
||
// NewDeduplicate returns a middleware that deduplicates duplicate uplink messages received from broken gateways | ||
func NewDeduplicate() *Deduplicate { | ||
return &Deduplicate{ | ||
log: log.Get(), | ||
lastMessage: make(map[string]*types.UplinkMessage), | ||
} | ||
} | ||
|
||
// Deduplicate middleware | ||
type Deduplicate struct { | ||
log log.Interface | ||
mu sync.RWMutex | ||
lastMessage map[string]*types.UplinkMessage | ||
} | ||
|
||
// HandleDisconnect cleans up | ||
func (d *Deduplicate) HandleDisconnect(ctx middleware.Context, msg *types.DisconnectMessage) error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
delete(d.lastMessage, msg.GatewayID) | ||
return nil | ||
} | ||
|
||
// ErrDuplicateMessage is returned when an uplink message is received multiple times | ||
var ErrDuplicateMessage = errors.New("deduplicate: already handled this message") | ||
|
||
// HandleUplink blocks duplicate messages | ||
func (d *Deduplicate) HandleUplink(_ middleware.Context, msg *types.UplinkMessage) error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
if lastMessage, ok := d.lastMessage[msg.GatewayID]; ok { | ||
if bytes.Equal(msg.Message.Payload, lastMessage.Message.Payload) && // length check on slice is fast | ||
msg.Message.GetGatewayMetadata().GetTimestamp() == lastMessage.Message.GetGatewayMetadata().GetTimestamp() { | ||
return ErrDuplicateMessage | ||
} | ||
} | ||
d.lastMessage[msg.GatewayID] = msg | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright © 2017 The Things Network | ||
// Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
|
||
package deduplicate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/TheThingsNetwork/gateway-connector-bridge/middleware" | ||
"github.com/TheThingsNetwork/gateway-connector-bridge/types" | ||
"github.com/TheThingsNetwork/ttn/api/router" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestDeduplicate(t *testing.T) { | ||
Convey("Given a new Deduplicate", t, func(c C) { | ||
i := NewDeduplicate() | ||
|
||
up := &types.UplinkMessage{GatewayID: "test", Message: &router.UplinkMessage{ | ||
Payload: []byte{1, 2, 3, 4}, | ||
}} | ||
upDup := &types.UplinkMessage{GatewayID: "test", Message: &router.UplinkMessage{ | ||
Payload: []byte{1, 2, 3, 4}, | ||
}} | ||
nextUp := &types.UplinkMessage{GatewayID: "test", Message: &router.UplinkMessage{ | ||
Payload: []byte{1, 2, 3, 4, 5}, | ||
}} | ||
|
||
Convey("When sending an UplinkMessage", func() { | ||
Reset(func() { | ||
i.HandleDisconnect(middleware.NewContext(), &types.DisconnectMessage{GatewayID: "test"}) | ||
}) | ||
err := i.HandleUplink(middleware.NewContext(), up) | ||
Convey("There should be no error", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
Convey("When sending a duplicate of that UplinkMessage", func() { | ||
err := i.HandleUplink(middleware.NewContext(), upDup) | ||
Convey("There should be an error", func() { | ||
So(err, ShouldEqual, ErrDuplicateMessage) | ||
}) | ||
}) | ||
Convey("When sending another UplinkMessage", func() { | ||
err := i.HandleUplink(middleware.NewContext(), nextUp) | ||
Convey("There should be no error", func() { | ||
So(err, ShouldBeNil) | ||
}) | ||
}) | ||
|
||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.