2020-09-28 05:09:46 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-09-28 05:09:46 +08:00
|
|
|
|
|
|
|
package nosql
|
|
|
|
|
|
|
|
import (
|
2022-04-01 01:01:43 +08:00
|
|
|
"context"
|
2020-09-28 05:09:46 +08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-04-01 01:01:43 +08:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2022-04-02 00:34:57 +08:00
|
|
|
|
2023-04-14 06:41:04 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2020-09-28 05:09:46 +08:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var manager *Manager
|
|
|
|
|
|
|
|
// Manager is the nosql connection manager
|
|
|
|
type Manager struct {
|
2022-04-01 01:01:43 +08:00
|
|
|
ctx context.Context
|
|
|
|
finished context.CancelFunc
|
|
|
|
mutex sync.Mutex
|
2020-09-28 05:09:46 +08:00
|
|
|
|
|
|
|
RedisConnections map[string]*redisClientHolder
|
|
|
|
LevelDBConnections map[string]*levelDBHolder
|
|
|
|
}
|
|
|
|
|
|
|
|
type redisClientHolder struct {
|
|
|
|
redis.UniversalClient
|
|
|
|
name []string
|
|
|
|
count int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *redisClientHolder) Close() error {
|
|
|
|
return manager.CloseRedisClient(r.name[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
type levelDBHolder struct {
|
|
|
|
name []string
|
|
|
|
count int64
|
|
|
|
db *leveldb.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
_ = GetManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetManager returns a Manager and initializes one as singleton is there's none yet
|
|
|
|
func GetManager() *Manager {
|
|
|
|
if manager == nil {
|
2022-04-01 01:01:43 +08:00
|
|
|
ctx, _, finished := process.GetManager().AddTypedContext(context.Background(), "Service: NoSQL", process.SystemProcessType, false)
|
2020-09-28 05:09:46 +08:00
|
|
|
manager = &Manager{
|
2022-04-01 01:01:43 +08:00
|
|
|
ctx: ctx,
|
|
|
|
finished: finished,
|
2020-09-28 05:09:46 +08:00
|
|
|
RedisConnections: make(map[string]*redisClientHolder),
|
|
|
|
LevelDBConnections: make(map[string]*levelDBHolder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
func valToTimeDuration(vs []string) (result time.Duration) {
|
|
|
|
var err error
|
|
|
|
for _, v := range vs {
|
|
|
|
result, err = time.ParseDuration(v)
|
|
|
|
if err != nil {
|
|
|
|
var val int
|
|
|
|
val, err = strconv.Atoi(v)
|
|
|
|
result = time.Duration(val)
|
|
|
|
}
|
|
|
|
if err == nil {
|
2023-07-09 19:58:06 +08:00
|
|
|
return result
|
2020-09-28 05:09:46 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 18:02:49 +08:00
|
|
|
return result
|
2020-09-28 05:09:46 +08:00
|
|
|
}
|