2020-01-07 19:23:09 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2021-05-15 22:22:26 +08:00
|
|
|
"context"
|
|
|
|
|
2020-09-28 05:09:46 +08:00
|
|
|
"code.gitea.io/gitea/modules/nosql"
|
|
|
|
|
2020-01-07 19:23:09 +08:00
|
|
|
"gitea.com/lunny/levelqueue"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LevelQueueType is the type for level queue
|
|
|
|
const LevelQueueType Type = "level"
|
|
|
|
|
|
|
|
// LevelQueueConfiguration is the configuration for a LevelQueue
|
|
|
|
type LevelQueueConfiguration struct {
|
2020-02-03 07:19:58 +08:00
|
|
|
ByteFIFOQueueConfiguration
|
2020-09-28 05:09:46 +08:00
|
|
|
DataDir string
|
|
|
|
ConnectionString string
|
|
|
|
QueueName string
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LevelQueue implements a disk library queue
|
|
|
|
type LevelQueue struct {
|
2020-02-03 07:19:58 +08:00
|
|
|
*ByteFIFOQueue
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLevelQueue creates a ledis local queue
|
|
|
|
func NewLevelQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error) {
|
|
|
|
configInterface, err := toConfig(LevelQueueConfiguration{}, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := configInterface.(LevelQueueConfiguration)
|
|
|
|
|
2020-09-28 05:09:46 +08:00
|
|
|
if len(config.ConnectionString) == 0 {
|
|
|
|
config.ConnectionString = config.DataDir
|
|
|
|
}
|
2021-05-15 22:22:26 +08:00
|
|
|
config.WaitOnEmpty = true
|
2020-09-28 05:09:46 +08:00
|
|
|
|
|
|
|
byteFIFO, err := NewLevelQueueByteFIFO(config.ConnectionString, config.QueueName)
|
2020-02-03 07:19:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
byteFIFOQueue, err := NewByteFIFOQueue(LevelQueueType, byteFIFO, handle, config.ByteFIFOQueueConfiguration, exemplar)
|
2020-01-07 19:23:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
queue := &LevelQueue{
|
2020-02-03 07:19:58 +08:00
|
|
|
ByteFIFOQueue: byteFIFOQueue,
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
2020-01-29 09:01:06 +08:00
|
|
|
queue.qid = GetManager().Add(queue, LevelQueueType, config, exemplar)
|
2020-01-07 19:23:09 +08:00
|
|
|
return queue, nil
|
|
|
|
}
|
|
|
|
|
2021-04-09 15:40:34 +08:00
|
|
|
var _ ByteFIFO = &LevelQueueByteFIFO{}
|
2020-01-07 19:23:09 +08:00
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// LevelQueueByteFIFO represents a ByteFIFO formed from a LevelQueue
|
|
|
|
type LevelQueueByteFIFO struct {
|
2020-09-28 05:09:46 +08:00
|
|
|
internal *levelqueue.Queue
|
|
|
|
connection string
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// NewLevelQueueByteFIFO creates a ByteFIFO formed from a LevelQueue
|
2020-09-28 05:09:46 +08:00
|
|
|
func NewLevelQueueByteFIFO(connection, prefix string) (*LevelQueueByteFIFO, error) {
|
|
|
|
db, err := nosql.GetManager().GetLevelDB(connection)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
internal, err := levelqueue.NewQueue(db, []byte(prefix), false)
|
2020-01-07 19:23:09 +08:00
|
|
|
if err != nil {
|
2020-02-03 07:19:58 +08:00
|
|
|
return nil, err
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
2020-02-03 07:19:58 +08:00
|
|
|
|
|
|
|
return &LevelQueueByteFIFO{
|
2020-09-28 05:09:46 +08:00
|
|
|
connection: connection,
|
|
|
|
internal: internal,
|
2020-02-03 07:19:58 +08:00
|
|
|
}, nil
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// PushFunc will push data into the fifo
|
2021-05-15 22:22:26 +08:00
|
|
|
func (fifo *LevelQueueByteFIFO) PushFunc(ctx context.Context, data []byte, fn func() error) error {
|
2020-02-03 07:19:58 +08:00
|
|
|
if fn != nil {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-29 09:01:06 +08:00
|
|
|
}
|
2020-02-03 07:19:58 +08:00
|
|
|
return fifo.internal.LPush(data)
|
2020-01-29 09:01:06 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// Pop pops data from the start of the fifo
|
2021-05-15 22:22:26 +08:00
|
|
|
func (fifo *LevelQueueByteFIFO) Pop(ctx context.Context) ([]byte, error) {
|
2020-02-03 07:19:58 +08:00
|
|
|
data, err := fifo.internal.RPop()
|
|
|
|
if err != nil && err != levelqueue.ErrNotFound {
|
|
|
|
return nil, err
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
2020-02-03 07:19:58 +08:00
|
|
|
return data, nil
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// Close this fifo
|
|
|
|
func (fifo *LevelQueueByteFIFO) Close() error {
|
2020-09-28 05:09:46 +08:00
|
|
|
err := fifo.internal.Close()
|
|
|
|
_ = nosql.GetManager().CloseLevelDB(fifo.connection)
|
|
|
|
return err
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:19:58 +08:00
|
|
|
// Len returns the length of the fifo
|
2021-05-15 22:22:26 +08:00
|
|
|
func (fifo *LevelQueueByteFIFO) Len(ctx context.Context) int64 {
|
2020-02-03 07:19:58 +08:00
|
|
|
return fifo.internal.Len()
|
2020-01-07 19:23:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
queuesMap[LevelQueueType] = NewLevelQueue
|
|
|
|
}
|