Skip to content

Commit c8f7a6b

Browse files
Slightly simplify the queue settings code to help reduce the risk of problems (#12976)
Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent e374bb7 commit c8f7a6b

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

modules/queue/helper.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,56 @@ import (
99
"reflect"
1010
)
1111

12+
// Mappable represents an interface that can MapTo another interface
13+
type Mappable interface {
14+
MapTo(v interface{}) error
15+
}
16+
1217
// toConfig will attempt to convert a given configuration cfg into the provided exemplar type.
1318
//
1419
// It will tolerate the cfg being passed as a []byte or string of a json representation of the
1520
// exemplar or the correct type of the exemplar itself
1621
func toConfig(exemplar, cfg interface{}) (interface{}, error) {
22+
23+
// First of all check if we've got the same type as the exemplar - if so it's all fine.
1724
if reflect.TypeOf(cfg).AssignableTo(reflect.TypeOf(exemplar)) {
1825
return cfg, nil
1926
}
2027

28+
// Now if not - does it provide a MapTo function we can try?
29+
if mappable, ok := cfg.(Mappable); ok {
30+
newVal := reflect.New(reflect.TypeOf(exemplar))
31+
if err := mappable.MapTo(newVal.Interface()); err == nil {
32+
return newVal.Elem().Interface(), nil
33+
}
34+
// MapTo has failed us ... let's try the json route ...
35+
}
36+
37+
// OK we've been passed a byte array right?
2138
configBytes, ok := cfg.([]byte)
2239
if !ok {
23-
configStr, ok := cfg.(string)
24-
if !ok {
25-
return nil, ErrInvalidConfiguration{cfg: cfg}
26-
}
40+
// oh ... it's a string then?
41+
var configStr string
42+
43+
configStr, ok = cfg.(string)
2744
configBytes = []byte(configStr)
2845
}
46+
if !ok {
47+
// hmm ... can we marshal it to json?
48+
var err error
49+
50+
configBytes, err = json.Marshal(cfg)
51+
ok = (err == nil)
52+
}
53+
if !ok {
54+
// no ... we've tried hard enough at this point - throw an error!
55+
return nil, ErrInvalidConfiguration{cfg: cfg}
56+
}
57+
58+
// OK unmarshal the byte array into a new copy of the exemplar
2959
newVal := reflect.New(reflect.TypeOf(exemplar))
3060
if err := json.Unmarshal(configBytes, newVal.Interface()); err != nil {
61+
// If we can't unmarshal it then return an error!
3162
return nil, ErrInvalidConfiguration{cfg: cfg, err: err}
3263
}
3364
return newVal.Elem().Interface(), nil

modules/queue/setting.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,10 @@ func validType(t string) (Type, error) {
2727

2828
func getQueueSettings(name string) (setting.QueueSettings, []byte) {
2929
q := setting.GetQueueSettings(name)
30-
opts := make(map[string]interface{})
31-
opts["Name"] = name
32-
opts["QueueLength"] = q.Length
33-
opts["BatchLength"] = q.BatchLength
34-
opts["DataDir"] = q.DataDir
35-
opts["Addresses"] = q.Addresses
36-
opts["Network"] = q.Network
37-
opts["Password"] = q.Password
38-
opts["DBIndex"] = q.DBIndex
39-
opts["QueueName"] = q.QueueName
40-
opts["SetName"] = q.SetName
41-
opts["Workers"] = q.Workers
42-
opts["MaxWorkers"] = q.MaxWorkers
43-
opts["BlockTimeout"] = q.BlockTimeout
44-
opts["BoostTimeout"] = q.BoostTimeout
45-
opts["BoostWorkers"] = q.BoostWorkers
46-
opts["ConnectionString"] = q.ConnectionString
4730

48-
cfg, err := json.Marshal(opts)
31+
cfg, err := json.Marshal(q)
4932
if err != nil {
50-
log.Error("Unable to marshall generic options: %v Error: %v", opts, err)
33+
log.Error("Unable to marshall generic options: %v Error: %v", q, err)
5134
log.Error("Unable to create queue for %s", name, err)
5235
return q, []byte{}
5336
}
@@ -75,7 +58,7 @@ func CreateQueue(name string, handle HandlerFunc, exemplar interface{}) Queue {
7558
Timeout: q.Timeout,
7659
MaxAttempts: q.MaxAttempts,
7760
Config: cfg,
78-
QueueLength: q.Length,
61+
QueueLength: q.QueueLength,
7962
Name: name,
8063
}, exemplar)
8164
}
@@ -114,7 +97,7 @@ func CreateUniqueQueue(name string, handle HandlerFunc, exemplar interface{}) Un
11497
Timeout: q.Timeout,
11598
MaxAttempts: q.MaxAttempts,
11699
Config: cfg,
117-
QueueLength: q.Length,
100+
QueueLength: q.QueueLength,
118101
}, exemplar)
119102
}
120103
if err != nil {

modules/setting/queue.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616

1717
// QueueSettings represent the settings for a queue from the ini
1818
type QueueSettings struct {
19+
Name string
1920
DataDir string
20-
Length int
21+
QueueLength int `ini:"LENGTH"`
2122
BatchLength int
2223
ConnectionString string
2324
Type string
@@ -44,6 +45,8 @@ var Queue = QueueSettings{}
4445
func GetQueueSettings(name string) QueueSettings {
4546
q := QueueSettings{}
4647
sec := Cfg.Section("queue." + name)
48+
q.Name = name
49+
4750
// DataDir is not directly inheritable
4851
q.DataDir = filepath.Join(Queue.DataDir, name)
4952
// QueueName is not directly inheritable either
@@ -65,8 +68,9 @@ func GetQueueSettings(name string) QueueSettings {
6568
q.DataDir = filepath.Join(AppDataPath, q.DataDir)
6669
}
6770
_, _ = sec.NewKey("DATADIR", q.DataDir)
71+
6872
// The rest are...
69-
q.Length = sec.Key("LENGTH").MustInt(Queue.Length)
73+
q.QueueLength = sec.Key("LENGTH").MustInt(Queue.QueueLength)
7074
q.BatchLength = sec.Key("BATCH_LENGTH").MustInt(Queue.BatchLength)
7175
q.ConnectionString = sec.Key("CONN_STR").MustString(Queue.ConnectionString)
7276
q.Type = sec.Key("TYPE").MustString(Queue.Type)
@@ -91,7 +95,7 @@ func NewQueueService() {
9195
if !filepath.IsAbs(Queue.DataDir) {
9296
Queue.DataDir = filepath.Join(AppDataPath, Queue.DataDir)
9397
}
94-
Queue.Length = sec.Key("LENGTH").MustInt(20)
98+
Queue.QueueLength = sec.Key("LENGTH").MustInt(20)
9599
Queue.BatchLength = sec.Key("BATCH_LENGTH").MustInt(20)
96100
Queue.ConnectionString = sec.Key("CONN_STR").MustString("")
97101
Queue.Type = sec.Key("TYPE").MustString("persistable-channel")

0 commit comments

Comments
 (0)