-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce GobSink, a Sink using Gob encoding
Signed-off-by: Alejandro Mery <[email protected]>
- Loading branch information
Showing
3 changed files
with
120 additions
and
6 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
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,111 @@ | ||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
"time" | ||
|
||
"github.com/darvaza-proxy/core" | ||
) | ||
|
||
var ( | ||
_ Sink = (*GobSink[any])(nil) | ||
) | ||
|
||
// GobSink is a Sink using generics for type safety and Gob | ||
// for encoding | ||
type GobSink[T any] struct { | ||
out *T | ||
e time.Time | ||
bytes []byte | ||
} | ||
|
||
// Bytes returns the Gob encoded representation of the object | ||
// in the Sink | ||
func (sink *GobSink[T]) Bytes() []byte { | ||
// TODO: do we need to copy them? | ||
return sink.bytes | ||
} | ||
|
||
// Expire tells when this object will be evicted from the Cache | ||
func (sink *GobSink[T]) Expire() time.Time { | ||
return sink.e | ||
} | ||
|
||
// SetBytes sets the object of the GobSink and its expiration time | ||
// from a Gob encoded byte array | ||
func (sink *GobSink[T]) SetBytes(v []byte, e time.Time) error { | ||
buf := bytes.NewBuffer(v) | ||
dec := gob.NewDecoder(buf) | ||
|
||
if err := dec.Decode(sink.out); err != nil { | ||
// failed to decode | ||
sink.Reset() | ||
return err | ||
} | ||
|
||
// store | ||
sink.bytes = make([]byte, len(v)) | ||
sink.e = e | ||
copy(sink.bytes, v) | ||
return nil | ||
} | ||
|
||
// SetString isn't supported by GobSink, but its needed to satisfy | ||
// the Sink interface | ||
func (sink *GobSink[T]) SetString(string, time.Time) error { | ||
sink.Reset() | ||
return ErrInvalid | ||
} | ||
|
||
// SetValue sets the object of the GobSink and its expiration time | ||
func (sink *GobSink[T]) SetValue(v any, e time.Time) error { | ||
var buf bytes.Buffer | ||
|
||
p, ok := v.(*T) | ||
if !ok { | ||
return ErrInvalid | ||
} | ||
|
||
enc := gob.NewEncoder(&buf) | ||
if err := enc.Encode(p); err != nil { | ||
sink.Reset() | ||
return err | ||
} | ||
|
||
sink.bytes = buf.Bytes() | ||
sink.e = e | ||
*sink.out = *p | ||
return nil | ||
} | ||
|
||
// Value gives a copy of the stored object | ||
func (sink *GobSink[T]) Value() T { | ||
if sink.out == nil { | ||
var zero T | ||
return zero | ||
} | ||
return *sink.out | ||
} | ||
|
||
// Reset clears everything but the type pointer | ||
// assigned during creation | ||
func (sink *GobSink[T]) Reset() { | ||
sink.bytes = []byte{} | ||
sink.e = time.Time{} | ||
|
||
if sink.out != nil { | ||
var zero T | ||
*sink.out = zero | ||
} | ||
} | ||
|
||
// NewGobSink creates a new Sink using Gob as encoding | ||
// and stores the object in the provided pointer | ||
func NewGobSink[T any](out *T) *GobSink[T] { | ||
if out == nil { | ||
var zero T | ||
core.Panicf("NewGobSink[%T]: output can not be nil", zero) | ||
} | ||
return &GobSink[T]{out: out} | ||
} |