github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/client/mq/kafka/consumer/consumer_group_option.go (about) 1 package consumer 2 3 import ( 4 "github.com/weedge/lib/client/mq/kafka/auth" 5 "strings" 6 ) 7 8 type ConsumerGroupOptions struct { 9 version string // kafka version 10 brokerList []string // kafka broker ip:port list 11 topicList []string // subscribe topic list 12 groupId string // consumer groupId 13 reBalanceStrategy string // consumer group partition assignment strategy (range, roundrobin, sticky) 14 initialOffset string // initial offset to consumer (oldest, newest) 15 16 *auth.AuthOptions 17 } 18 19 type Option interface { 20 apply(ConsumerGroupOptions *ConsumerGroupOptions) 21 } 22 23 type funcServerOption struct { 24 f func(*ConsumerGroupOptions) 25 } 26 27 func (fdo *funcServerOption) apply(do *ConsumerGroupOptions) { 28 fdo.f(do) 29 } 30 31 func newFuncServerOption(f func(ConsumerGroupOptions *ConsumerGroupOptions)) *funcServerOption { 32 return &funcServerOption{ 33 f: f, 34 } 35 } 36 37 // kafka brokers ip:port,ip:port 38 func WithBrokerList(brokers string) Option { 39 return newFuncServerOption(func(o *ConsumerGroupOptions) { 40 if len(brokers) == 0 { 41 panic("empty brokers") 42 } 43 o.brokerList = strings.Split(brokers, ",") 44 }) 45 } 46 47 // consumer groupId 48 func WithGroupId(groupId string) Option { 49 return newFuncServerOption(func(o *ConsumerGroupOptions) { 50 if len(groupId) == 0 { 51 panic("no groupId to consumer") 52 } 53 o.groupId = groupId 54 }) 55 } 56 57 // kafka version 58 func WithVersion(version string) Option { 59 return newFuncServerOption(func(o *ConsumerGroupOptions) { 60 if len(version) == 0 { 61 return 62 } 63 o.version = version 64 }) 65 } 66 67 // subscribe topics "test1,test2" 68 func WithTopicList(topics string) Option { 69 return newFuncServerOption(func(o *ConsumerGroupOptions) { 70 if len(topics) == 0 { 71 panic("empty topics") 72 } 73 o.topicList = strings.Split(topics, ",") 74 }) 75 } 76 77 // consumer group partition assignment strategy (range, roundrobin, sticky) 78 func WithReBalanceStrategy(reBalanceStrategy string) Option { 79 return newFuncServerOption(func(o *ConsumerGroupOptions) { 80 if len(reBalanceStrategy) == 0 { 81 return 82 } 83 o.reBalanceStrategy = reBalanceStrategy 84 }) 85 } 86 87 // initial offset to consumer (oldest, newest) 88 func WithInitialOffset(initialOffset string) Option { 89 return newFuncServerOption(func(o *ConsumerGroupOptions) { 90 if len(initialOffset) == 0 { 91 return 92 } 93 o.initialOffset = initialOffset 94 }) 95 } 96 97 func getConsumerOptions(authOpts []auth.Option, opts ...Option) *ConsumerGroupOptions { 98 ConsumerGroupOptions := &ConsumerGroupOptions{ 99 version: "0.8.2.0", 100 groupId: "", 101 brokerList: nil, 102 topicList: nil, 103 reBalanceStrategy: "sticky", 104 initialOffset: "newest", 105 AuthOptions: auth.GetAuthOptions(authOpts...), 106 } 107 108 for _, o := range opts { 109 o.apply(ConsumerGroupOptions) 110 } 111 return ConsumerGroupOptions 112 }