github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/kafka/utils.go (about) 1 /* 2 Copyright 2021 The Dapr Authors 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12 */ 13 14 package kafka 15 16 import ( 17 "encoding/base64" 18 "encoding/pem" 19 "fmt" 20 "strings" 21 22 "github.com/Shopify/sarama" 23 ) 24 25 const ( 26 // DefaultMaxBulkSubCount is the default max bulk count for kafka pubsub component 27 // if the MaxBulkCountKey is not set in the metadata. 28 DefaultMaxBulkSubCount = 80 29 // DefaultMaxBulkSubAwaitDurationMs is the default max bulk await duration for kafka pubsub component 30 // if the MaxBulkAwaitDurationKey is not set in the metadata. 31 DefaultMaxBulkSubAwaitDurationMs = 10000 32 ) 33 34 // asBase64String implements the `fmt.Stringer` interface in order to print 35 // `[]byte` as a base 64 encoded string. 36 // It is used above to log the message key. The call to `EncodeToString` 37 // only occurs for logs that are written based on the logging level. 38 type asBase64String []byte 39 40 func (s asBase64String) String() string { 41 return base64.StdEncoding.EncodeToString(s) 42 } 43 44 func parseInitialOffset(value string) (initialOffset int64, err error) { 45 switch strings.ToLower(value) { 46 case "oldest": 47 initialOffset = sarama.OffsetOldest 48 case "newest": 49 initialOffset = sarama.OffsetNewest 50 default: 51 return 0, fmt.Errorf("kafka error: invalid initialOffset: %s", value) 52 } 53 return initialOffset, err 54 } 55 56 // isValidPEM validates the provided input has PEM formatted block. 57 func isValidPEM(val string) bool { 58 block, _ := pem.Decode([]byte(val)) 59 60 return block != nil 61 } 62 63 // TopicHandlerConfig is the map of topics and sruct containing handler and their config. 64 type TopicHandlerConfig map[string]SubscriptionHandlerConfig 65 66 // TopicList returns the list of topics 67 func (tbh TopicHandlerConfig) TopicList() []string { 68 topics := make([]string, len(tbh)) 69 i := 0 70 for topic := range tbh { 71 topics[i] = topic 72 i++ 73 } 74 return topics 75 }