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, )
| 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. |
| 727 | func 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 |