options.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package agollo
  2. import (
  3. "time"
  4. )
  5. type Options struct {
  6. ConfigServerURL string // apollo 服务地址
  7. AppID string // appid
  8. Cluster string // 默认的集群名称,默认:default
  9. DefaultNamespace string // 默认的命名空间,默认:application
  10. PreloadNamespaces []string // 预加载命名空间,默认:application
  11. ApolloClient ApolloClient // apollo HTTP api实现
  12. Logger Logger // 需要日志需要设置实现,或者注入有效的io.Writer,默认: ioutil.Discard
  13. AutoFetchOnCacheMiss bool // 自动获取非预设以外的namespace的配置,默认:false
  14. LongPollerInterval time.Duration // 轮训间隔时间,默认:1s
  15. BackupFile string // 备份文件存放地址,默认:.agollo
  16. FailTolerantOnBackupExists bool // 服务器连接失败时允许读取备份,默认:false
  17. }
  18. func newOptions(opts ...Option) Options {
  19. var options = Options{
  20. AutoFetchOnCacheMiss: defaultAutoFetchOnCacheMiss,
  21. FailTolerantOnBackupExists: defaultFailTolerantOnBackupExists,
  22. }
  23. for _, opt := range opts {
  24. opt(&options)
  25. }
  26. if options.Cluster == "" {
  27. options.Cluster = defaultCluster
  28. }
  29. if options.DefaultNamespace == "" {
  30. options.DefaultNamespace = defaultNamespace
  31. }
  32. if len(options.PreloadNamespaces) == 0 {
  33. options.PreloadNamespaces = []string{defaultNamespace}
  34. } else {
  35. if !stringInSlice(defaultNamespace, options.PreloadNamespaces) {
  36. PreloadNamespaces(defaultNamespace)(&options)
  37. }
  38. }
  39. if options.ApolloClient == nil {
  40. options.ApolloClient = NewApolloClient()
  41. }
  42. if options.Logger == nil {
  43. options.Logger = NewLogger()
  44. }
  45. if options.LongPollerInterval <= time.Duration(0) {
  46. options.LongPollerInterval = defaultLongPollInterval
  47. }
  48. if options.BackupFile == "" {
  49. options.BackupFile = defaultBackupFile
  50. }
  51. return options
  52. }
  53. type Option func(*Options)
  54. func Cluster(cluster string) Option {
  55. return func(o *Options) {
  56. o.Cluster = cluster
  57. }
  58. }
  59. func DefaultNamespace(defaultNamespace string) Option {
  60. return func(o *Options) {
  61. o.DefaultNamespace = defaultNamespace
  62. }
  63. }
  64. func PreloadNamespaces(namespaces ...string) Option {
  65. return func(o *Options) {
  66. o.PreloadNamespaces = append(o.PreloadNamespaces, namespaces...)
  67. }
  68. }
  69. func WithApolloClient(c ApolloClient) Option {
  70. return func(o *Options) {
  71. o.ApolloClient = c
  72. }
  73. }
  74. func WithLogger(l Logger) Option {
  75. return func(o *Options) {
  76. o.Logger = l
  77. }
  78. }
  79. func AutoFetchOnCacheMiss() Option {
  80. return func(o *Options) {
  81. o.AutoFetchOnCacheMiss = true
  82. }
  83. }
  84. func LongPollerInterval(i time.Duration) Option {
  85. return func(o *Options) {
  86. o.LongPollerInterval = i
  87. }
  88. }
  89. func BackupFile(backupFile string) Option {
  90. return func(o *Options) {
  91. o.BackupFile = backupFile
  92. }
  93. }
  94. func FailTolerantOnBackupExists() Option {
  95. return func(o *Options) {
  96. o.FailTolerantOnBackupExists = true
  97. }
  98. }
  99. type GetOptions struct {
  100. DefaultValue string
  101. Namespace string
  102. }
  103. func newGetOptions(opts ...GetOption) GetOptions {
  104. var getOpts GetOptions
  105. for _, opt := range opts {
  106. opt(&getOpts)
  107. }
  108. return getOpts
  109. }
  110. type GetOption func(*GetOptions)
  111. func WithDefault(defVal string) GetOption {
  112. return func(o *GetOptions) {
  113. o.DefaultValue = defVal
  114. }
  115. }
  116. func WithNamespace(namespace string) GetOption {
  117. return func(o *GetOptions) {
  118. o.Namespace = namespace
  119. }
  120. }