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

unique_queue_redis.go 3.4 kB

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