github.com/polarismesh/polaris@v1.17.8/service/batch/config.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package batch 19 20 import ( 21 "errors" 22 23 "github.com/mitchellh/mapstructure" 24 ) 25 26 // Config 批量配置,控制最大的条目,批量等待时间等 27 type Config struct { 28 Register *CtrlConfig `mapstructure:"register"` 29 Deregister *CtrlConfig `mapstructure:"deregister"` 30 Heartbeat *CtrlConfig `mapstructure:"heartbeat"` 31 ClientRegister *CtrlConfig `mapstructure:"clientRegister"` 32 ClientDeregister *CtrlConfig `mapstructure:"clientDeregister"` 33 } 34 35 // CtrlConfig batch控制配置项 36 type CtrlConfig struct { 37 // 是否开启Batch工作模式 38 Open bool `mapstructure:"open"` 39 // 注册请求队列的长度 40 QueueSize int `mapstructure:"queueSize"` 41 // 最长多久一次批量操作 42 WaitTime string `mapstructure:"waitTime"` 43 // 每次操作最大的批量数 44 MaxBatchCount int `mapstructure:"maxBatchCount"` 45 // 写store的并发协程数 46 Concurrency int `mapstructure:"concurrency"` 47 // 任务最大存活周期 48 TaskLife string `mapstructure:"taskLife"` 49 } 50 51 func defaultBatchCtrlConfig() *Config { 52 return &Config{ 53 Register: &CtrlConfig{ 54 Open: true, 55 QueueSize: 10240, 56 WaitTime: "32ms", 57 MaxBatchCount: 32, 58 Concurrency: 64, 59 TaskLife: "30s", 60 }, 61 Deregister: &CtrlConfig{ 62 Open: true, 63 QueueSize: 10240, 64 WaitTime: "32ms", 65 MaxBatchCount: 32, 66 Concurrency: 64, 67 }, 68 Heartbeat: &CtrlConfig{ 69 Open: true, 70 QueueSize: 10240, 71 WaitTime: "32ms", 72 MaxBatchCount: 32, 73 Concurrency: 64, 74 }, 75 ClientRegister: &CtrlConfig{ 76 Open: true, 77 QueueSize: 10240, 78 WaitTime: "32ms", 79 MaxBatchCount: 32, 80 Concurrency: 64, 81 }, 82 ClientDeregister: &CtrlConfig{ 83 Open: true, 84 QueueSize: 10240, 85 WaitTime: "32ms", 86 MaxBatchCount: 32, 87 Concurrency: 64, 88 }, 89 } 90 } 91 92 // ParseBatchConfig 解析配置文件为config 93 func ParseBatchConfig(opt map[string]interface{}) (*Config, error) { 94 if opt == nil { 95 return nil, nil 96 } 97 98 config := defaultBatchCtrlConfig() 99 if err := mapstructure.Decode(opt, &config); err != nil { 100 log.Errorf("[Batch] parse config(%+v) err: %s", opt, err.Error()) 101 return nil, err 102 } 103 104 // 对配置文件做校验 105 if !checkCtrlConfig(config.Register) { 106 log.Errorf("[Controller] batch register config is invalid: %+v", config) 107 return nil, errors.New("batch register config is invalid") 108 } 109 if !checkCtrlConfig(config.Deregister) { 110 log.Errorf("[Controller] batch deregister config is invalid: %+v", config) 111 return nil, errors.New("batch deregister config is invalid") 112 } 113 if !checkCtrlConfig(config.Heartbeat) { 114 log.Errorf("[Controller] batch heartbeat config is invalid: %+v", config) 115 return nil, errors.New("batch deregister config is invalid") 116 } 117 if !checkCtrlConfig(config.ClientRegister) { 118 log.Errorf("[Controller] batch client register config is invalid: %+v", config) 119 return nil, errors.New("batch client register config is invalid") 120 } 121 if !checkCtrlConfig(config.ClientDeregister) { 122 log.Errorf("[Controller] batch client deregister config is invalid: %+v", config) 123 return nil, errors.New("batch client deregister config is invalid") 124 } 125 return config, nil 126 } 127 128 // checkCtrlConfig 配置文件校验 129 func checkCtrlConfig(ctrl *CtrlConfig) bool { 130 if ctrl == nil { 131 return true 132 } 133 134 if ctrl.QueueSize <= 0 || ctrl.MaxBatchCount <= 0 || ctrl.Concurrency <= 0 { 135 return false 136 } 137 138 return true 139 }