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.

queue_disk.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "code.gitea.io/gitea/modules/nosql"
  7. "gitea.com/lunny/levelqueue"
  8. )
  9. // LevelQueueType is the type for level queue
  10. const LevelQueueType Type = "level"
  11. // LevelQueueConfiguration is the configuration for a LevelQueue
  12. type LevelQueueConfiguration struct {
  13. ByteFIFOQueueConfiguration
  14. DataDir string
  15. ConnectionString string
  16. QueueName string
  17. }
  18. // LevelQueue implements a disk library queue
  19. type LevelQueue struct {
  20. *ByteFIFOQueue
  21. }
  22. // NewLevelQueue creates a ledis local queue
  23. func NewLevelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
  24. configInterface, err := toConfig(LevelQueueConfiguration{}, cfg)
  25. if err != nil {
  26. return nil, err
  27. }
  28. config := configInterface.(LevelQueueConfiguration)
  29. if len(config.ConnectionString) == 0 {
  30. config.ConnectionString = config.DataDir
  31. }
  32. byteFIFO, err := NewLevelQueueByteFIFO(config.ConnectionString, config.QueueName)
  33. if err != nil {
  34. return nil, err
  35. }
  36. byteFIFOQueue, err := NewByteFIFOQueue(LevelQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
  37. if err != nil {
  38. return nil, err
  39. }
  40. queue := &LevelQueue{
  41. ByteFIFOQueue: byteFIFOQueue,
  42. }
  43. queue.qid = GetManager().Add(queue, LevelQueueType, config, exemplar)
  44. return queue, nil
  45. }
  46. var _ ByteFIFO = &LevelQueueByteFIFO{}
  47. // LevelQueueByteFIFO represents a ByteFIFO formed from a LevelQueue
  48. type LevelQueueByteFIFO struct {
  49. internal *levelqueue.Queue
  50. connection string
  51. }
  52. // NewLevelQueueByteFIFO creates a ByteFIFO formed from a LevelQueue
  53. func NewLevelQueueByteFIFO(connection, prefix string) (*LevelQueueByteFIFO, error) {
  54. db, err := nosql.GetManager().GetLevelDB(connection)
  55. if err != nil {
  56. return nil, err
  57. }
  58. internal, err := levelqueue.NewQueue(db, []byte(prefix), false)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &LevelQueueByteFIFO{
  63. connection: connection,
  64. internal: internal,
  65. }, nil
  66. }
  67. // PushFunc will push data into the fifo
  68. func (fifo *LevelQueueByteFIFO) PushFunc(data []byte, fn func() error) error {
  69. if fn != nil {
  70. if err := fn(); err != nil {
  71. return err
  72. }
  73. }
  74. return fifo.internal.LPush(data)
  75. }
  76. // Pop pops data from the start of the fifo
  77. func (fifo *LevelQueueByteFIFO) Pop() ([]byte, error) {
  78. data, err := fifo.internal.RPop()
  79. if err != nil && err != levelqueue.ErrNotFound {
  80. return nil, err
  81. }
  82. return data, nil
  83. }
  84. // Close this fifo
  85. func (fifo *LevelQueueByteFIFO) Close() error {
  86. err := fifo.internal.Close()
  87. _ = nosql.GetManager().CloseLevelDB(fifo.connection)
  88. return err
  89. }
  90. // Len returns the length of the fifo
  91. func (fifo *LevelQueueByteFIFO) Len() int64 {
  92. return fifo.internal.Len()
  93. }
  94. func init() {
  95. queuesMap[LevelQueueType] = NewLevelQueue
  96. }