-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
82 lines (76 loc) · 2.23 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gpool
import (
"errors"
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
)
//Config pool config
type Config struct {
//InitialPoolSize initial pool size. Default: 5
InitialPoolSize int
//MinPoolSize min item in pool. Default: 2
MinPoolSize int
//MaxPoolSize max item in pool. Default: 15
MaxPoolSize int
//AcquireRetryAttempts retry times when get item Failed. Default: 5
AcquireRetryAttempts int
//AcquireIncrement create item count when pool is empty. Default: 5
AcquireIncrement int
//TestDuration interval time between check item avaiable.Unit:Millisecond Default: 1000
TestDuration int
//TestOnGetItem test avaiable when get item. Default: false
TestOnGetItem bool
//Params item initial params
Params map[string]string
}
//String String
func (config *Config) String() string {
result := fmt.Sprintf("InitialPoolSize : %d \n MinPoolSize : %d \n MaxPoolSize : %d \n AcquireRetryAttempts : %d \n AcquireIncrement : %d \n TestDuration : %d \n TestOnGetItem : %t \n",
config.InitialPoolSize,
config.MinPoolSize,
config.MaxPoolSize,
config.AcquireRetryAttempts,
config.AcquireIncrement,
config.TestDuration,
config.TestOnGetItem,
)
result = result + "Params:\n"
for key, value := range config.Params {
result = result + fmt.Sprintf("\t%s : %s \n", key, value)
}
return result
}
//LoadToml load config from toml file
func (config *Config) LoadToml(tomlFilePath string) error {
log.WithField("tomlFileName", tomlFilePath).Debugf("load config")
inf, err := os.Stat(tomlFilePath)
if err != nil {
log.WithError(err).Error("load toml config ERROR - FILE NOT EXIST ")
return err
}
if !strings.HasSuffix(inf.Name(), ".toml") {
log.WithFields(log.Fields{
"need": "*.toml",
"got": inf.Name,
}).Error("load toml config ERROR - FILE TYPE ERROR")
return errors.New("FILE TYPE ERROR")
}
_, err = toml.DecodeFile(tomlFilePath, config)
return err
}
//DefaultConfig create default config
func DefaultConfig() Config {
return Config{
InitialPoolSize: 5,
MinPoolSize: 2,
MaxPoolSize: 15,
AcquireRetryAttempts: 5,
AcquireIncrement: 5,
TestDuration: 60000,
TestOnGetItem: false,
Params: make(map[string]string),
}
}