github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/adminoptions.go (about) 1 /** 2 * Copyright 2018 Confluent Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package kafka 18 19 import ( 20 "fmt" 21 "time" 22 "unsafe" 23 ) 24 25 /* 26 #include "select_rdkafka.h" 27 #include <stdlib.h> 28 */ 29 import "C" 30 31 // AdminOptionOperationTimeout sets the broker's operation timeout, such as the 32 // timeout for CreateTopics to complete the creation of topics on the controller 33 // before returning a result to the application. 34 // 35 // CreateTopics, DeleteTopics, CreatePartitions: 36 // a value 0 will return immediately after triggering topic 37 // creation, while > 0 will wait this long for topic creation to propagate 38 // in cluster. 39 // 40 // Default: 0 (return immediately). 41 // 42 // Valid for CreateTopics, DeleteTopics, CreatePartitions. 43 type AdminOptionOperationTimeout struct { 44 isSet bool 45 val time.Duration 46 } 47 48 func (ao AdminOptionOperationTimeout) supportsCreateTopics() { 49 } 50 func (ao AdminOptionOperationTimeout) supportsDeleteTopics() { 51 } 52 func (ao AdminOptionOperationTimeout) supportsCreatePartitions() { 53 } 54 55 func (ao AdminOptionOperationTimeout) apply(cOptions *C.rd_kafka_AdminOptions_t) error { 56 if !ao.isSet { 57 return nil 58 } 59 60 cErrstrSize := C.size_t(512) 61 cErrstr := (*C.char)(C.malloc(cErrstrSize)) 62 defer C.free(unsafe.Pointer(cErrstr)) 63 64 cErr := C.rd_kafka_AdminOptions_set_operation_timeout( 65 cOptions, C.int(durationToMilliseconds(ao.val)), 66 cErrstr, cErrstrSize) 67 if cErr != 0 { 68 C.rd_kafka_AdminOptions_destroy(cOptions) 69 return newCErrorFromString(cErr, 70 fmt.Sprintf("Failed to set operation timeout: %s", C.GoString(cErrstr))) 71 72 } 73 74 return nil 75 } 76 77 // SetAdminOperationTimeout sets the broker's operation timeout, such as the 78 // timeout for CreateTopics to complete the creation of topics on the controller 79 // before returning a result to the application. 80 // 81 // CreateTopics, DeleteTopics, CreatePartitions: 82 // a value 0 will return immediately after triggering topic 83 // creation, while > 0 will wait this long for topic creation to propagate 84 // in cluster. 85 // 86 // Default: 0 (return immediately). 87 // 88 // Valid for CreateTopics, DeleteTopics, CreatePartitions. 89 func SetAdminOperationTimeout(t time.Duration) (ao AdminOptionOperationTimeout) { 90 ao.isSet = true 91 ao.val = t 92 return ao 93 } 94 95 // AdminOptionRequestTimeout sets the overall request timeout, including broker 96 // lookup, request transmission, operation time on broker, and response. 97 // 98 // Default: `socket.timeout.ms`. 99 // 100 // Valid for all Admin API methods. 101 type AdminOptionRequestTimeout struct { 102 isSet bool 103 val time.Duration 104 } 105 106 func (ao AdminOptionRequestTimeout) supportsCreateTopics() { 107 } 108 func (ao AdminOptionRequestTimeout) supportsDeleteTopics() { 109 } 110 func (ao AdminOptionRequestTimeout) supportsCreatePartitions() { 111 } 112 func (ao AdminOptionRequestTimeout) supportsAlterConfigs() { 113 } 114 func (ao AdminOptionRequestTimeout) supportsDescribeConfigs() { 115 } 116 117 func (ao AdminOptionRequestTimeout) apply(cOptions *C.rd_kafka_AdminOptions_t) error { 118 if !ao.isSet { 119 return nil 120 } 121 122 cErrstrSize := C.size_t(512) 123 cErrstr := (*C.char)(C.malloc(cErrstrSize)) 124 defer C.free(unsafe.Pointer(cErrstr)) 125 126 cErr := C.rd_kafka_AdminOptions_set_request_timeout( 127 cOptions, C.int(durationToMilliseconds(ao.val)), 128 cErrstr, cErrstrSize) 129 if cErr != 0 { 130 C.rd_kafka_AdminOptions_destroy(cOptions) 131 return newCErrorFromString(cErr, 132 fmt.Sprintf("%s", C.GoString(cErrstr))) 133 134 } 135 136 return nil 137 } 138 139 // SetAdminRequestTimeout sets the overall request timeout, including broker 140 // lookup, request transmission, operation time on broker, and response. 141 // 142 // Default: `socket.timeout.ms`. 143 // 144 // Valid for all Admin API methods. 145 func SetAdminRequestTimeout(t time.Duration) (ao AdminOptionRequestTimeout) { 146 ao.isSet = true 147 ao.val = t 148 return ao 149 } 150 151 // AdminOptionValidateOnly tells the broker to only validate the request, 152 // without performing the requested operation (create topics, etc). 153 // 154 // Default: false. 155 // 156 // Valid for CreateTopics, CreatePartitions, AlterConfigs 157 type AdminOptionValidateOnly struct { 158 isSet bool 159 val bool 160 } 161 162 func (ao AdminOptionValidateOnly) supportsCreateTopics() { 163 } 164 func (ao AdminOptionValidateOnly) supportsCreatePartitions() { 165 } 166 func (ao AdminOptionValidateOnly) supportsAlterConfigs() { 167 } 168 169 func (ao AdminOptionRequestTimeout) supportsCreateACLs() { 170 } 171 172 func (ao AdminOptionRequestTimeout) supportsDescribeACLs() { 173 } 174 175 func (ao AdminOptionRequestTimeout) supportsDeleteACLs() { 176 } 177 178 func (ao AdminOptionValidateOnly) apply(cOptions *C.rd_kafka_AdminOptions_t) error { 179 if !ao.isSet { 180 return nil 181 } 182 183 cErrstrSize := C.size_t(512) 184 cErrstr := (*C.char)(C.malloc(cErrstrSize)) 185 defer C.free(unsafe.Pointer(cErrstr)) 186 187 cErr := C.rd_kafka_AdminOptions_set_validate_only( 188 cOptions, bool2cint(ao.val), 189 cErrstr, cErrstrSize) 190 if cErr != 0 { 191 C.rd_kafka_AdminOptions_destroy(cOptions) 192 return newCErrorFromString(cErr, 193 fmt.Sprintf("%s", C.GoString(cErrstr))) 194 195 } 196 197 return nil 198 } 199 200 // SetAdminValidateOnly tells the broker to only validate the request, 201 // without performing the requested operation (create topics, etc). 202 // 203 // Default: false. 204 // 205 // Valid for CreateTopics, DeleteTopics, CreatePartitions, AlterConfigs 206 func SetAdminValidateOnly(validateOnly bool) (ao AdminOptionValidateOnly) { 207 ao.isSet = true 208 ao.val = validateOnly 209 return ao 210 } 211 212 // CreateTopicsAdminOption - see setters. 213 // 214 // See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. 215 type CreateTopicsAdminOption interface { 216 supportsCreateTopics() 217 apply(cOptions *C.rd_kafka_AdminOptions_t) error 218 } 219 220 // DeleteTopicsAdminOption - see setters. 221 // 222 // See SetAdminRequestTimeout, SetAdminOperationTimeout. 223 type DeleteTopicsAdminOption interface { 224 supportsDeleteTopics() 225 apply(cOptions *C.rd_kafka_AdminOptions_t) error 226 } 227 228 // CreatePartitionsAdminOption - see setters. 229 // 230 // See SetAdminRequestTimeout, SetAdminOperationTimeout, SetAdminValidateOnly. 231 type CreatePartitionsAdminOption interface { 232 supportsCreatePartitions() 233 apply(cOptions *C.rd_kafka_AdminOptions_t) error 234 } 235 236 // AlterConfigsAdminOption - see setters. 237 // 238 // See SetAdminRequestTimeout, SetAdminValidateOnly, SetAdminIncremental. 239 type AlterConfigsAdminOption interface { 240 supportsAlterConfigs() 241 apply(cOptions *C.rd_kafka_AdminOptions_t) error 242 } 243 244 // DescribeConfigsAdminOption - see setters. 245 // 246 // See SetAdminRequestTimeout. 247 type DescribeConfigsAdminOption interface { 248 supportsDescribeConfigs() 249 apply(cOptions *C.rd_kafka_AdminOptions_t) error 250 } 251 252 // CreateACLsAdminOption - see setter. 253 // 254 // See SetAdminRequestTimeout 255 type CreateACLsAdminOption interface { 256 supportsCreateACLs() 257 apply(cOptions *C.rd_kafka_AdminOptions_t) error 258 } 259 260 // DescribeACLsAdminOption - see setter. 261 // 262 // See SetAdminRequestTimeout 263 type DescribeACLsAdminOption interface { 264 supportsDescribeACLs() 265 apply(cOptions *C.rd_kafka_AdminOptions_t) error 266 } 267 268 // DeleteACLsAdminOption - see setter. 269 // 270 // See SetAdminRequestTimeout 271 type DeleteACLsAdminOption interface { 272 supportsDeleteACLs() 273 apply(cOptions *C.rd_kafka_AdminOptions_t) error 274 } 275 276 // AdminOption is a generic type not to be used directly. 277 // 278 // See CreateTopicsAdminOption et.al. 279 type AdminOption interface { 280 apply(cOptions *C.rd_kafka_AdminOptions_t) error 281 } 282 283 func adminOptionsSetup(h *handle, opType C.rd_kafka_admin_op_t, options []AdminOption) (*C.rd_kafka_AdminOptions_t, error) { 284 285 cOptions := C.rd_kafka_AdminOptions_new(h.rk, opType) 286 for _, opt := range options { 287 if opt == nil { 288 continue 289 } 290 err := opt.apply(cOptions) 291 if err != nil { 292 return nil, err 293 } 294 } 295 296 return cOptions, nil 297 }