Skip to content

client option to set DefaultMaxMessageSize #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pulsar/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ type ClientOptions struct {

// Add custom labels to all the metrics reported by this client instance
CustomMetricsLabels map[string]string

// The default max message size in bytes. The broker default is 5,242,880 (5MB) but it can be increased.
DefaultMaxMessageSize int
}

// Client represents a pulsar client
Expand Down
6 changes: 5 additions & 1 deletion pulsar/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func newClient(options ClientOptions) (Client, error) {
operationTimeout = defaultOperationTimeout
}

if options.DefaultMaxMessageSize < internal.MaxMessageSize {
options.DefaultMaxMessageSize = internal.MaxMessageSize
}

maxConnectionsPerHost := options.MaxConnectionsPerBroker
if maxConnectionsPerHost <= 0 {
maxConnectionsPerHost = 1
Expand All @@ -120,7 +124,7 @@ func newClient(options ClientOptions) (Client, error) {

c := &client{
cnxPool: internal.NewConnectionPool(tlsConfig, authProvider, connectionTimeout, maxConnectionsPerHost, logger,
metrics),
options.DefaultMaxMessageSize, metrics),
log: logger,
metrics: metrics,
}
Expand Down
21 changes: 10 additions & 11 deletions pulsar/internal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,14 @@ type connection struct {

// connectionOptions defines configurations for creating connection.
type connectionOptions struct {
logicalAddr *url.URL
physicalAddr *url.URL
tls *TLSOptions
connectionTimeout time.Duration
auth auth.Provider
logger log.Logger
metrics *Metrics
logicalAddr *url.URL
physicalAddr *url.URL
tls *TLSOptions
connectionTimeout time.Duration
auth auth.Provider
logger log.Logger
defaultMaxMessageSize int32
metrics *Metrics
}

func newConnection(opts connectionOptions) *connection {
Expand Down Expand Up @@ -206,6 +207,7 @@ func newConnection(opts connectionOptions) *connection {
listeners: make(map[uint64]ConnectionListener),
consumerHandlers: make(map[uint64]ConsumerHandler),
metrics: opts.metrics,
maxMessageSize: opts.defaultMaxMessageSize,
}
cnx.setState(connectionInit)
cnx.reader = newConnectionReader(cnx)
Expand Down Expand Up @@ -317,11 +319,8 @@ func (c *connection) doHandshake() bool {
if cmd.Connected.MaxMessageSize != nil && *cmd.Connected.MaxMessageSize > 0 {
c.log.Debug("Got MaxMessageSize from handshake response:", *cmd.Connected.MaxMessageSize)
c.maxMessageSize = *cmd.Connected.MaxMessageSize
} else {
c.log.Debug("No MaxMessageSize from handshake response, use default: ", MaxMessageSize)
c.maxMessageSize = MaxMessageSize
}
c.log.Info("Connection is ready")
c.log.Info("Connection is ready with maxMessageSize ", c.maxMessageSize)
c.changeState(connectionReady)
return true
}
Expand Down
18 changes: 11 additions & 7 deletions pulsar/internal/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type connectionPool struct {
maxConnectionsPerHost int32
roundRobinCnt int32
metrics *Metrics
defaultMaxMessageSize int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest making this an int32 to be consistent with the type of connection.defaultMaxMessageSize.


log log.Logger
}
Expand All @@ -57,6 +58,7 @@ func NewConnectionPool(
connectionTimeout time.Duration,
maxConnectionsPerHost int,
logger log.Logger,
defaultMaxMsgSize int,
metrics *Metrics) ConnectionPool {
return &connectionPool{
connections: make(map[string]*connection),
Expand All @@ -66,6 +68,7 @@ func NewConnectionPool(
maxConnectionsPerHost: int32(maxConnectionsPerHost),
log: logger,
metrics: metrics,
defaultMaxMessageSize: defaultMaxMsgSize,
}
}

Expand All @@ -89,13 +92,14 @@ func (p *connectionPool) GetConnection(logicalAddr *url.URL, physicalAddr *url.U

if conn == nil {
conn = newConnection(connectionOptions{
logicalAddr: logicalAddr,
physicalAddr: physicalAddr,
tls: p.tlsOptions,
connectionTimeout: p.connectionTimeout,
auth: p.auth,
logger: p.log,
metrics: p.metrics,
logicalAddr: logicalAddr,
physicalAddr: physicalAddr,
tls: p.tlsOptions,
connectionTimeout: p.connectionTimeout,
auth: p.auth,
logger: p.log,
metrics: p.metrics,
defaultMaxMessageSize: int32(p.defaultMaxMessageSize),
})
p.connections[key] = conn
p.Unlock()
Expand Down