Skip to content

Commit

Permalink
Merge pull request #71 from pusher/allow-bigger-requests
Browse files Browse the repository at this point in the history
Allow message limit to be overridden for dedicate cluster customers
  • Loading branch information
Luís Fonseca authored Sep 2, 2020
2 parents 81098b9 + 8557c5a commit 61fc56d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
4.0.4 / 2020-09-02
==================
* Allow message size to be overridden for dedicate cluster customers (PR [#63](https://github.com/pusher/pusher-http-go/pull/71))

4.0.3 / 2020-07-28
==================
* Added library name and version in HTTP Header (PR [#62](https://github.com/pusher/pusher-http-go/pull/62))
Expand Down
7 changes: 4 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var pusherPathRegex = regexp.MustCompile("^/apps/([0-9]+)$")
var maxTriggerableChannels = 100

const (
libraryVersion = "4.0.3"
libraryVersion = "4.0.4"
libraryName = "pusher-http-go"
)

Expand Down Expand Up @@ -58,6 +58,7 @@ type Client struct {
HTTPClient *http.Client
EncryptionMasterKey string // deprecated
EncryptionMasterKeyBase64 string // for E2E
OverrideMaxMessagePayloadKB int // set the agreed Pusher message limit increase
validatedEncryptionMasterKey *[]byte // parsed key for use
}

Expand Down Expand Up @@ -200,7 +201,7 @@ func (c *Client) trigger(channels []string, eventName string, data interface{},
return err
}

payload, err := encodeTriggerBody(channels, eventName, data, socketID, masterKey)
payload, err := encodeTriggerBody(channels, eventName, data, socketID, masterKey, c.OverrideMaxMessagePayloadKB)
if err != nil {
return err
}
Expand Down Expand Up @@ -250,7 +251,7 @@ func (c *Client) TriggerBatch(batch []Event) error {
return keyErr
}

payload, err := encodeTriggerBatchBody(batch, masterKey)
payload, err := encodeTriggerBatchBody(batch, masterKey, c.OverrideMaxMessagePayloadKB)
if err != nil {
return err
}
Expand Down
25 changes: 25 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,31 @@ func TestDataSizeValidation(t *testing.T) {
err := client.Trigger("channel", "event", data)

assert.EqualError(t, err, "Event payload exceeded maximum size (20481 bytes is too much)")

err = client.TriggerBatch([]Event{
{"channel", "event", data, nil},
})
assert.EqualError(t, err, "Data of the event #0 in batch, exceeded maximum size (20481 bytes is too much)")
}

func TestDataSizeOverridenValidation(t *testing.T) {
client := Client{AppID: "id", Key: "key", Secret: "secret", OverrideMaxMessagePayloadKB: 80}
data := strings.Repeat("a", 81920)
err := client.Trigger("channel", "event", data)
assert.NotContains(t, err.Error(), "\"Event payload exceeded maximum size (81920 bytes is too much)")
err = client.TriggerBatch([]Event{
{"channel", "event", data, nil},
})
assert.NotContains(t, err.Error(), "Data of the event #0 in batch, exceeded maximum size (81920 bytes is too much)")

data = strings.Repeat("a", 81921)
err = client.Trigger("channel", "event", data)
assert.EqualError(t, err, "Event payload exceeded maximum size (81921 bytes is too much)")

err = client.TriggerBatch([]Event{
{"channel", "event", data, nil},
})
assert.EqualError(t, err, "Data of the event #0 in batch, exceeded maximum size (81921 bytes is too much)")
}

func TestInitialisationFromURL(t *testing.T) {
Expand Down
39 changes: 32 additions & 7 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"fmt"
)

// maxEventPayloadSize indicates the max size allowed for the data content (payload) of each event
const maxEventPayloadSize = 20480 // on dedicated clusters we may allow 2x the usual limit
// defaultMaxEventPayloadSizeKB indicates the max size allowed for the data content
// (payload) of each event, unless an override is present in the client
const defaultMaxEventPayloadSizeKB = 10

type batchEvent struct {
Channel string `json:"channel"`
Expand All @@ -26,7 +27,14 @@ type eventPayload struct {
SocketID *string `json:"socket_id,omitempty"`
}

func encodeTriggerBody(channels []string, event string, data interface{}, socketID *string, encryptionKey []byte) ([]byte, error) {
func encodeTriggerBody(
channels []string,
event string,
data interface{},
socketID *string,
encryptionKey []byte,
overrideMaxMessagePayloadKB int,
) ([]byte, error) {
dataBytes, err := encodeEventData(data)
if err != nil {
return nil, err
Expand All @@ -37,7 +45,14 @@ func encodeTriggerBody(channels []string, event string, data interface{}, socket
} else {
payloadData = string(dataBytes)
}
if len(payloadData) > maxEventPayloadSize {

eventExceedsMaximumSize := false
if overrideMaxMessagePayloadKB == 0 {
eventExceedsMaximumSize = len(payloadData) > defaultMaxEventPayloadSizeKB*1024
} else {
eventExceedsMaximumSize = len(payloadData) > overrideMaxMessagePayloadKB*1024
}
if eventExceedsMaximumSize {
return nil, errors.New(fmt.Sprintf("Event payload exceeded maximum size (%d bytes is too much)", len(payloadData)))
}
return json.Marshal(&eventPayload{
Expand All @@ -48,7 +63,11 @@ func encodeTriggerBody(channels []string, event string, data interface{}, socket
})
}

func encodeTriggerBatchBody(batch []Event, encryptionKey []byte) ([]byte, error) {
func encodeTriggerBatchBody(
batch []Event,
encryptionKey []byte,
overrideMaxMessagePayloadKB int,
) ([]byte, error) {
batchEvents := make([]batchEvent, len(batch))
for idx, e := range batch {
var stringifyedDataBytes string
Expand All @@ -61,8 +80,14 @@ func encodeTriggerBatchBody(batch []Event, encryptionKey []byte) ([]byte, error)
} else {
stringifyedDataBytes = string(dataBytes)
}
if len(stringifyedDataBytes) > maxEventPayloadSize {
return nil, fmt.Errorf("Data of the event #%d in batch, must be smaller than 10kb", idx)
eventExceedsMaximumSize := false
if overrideMaxMessagePayloadKB == 0 {
eventExceedsMaximumSize = len(stringifyedDataBytes) > defaultMaxEventPayloadSizeKB*1024
} else {
eventExceedsMaximumSize = len(stringifyedDataBytes) > overrideMaxMessagePayloadKB*1024
}
if eventExceedsMaximumSize {
return nil, fmt.Errorf("Data of the event #%d in batch, exceeded maximum size (%d bytes is too much)", idx, len(stringifyedDataBytes))
}
newBatchEvent := batchEvent{
Channel: e.Channel,
Expand Down

0 comments on commit 61fc56d

Please sign in to comment.