Skip to content

support custom split func #166

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 2 commits 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
13 changes: 10 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package sse

import (
"bufio"
"bytes"
"context"
"encoding/base64"
Expand Down Expand Up @@ -32,6 +33,12 @@ func ClientMaxBufferSize(s int) func(c *Client) {
}
}

func SplitFunc(f bufio.SplitFunc) func(c *Client) {
return func(c *Client) {
c.split = f
}
}

// ConnCallback defines a function to be called on a particular connection event
type ConnCallback func(c *Client)

Expand All @@ -55,6 +62,7 @@ type Client struct {
mu sync.Mutex
EncodingBase64 bool
Connected bool
split bufio.SplitFunc
}

// NewClient creates a new client
Expand Down Expand Up @@ -97,7 +105,7 @@ func (c *Client) SubscribeWithContext(ctx context.Context, stream string, handle
}
defer resp.Body.Close()

reader := NewEventStreamReader(resp.Body, c.maxBufferSize)
reader := NewEventStreamReader(resp.Body, c.maxBufferSize, c.split)
eventChan, errorChan := c.startReadLoop(reader)

for {
Expand Down Expand Up @@ -155,7 +163,7 @@ func (c *Client) SubscribeChanWithContext(ctx context.Context, stream string, ch
connected = true
}

reader := NewEventStreamReader(resp.Body, c.maxBufferSize)
reader := NewEventStreamReader(resp.Body, c.maxBufferSize, c.split)
eventChan, errorChan := c.startReadLoop(reader)

for {
Expand Down Expand Up @@ -387,4 +395,3 @@ func trimHeader(size int, data []byte) []byte {
data = data[:len(data)-1]
}
return data
}
29 changes: 16 additions & 13 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,29 @@ type EventStreamReader struct {
}

// NewEventStreamReader creates an instance of EventStreamReader.
func NewEventStreamReader(eventStream io.Reader, maxBufferSize int) *EventStreamReader {
func NewEventStreamReader(eventStream io.Reader, maxBufferSize int, split bufio.SplitFunc) *EventStreamReader {
scanner := bufio.NewScanner(eventStream)
initBufferSize := minPosInt(4096, maxBufferSize)
scanner.Buffer(make([]byte, initBufferSize), maxBufferSize)

split := func(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
if split == nil {
split = func(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}

// We have a full event payload to parse.
if i, nlen := containsDoubleNewline(data); i >= 0 {
return i + nlen, data[0:i], nil
}
// If we're at EOF, we have all of the data.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}

// We have a full event payload to parse.
if i, nlen := containsDoubleNewline(data); i >= 0 {
return i + nlen, data[0:i], nil
}
// If we're at EOF, we have all of the data.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
// Set the split function for the scanning operation.
scanner.Split(split)
Expand Down