many fixes

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-08-25 21:51:15 +03:00
parent 70a55ced3f
commit e3b2a0d62b
5 changed files with 163 additions and 235 deletions

View File

@@ -2,19 +2,15 @@ package kgo
import (
"context"
"net"
"time"
kgo "github.com/twmb/franz-go/pkg/kgo"
sasl "github.com/twmb/franz-go/pkg/sasl"
"github.com/unistack-org/micro/v3/broker"
"github.com/unistack-org/micro/v3/client"
)
var (
// DefaultCommitInterval specifies how fast send commit offsets to kafka
DefaultCommitInterval = 5 * time.Second
)
// DefaultCommitInterval specifies how fast send commit offsets to kafka
var DefaultCommitInterval = 5 * time.Second
type subscribeContextKey struct{}
@@ -35,116 +31,21 @@ func ClientPublishKey(key []byte) client.PublishOption {
return client.SetPublishOption(publishKey{}, key)
}
type clientIDKey struct{}
// ClientID sets the kafka client id
func ClientID(id string) broker.Option {
return broker.SetOption(clientIDKey{}, id)
}
type maxReadBytesKey struct{}
// MaxReadBytes limit max bytes to read
func MaxReadBytes(n int32) broker.Option {
return broker.SetOption(maxReadBytesKey{}, n)
}
type maxWriteBytesKey struct{}
// MaxWriteBytes limit max bytes to write
func MaxWriteBytes(n int32) broker.Option {
return broker.SetOption(maxWriteBytesKey{}, n)
}
type connIdleTimeoutKey struct{}
// ConnIdleTimeout limit timeout for connection
func ConnIdleTimeout(td time.Duration) broker.Option {
return broker.SetOption(connIdleTimeoutKey{}, td)
}
type connTimeoutOverheadKey struct{}
// ConnTimeoutOverhead ...
func ConnTimeoutOverhead(td time.Duration) broker.Option {
return broker.SetOption(connTimeoutOverheadKey{}, td)
}
type dialerKey struct{}
// Dialer pass dialer
func Dialer(fn func(ctx context.Context, network, host string) (net.Conn, error)) broker.Option {
return broker.SetOption(dialerKey{}, fn)
}
type metadataMaxAgeKey struct{}
// MetadataMaxAge limit metadata max age
func MetadataMaxAge(td time.Duration) broker.Option {
return broker.SetOption(metadataMaxAgeKey{}, td)
}
type metadataMinAgeKey struct{}
// MetadataMinAge limit metadata min age
func MetadataMinAge(td time.Duration) broker.Option {
return broker.SetOption(metadataMinAgeKey{}, td)
}
type produceRetriesKey struct{}
// ProduceRetries limit number of retries
func ProduceRetries(n int) broker.Option {
return broker.SetOption(produceRetriesKey{}, n)
}
type requestRetriesKey struct{}
// RequestRetries limit number of retries
func RequestRetries(n int) broker.Option {
return broker.SetOption(requestRetriesKey{}, n)
}
type retryBackoffFnKey struct{}
// RetryBackoffFn set backoff func for retry
func RetryBackoffFn(fn func(int) time.Duration) broker.Option {
return broker.SetOption(retryBackoffFnKey{}, fn)
}
type retryTimeoutKey struct{}
// RetryTimeout limit retry timeout
func RetryTimeout(td time.Duration) broker.Option {
return broker.SetOption(retryTimeoutKey{}, td)
}
type retryTimeoutFnKey struct{}
// RetryTimeoutFn set func limit retry timeout
func RetryTimeoutFn(fn func(int16) time.Duration) broker.Option {
return broker.SetOption(retryTimeoutFnKey{}, fn)
}
type saslKey struct{}
// SASL pass sasl mechanism to auth
func SASL(sasls ...sasl.Mechanism) broker.Option {
return broker.SetOption(saslKey{}, sasls)
}
type hooksKey struct{}
// Hooks pass hooks (useful for tracing and logging)
func Hooks(hooks ...kgo.Hook) broker.Option {
return broker.SetOption(hooksKey{}, hooks)
}
type optionsKey struct{}
// Options pass additional options to broker
func Options(opts ...kgo.Opt) broker.Option {
return broker.SetOption(optionsKey{}, opts)
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
options, ok := o.Context.Value(optionsKey{}).([]kgo.Opt)
if !ok {
options = make([]kgo.Opt, 0, len(opts))
}
options = append(options, opts...)
o.Context = context.WithValue(o.Context, optionsKey{}, options)
}
}
type commitIntervalKey struct{}