You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unique_queue_redis.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package queue
  5. import (
  6. "context"
  7. "github.com/go-redis/redis/v8"
  8. )
  9. // RedisUniqueQueueType is the type for redis queue
  10. const RedisUniqueQueueType Type = "unique-redis"
  11. // RedisUniqueQueue redis queue
  12. type RedisUniqueQueue struct {
  13. *ByteFIFOUniqueQueue
  14. }
  15. // RedisUniqueQueueConfiguration is the configuration for the redis queue
  16. type RedisUniqueQueueConfiguration struct {
  17. ByteFIFOQueueConfiguration
  18. RedisUniqueByteFIFOConfiguration
  19. }
  20. // NewRedisUniqueQueue creates single redis or cluster redis queue.
  21. //
  22. // Please note that this Queue does not guarantee that a particular
  23. // task cannot be processed twice or more at the same time. Uniqueness is
  24. // only guaranteed whilst the task is waiting in the queue.
  25. func NewRedisUniqueQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
  26. configInterface, err := toConfig(RedisUniqueQueueConfiguration{}, cfg)
  27. if err != nil {
  28. return nil, err
  29. }
  30. config := configInterface.(RedisUniqueQueueConfiguration)
  31. byteFIFO, err := NewRedisUniqueByteFIFO(config.RedisUniqueByteFIFOConfiguration)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if len(byteFIFO.setName) == 0 {
  36. byteFIFO.setName = byteFIFO.queueName + "_unique"
  37. }
  38. byteFIFOQueue, err := NewByteFIFOUniqueQueue(RedisUniqueQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
  39. if err != nil {
  40. return nil, err
  41. }
  42. queue := &RedisUniqueQueue{
  43. ByteFIFOUniqueQueue: byteFIFOQueue,
  44. }
  45. queue.qid = GetManager().Add(queue, RedisUniqueQueueType, config, exemplar)
  46. return queue, nil
  47. }
  48. var _ UniqueByteFIFO = &RedisUniqueByteFIFO{}
  49. // RedisUniqueByteFIFO represents a UniqueByteFIFO formed from a redisClient
  50. type RedisUniqueByteFIFO struct {
  51. RedisByteFIFO
  52. setName string
  53. }
  54. // RedisUniqueByteFIFOConfiguration is the configuration for the RedisUniqueByteFIFO
  55. type RedisUniqueByteFIFOConfiguration struct {
  56. RedisByteFIFOConfiguration
  57. SetName string
  58. }
  59. // NewRedisUniqueByteFIFO creates a UniqueByteFIFO formed from a redisClient
  60. func NewRedisUniqueByteFIFO(config RedisUniqueByteFIFOConfiguration) (*RedisUniqueByteFIFO, error) {
  61. internal, err := NewRedisByteFIFO(config.RedisByteFIFOConfiguration)
  62. if err != nil {
  63. return nil, err
  64. }
  65. fifo := &RedisUniqueByteFIFO{
  66. RedisByteFIFO: *internal,
  67. setName: config.SetName,
  68. }
  69. return fifo, nil
  70. }
  71. // PushFunc pushes data to the end of the fifo and calls the callback if it is added
  72. func (fifo *RedisUniqueByteFIFO) PushFunc(ctx context.Context, data []byte, fn func() error) error {
  73. added, err := fifo.client.SAdd(ctx, fifo.setName, data).Result()
  74. if err != nil {
  75. return err
  76. }
  77. if added == 0 {
  78. return ErrAlreadyInQueue
  79. }
  80. if fn != nil {
  81. if err := fn(); err != nil {
  82. return err
  83. }
  84. }
  85. return fifo.client.RPush(ctx, fifo.queueName, data).Err()
  86. }
  87. // Pop pops data from the start of the fifo
  88. func (fifo *RedisUniqueByteFIFO) Pop(ctx context.Context) ([]byte, error) {
  89. data, err := fifo.client.LPop(ctx, fifo.queueName).Bytes()
  90. if err != nil && err != redis.Nil {
  91. return data, err
  92. }
  93. if len(data) == 0 {
  94. return data, nil
  95. }
  96. err = fifo.client.SRem(ctx, fifo.setName, data).Err()
  97. return data, err
  98. }
  99. // Has returns whether the fifo contains this data
  100. func (fifo *RedisUniqueByteFIFO) Has(ctx context.Context, data []byte) (bool, error) {
  101. return fifo.client.SIsMember(ctx, fifo.setName, data).Result()
  102. }
  103. func init() {
  104. queuesMap[RedisUniqueQueueType] = NewRedisUniqueQueue
  105. }