Skip to content

Commit d4eb956

Browse files
MoumitaMMoumita Mandalsaikumarrs
authored
Feature to make max message and batch bytes configurable (#7)
* max message size option added in config, max size limit changed from 32kb to 1mb * max message size option added in config, max size limit changed from 32kb to 1mb * Added support for MaxMessageBytes and MaxBatchBytes configuration options * Typo fixed * Unwanted print statement removed * Type and undefined reference issues fixed * updated MaxMessageBytes logic * Clean up and typo corrected Co-authored-by: Moumita Mandal <moumita@rudderstack.com> Co-authored-by: saikumarrs <saibattinoju@rudderstack.com>
1 parent 2e85aaf commit d4eb956

File tree

29 files changed

+9052
-53
lines changed

29 files changed

+9052
-53
lines changed

analytics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ func (c *client) push(q *messageQueue, m Message, wg *sync.WaitGroup, ex *execut
537537
var msg message
538538
var err error
539539

540-
if msg, err = makeMessage(m, maxMessageBytes); err != nil {
540+
if msg, err = makeMessage(m, c.MaxMessageBytes); err != nil {
541541
c.errorf("%s - %v", err, m)
542542
c.notifyFailure([]message{{m, nil}}, err)
543543
return
@@ -578,7 +578,7 @@ func (c *client) maxBatchBytes() int {
578578
SentAt: c.now(),
579579
Context: c.DefaultContext,
580580
})
581-
return maxBatchBytes - len(b)
581+
return c.MaxBatchBytes - len(b)
582582
}
583583

584584
func (c *client) notifySuccess(msgs []message) {

config.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ type Config struct {
8282
//This variable will disable checking for the cluster-info end point and
8383
//split the payload at node level for multi node setup
8484
NoProxySupport bool
85+
86+
// Maximum bytes in a message
87+
MaxMessageBytes int
88+
89+
// Maximum bytes in a batch
90+
MaxBatchBytes int
8591
}
8692

8793
// This constant sets the default endpoint to which client instances send
@@ -116,6 +122,22 @@ func (c *Config) validate() error {
116122
}
117123
}
118124

125+
if c.MaxMessageBytes < 0 {
126+
return ConfigError{
127+
Reason: "negetive value is not supported for MaxMessageBytes",
128+
Field: "MaxMessageBytes",
129+
Value: c.MaxMessageBytes,
130+
}
131+
}
132+
133+
if c.MaxBatchBytes < 0 {
134+
return ConfigError{
135+
Reason: "negetive value is not supported for MaxBatchBytes",
136+
Field: "MaxBatchBytes",
137+
Value: c.MaxBatchBytes,
138+
}
139+
}
140+
119141
return nil
120142
}
121143

@@ -162,6 +184,14 @@ func makeConfig(c Config) Config {
162184
c.maxConcurrentRequests = 1000
163185
}
164186

187+
if c.MaxMessageBytes == 0 {
188+
c.MaxMessageBytes = defMaxMessageBytes
189+
}
190+
191+
if c.MaxBatchBytes == 0 {
192+
c.MaxBatchBytes = defMaxBatchBytes
193+
}
194+
165195
// We always overwrite the 'library' field of the default context set on the
166196
// client because we want this information to be accurate.
167197
c.DefaultContext.Library = LibraryInfo{

example/example-1.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ import (
77
func main() {
88
// Instantiates a client to use send messages to the Rudder API.
99
// User your WRITE KEY in below placeholder "RUDDER WRITE KEY"
10-
client := analytics.New("1aUR9IELHp6jqOW8HWkrYvMYHWy", "https://218da72a.ngrok.io")
10+
client := analytics.New("1weqc5iqxRkpaUXHgDgYo3g34mg", "https://hosted.rudderlabs.com")
1111

12-
// Enqueues a track event that will be sent asynchronously.
13-
client.Enqueue(analytics.Track{
14-
UserId: "test-user",
15-
Event: "test-snippet",
16-
})
12+
if client!= nil{
13+
// Enqueues a track event that will be sent asynchronously.
14+
client.Enqueue(analytics.Track{
15+
UserId: "test-user",
16+
Event: "test-snippet",
17+
Properties: analytics.NewProperties().
18+
Set("text", "Lorem Ipsum is simply dummy text of the printing and typesetting industry."),
19+
})
1720

18-
// Flushes any queued messages and closes the client.
19-
client.Close()
21+
// Flushes any queued messages and closes the client.
22+
client.Close()
23+
}
24+
2025
}

example/example-3.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"github.com/rudderlabs/analytics-go"
5+
)
6+
7+
func main() {
8+
// Instantiates a client to use send messages to the Rudder API.
9+
// User your WRITE KEY in below placeholder "RUDDER WRITE KEY"
10+
client, _ := analytics.NewWithConfig("WRITE-KEY", "DATA-PLANE-URL",
11+
analytics.Config{
12+
MaxMessageBytes: 35000, //new field to control the max message size
13+
})
14+
15+
if client!= nil{
16+
// Enqueues a track event that will be sent asynchronously.
17+
client.Enqueue(analytics.Track{
18+
UserId: "test-user",
19+
Event: "test-snippet",
20+
Properties: analytics.NewProperties().
21+
Set("text", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with cently ng and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s"),
22+
})
23+
24+
// Flushes any queued messages and closes the client.
25+
client.Close()
26+
}
27+
28+
}

example/vendor/github.com/rudderlabs/analytics-go/analytics.go

Lines changed: 140 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)