MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / DeduplicateProtos

Function DeduplicateProtos

pkg/redis/redis.go:727–749  ·  view source on GitHub ↗

DeduplicateProtos deduplicates protos using key k. It stores a lock at LockKey(k) and the list of collected protos at ListKey(k). If the number of protos exceeds limit, the messages are trimmed from the start of the list.

(
	ctx context.Context, r redis.Scripter, k string, window time.Duration, limit int, msgs ...proto.Message,
)

Source from the content-addressed store, hash-verified

725// and the list of collected protos at ListKey(k).
726// If the number of protos exceeds limit, the messages are trimmed from the start of the list.
727func DeduplicateProtos(
728 ctx context.Context, r redis.Scripter, k string, window time.Duration, limit int, msgs ...proto.Message,
729) (bool, error) {
730 args := make([]any, 0, 2+len(msgs))
731 args = append(args, milliseconds(window))
732 args = append(args, limit)
733 if n := len(msgs) - limit; n > 0 {
734 msgs = msgs[n:]
735 }
736
737 for _, msg := range msgs {
738 s, err := MarshalProto(msg)
739 if err != nil {
740 return false, err
741 }
742 args = append(args, s)
743 }
744 res, err := deduplicateProtosScript.Run(ctx, r, []string{LockKey(k), ListKey(k)}, args...).Int64()
745 if err != nil {
746 return false, ConvertError(err)
747 }
748 return res == 1, nil
749}
750
751// NOTE: Time stops in lua scripts and expired keys stay available.
752

Callers 2

TestProtoDeduplicatorFunction · 0.85

Calls 6

millisecondsFunction · 0.85
MarshalProtoFunction · 0.85
LockKeyFunction · 0.85
ListKeyFunction · 0.85
ConvertErrorFunction · 0.85
RunMethod · 0.65

Tested by 2

TestProtoDeduplicatorFunction · 0.68