-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: move sse encoding and typed sync/Pool wrapped into util pkg
- Loading branch information
Showing
5 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package pool | ||
|
||
import "sync" | ||
|
||
// Pool is a sync.Pool wrapped for type-safety | ||
type Pool[T any] struct { | ||
p sync.Pool | ||
} | ||
|
||
func NewPool[T any](newFn func() T) *Pool[T] { | ||
return &Pool[T]{ | ||
sync.Pool{ | ||
New: func() any { return newFn() }, | ||
}, | ||
} | ||
} | ||
|
||
func (p *Pool[T]) Get() T { | ||
return p.p.Get().(T) | ||
} | ||
|
||
func (p *Pool[T]) Put(v T) { | ||
p.p.Put(v) | ||
} | ||
|
||
type Resetable interface { | ||
Reset() | ||
} | ||
|
||
// ResetPool is a sync.Pool wrapped with a generic Resetable interface, the pool calls | ||
// Reset before returning an item to the pool. | ||
type ResetPool[T Resetable] struct { | ||
*Pool[T] | ||
} | ||
|
||
func NewResetPool[T Resetable](newFn func() T) *ResetPool[T] { | ||
return &ResetPool[T]{NewPool(newFn)} | ||
} | ||
|
||
func (p *ResetPool[T]) Put(v T) { | ||
v.Reset() | ||
p.p.Put(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package sse | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/R-a-dio/valkyrie/util/pool" | ||
) | ||
|
||
type Event struct { | ||
ID []byte | ||
Name string | ||
Retry time.Duration | ||
Data []byte | ||
} | ||
|
||
var bufferPool = pool.NewResetPool(func() *bytes.Buffer { return new(bytes.Buffer) }) | ||
|
||
func (e Event) Encode() []byte { | ||
b := bufferPool.Get() | ||
defer bufferPool.Put(b) | ||
|
||
if e.ID != nil { | ||
b.WriteString("id: ") | ||
b.Write(e.ID) | ||
b.WriteString("\n") | ||
} | ||
if e.Name != "" { | ||
b.WriteString("event: ") | ||
b.WriteString(e.Name) | ||
b.WriteString("\n") | ||
} | ||
if e.Retry != 0 { | ||
b.WriteString("retry: ") | ||
b.WriteString(strconv.Itoa(int(e.Retry.Milliseconds()))) | ||
b.WriteString("\n") | ||
} | ||
if e.Data != nil { | ||
for _, line := range bytes.Split(e.Data, []byte("\n")) { | ||
if len(line) == 0 { | ||
b.WriteString("data\n") | ||
} else { | ||
b.WriteString("data: ") | ||
b.Write(line) | ||
b.WriteString("\n") | ||
} | ||
} | ||
} | ||
b.WriteString("\n") | ||
|
||
return b.Bytes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package v1 | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type eventCase struct { | ||
desc string | ||
e Event | ||
expect string | ||
} | ||
|
||
var eventEncodeCases = []eventCase{ | ||
{ | ||
"simple message", | ||
Event{Name: EventQueue, Data: []byte("queue information")}, | ||
"event: queue\ndata: queue information\n\n", | ||
}, { | ||
"simple newlines", | ||
Event{Name: EventThread, Data: []byte("some data\nwith\nnewlines")}, | ||
"event: thread\ndata: some data\ndata: with\ndata: newlines\n\n", | ||
}, { | ||
"double newline", | ||
Event{Name: EventMetadata, Data: []byte("some data\n\nwith newlines")}, | ||
"event: metadata\ndata: some data\ndata\ndata: with newlines\n\n", | ||
}, { | ||
"encode id", | ||
Event{ID: []byte("50")}, | ||
"id: 50\n\n", | ||
}, { | ||
"encode retry", | ||
Event{Retry: time.Second * 10}, | ||
"retry: 10000\n\n", | ||
}, { | ||
"encode everything", | ||
Event{ID: []byte("100"), Name: EventMetadata, Retry: time.Second * 50, Data: []byte("some data\nand a newline")}, | ||
"id: 100\nevent: metadata\nretry: 50000\ndata: some data\ndata: and a newline\n\n", | ||
}, | ||
} | ||
|
||
func TestEventEncode(t *testing.T) { | ||
for _, c := range eventEncodeCases { | ||
t.Run(c.desc, func(t *testing.T) { | ||
result := c.e.Encode() | ||
expect := []byte(c.expect) | ||
|
||
if !bytes.Equal(result, expect) { | ||
t.Errorf("%#v != %#v\n", string(result), string(expect)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkEventEncodeSimple(b *testing.B) { | ||
e := Event{Name: EventMetadata, Data: []byte("some data\nand a newline")} | ||
for i := 0; i < b.N; i++ { | ||
e.Encode() | ||
} | ||
} | ||
|
||
func BenchmarkEventEncodeEverything(b *testing.B) { | ||
e := Event{ID: []byte("100"), Name: EventMetadata, Retry: time.Second * 50, Data: []byte("some data\nand a newline")} | ||
for i := 0; i < b.N; i++ { | ||
e.Encode() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters