-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow common redis and leveldb connections
Prevents multiple reopening of redis and leveldb connections to the same place by sharing connections. Further allows for more configurable redis connection type using the redisURI and a leveldbURI scheme. Signed-off-by: Andrew Thornton <art27@cantab.net>
- Loading branch information
Showing
70 changed files
with
4,836 additions
and
3,088 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2020 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 nosql | ||
|
||
import "net/url" | ||
|
||
// ToLevelDBURI converts old style connections to a LevelDBURI | ||
// | ||
// A LevelDBURI matches the pattern: | ||
// | ||
// leveldb://path[?[option=value]*] | ||
// | ||
// We have previously just provided the path but this prevent other options | ||
func ToLevelDBURI(connection string) *url.URL { | ||
uri, err := url.Parse(connection) | ||
if err == nil && uri.Scheme == "leveldb" { | ||
return uri | ||
} | ||
uri, _ = url.Parse("leveldb://common") | ||
uri.Host = "" | ||
uri.Path = connection | ||
return uri | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2020 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 nosql | ||
|
||
import ( | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v7" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
) | ||
|
||
var manager *Manager | ||
|
||
// Manager is the nosql connection manager | ||
type Manager struct { | ||
mutex sync.Mutex | ||
|
||
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 { | ||
manager = &Manager{ | ||
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 { | ||
return | ||
} | ||
} | ||
return | ||
} |
Oops, something went wrong.