git.zd.zone/hrpc/hrpc@v0.0.12/mq/kafka/option.go (about) 1 package kafka 2 3 import "github.com/Shopify/sarama" 4 5 type Options struct { 6 Brokers string `json:"brokers"` 7 SASL sasl `json:"sasl"` 8 Acks RequiredAcks `json:"acks"` 9 AutoCommit bool `json:"auto_commit"` 10 OffsetInitial int64 `json:"offset_initial"` 11 version sarama.KafkaVersion `json:"-"` 12 } 13 14 type sasl struct { 15 enabled bool `json:"-"` 16 Username string `json:"username"` 17 Password string `json:"password"` 18 } 19 20 type Option func(*Options) 21 22 func mergeOptions(opts ...Option) *Options { 23 var opt = &Options{ 24 OffsetInitial: OffsetNewest, 25 Acks: WaitForLocal, 26 version: sarama.V1_1_1_0, 27 } 28 for _, o := range opts { 29 o(opt) 30 } 31 return opt 32 } 33 34 func WithSASL(username, password string) Option { 35 return func(o *Options) { 36 o.SASL.enabled = true 37 o.SASL.Username = username 38 o.SASL.Password = password 39 } 40 } 41 42 func WithAcks(ack RequiredAcks) Option { 43 return func(o *Options) { 44 o.Acks = ack 45 } 46 } 47 48 func WithAutoCommit() Option { 49 return func(o *Options) { 50 o.AutoCommit = true 51 } 52 } 53 54 func WithOffsetInitial(i int64) Option { 55 return func(o *Options) { 56 o.OffsetInitial = i 57 } 58 } 59 60 func WithVersion(v string) Option { 61 var version sarama.KafkaVersion 62 switch v { 63 case "1.1.1": 64 version = sarama.V1_1_1_0 65 default: 66 version = sarama.V1_1_1_0 67 } 68 return func(o *Options) { 69 o.version = version 70 } 71 } 72 73 type RequiredAcks int16 74 75 const ( 76 // NoResponse doesn't send any response, the TCP ACK is all you get. 77 NoResponse RequiredAcks = 0 78 // WaitForLocal waits for only the local commit to succeed before responding. 79 WaitForLocal RequiredAcks = 1 80 // WaitForAll waits for all in-sync replicas to commit before responding. 81 // The minimum number of in-sync replicas is configured on the broker via 82 // the `min.insync.replicas` configuration key. 83 WaitForAll RequiredAcks = -1 84 ) 85 86 const ( 87 // OffsetNewest stands for the log head offset, i.e. the offset that will be 88 // assigned to the next message that will be produced to the partition. You 89 // can send this to a client's GetOffset method to get this offset, or when 90 // calling ConsumePartition to start consuming new messages. 91 OffsetNewest int64 = -1 92 // OffsetOldest stands for the oldest offset available on the broker for a 93 // partition. You can send this to a client's GetOffset method to get this 94 // offset, or when calling ConsumePartition to start consuming from the 95 // oldest offset that is still available on the broker. 96 OffsetOldest int64 = -2 97 )