Initial Bus implementation
This commit is contained in:
50
bus.go
Normal file
50
bus.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type Bus struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
chans map[string][]chan Object
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBus() *Bus {
|
||||||
|
return &Bus{
|
||||||
|
chans: map[string][]chan Object{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bus) Announce(obj Object) {
|
||||||
|
key := ObjectKey(obj)
|
||||||
|
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
chans := b.chans[key]
|
||||||
|
newChans := []chan Object{}
|
||||||
|
|
||||||
|
for _, ch := range chans {
|
||||||
|
select {
|
||||||
|
case ch <- obj:
|
||||||
|
newChans = append(newChans, ch)
|
||||||
|
default:
|
||||||
|
close(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(chans) != len(newChans) {
|
||||||
|
b.chans[key] = newChans
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bus) Subscribe(obj Object) chan Object {
|
||||||
|
key := ObjectKey(obj)
|
||||||
|
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
ch := make(chan Object, 100)
|
||||||
|
|
||||||
|
b.chans[key] = append(b.chans[key], ch)
|
||||||
|
|
||||||
|
return ch
|
||||||
|
}
|
||||||
25
main.go
25
main.go
@@ -5,20 +5,23 @@ import "fmt"
|
|||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
store := NewStore("foo")
|
store := NewStore("foo")
|
||||||
|
|
||||||
out := &Template{
|
out := &Template{
|
||||||
Id: uuid.NewString(),
|
Id: uuid.NewString(),
|
||||||
Test: "round trip",
|
Test: "round trip",
|
||||||
}
|
}
|
||||||
|
|
||||||
store.Write(out)
|
store.Write(out)
|
||||||
|
|
||||||
in := &Template{
|
in := &Template{
|
||||||
Id: out.Id,
|
Id: out.Id,
|
||||||
}
|
}
|
||||||
|
|
||||||
store.Read(in)
|
store.Read(in)
|
||||||
|
|
||||||
fmt.Printf("%+v\n", in)
|
fmt.Printf("%+v\n", in)
|
||||||
|
|
||||||
|
bus := NewBus()
|
||||||
|
bus.Announce(in)
|
||||||
}
|
}
|
||||||
|
|||||||
17
object.go
17
object.go
@@ -1,18 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "encoding/hex"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type Object interface {
|
type Object interface {
|
||||||
GetType() string
|
GetType() string
|
||||||
GetId() string
|
GetId() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ObjectSafeId(obj Object) string {
|
||||||
|
return hex.EncodeToString([]byte(obj.GetId()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ObjectKey(obj Object) string {
|
func ObjectKey(obj Object) string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf("%s:%s", obj.GetType(), ObjectSafeId(obj))
|
||||||
"%d:%s:%d:%s",
|
|
||||||
len(obj.GetType()),
|
|
||||||
obj.GetType(),
|
|
||||||
len(obj.GetId()),
|
|
||||||
obj.GetId(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
89
store.go
89
store.go
@@ -1,74 +1,73 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "encoding/hex"
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "os"
|
import "os"
|
||||||
import "path/filepath"
|
import "path/filepath"
|
||||||
|
|
||||||
type Store struct {
|
type Store struct {
|
||||||
root string
|
root string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStore(root string) *Store {
|
func NewStore(root string) *Store {
|
||||||
return &Store{
|
return &Store{
|
||||||
root: root,
|
root: root,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Write(obj Object) error {
|
func (s *Store) Write(obj Object) error {
|
||||||
dir := filepath.Join(s.root, obj.GetType())
|
dir := filepath.Join(s.root, obj.GetType())
|
||||||
filename := hex.EncodeToString([]byte(obj.GetId()))
|
filename := ObjectSafeId(obj)
|
||||||
|
|
||||||
err := os.MkdirAll(dir, 0700)
|
err := os.MkdirAll(dir, 0700)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, err := os.CreateTemp(dir, fmt.Sprintf("%s.*", filename))
|
tmp, err := os.CreateTemp(dir, fmt.Sprintf("%s.*", filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer tmp.Close()
|
defer tmp.Close()
|
||||||
|
|
||||||
enc := json.NewEncoder(tmp)
|
enc := json.NewEncoder(tmp)
|
||||||
enc.SetEscapeHTML(false)
|
enc.SetEscapeHTML(false)
|
||||||
|
|
||||||
err = enc.Encode(obj)
|
err = enc.Encode(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = tmp.Close()
|
err = tmp.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Rename(tmp.Name(), filepath.Join(dir, filename))
|
err = os.Rename(tmp.Name(), filepath.Join(dir, filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Read(obj Object) error {
|
func (s *Store) Read(obj Object) error {
|
||||||
dir := filepath.Join(s.root, obj.GetType())
|
dir := filepath.Join(s.root, obj.GetType())
|
||||||
filename := hex.EncodeToString([]byte(obj.GetId()))
|
filename := ObjectSafeId(obj)
|
||||||
|
|
||||||
fh, err := os.Open(filepath.Join(dir, filename))
|
fh, err := os.Open(filepath.Join(dir, filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fh.Close()
|
defer fh.Close()
|
||||||
|
|
||||||
dec := json.NewDecoder(fh)
|
dec := json.NewDecoder(fh)
|
||||||
dec.DisallowUnknownFields()
|
dec.DisallowUnknownFields()
|
||||||
|
|
||||||
err = dec.Decode(obj)
|
err = dec.Decode(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type Template struct {
|
type Template struct {
|
||||||
Id string
|
Id string
|
||||||
Test string
|
Test string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Template) GetType() string {
|
func (t *Template) GetType() string {
|
||||||
return "template"
|
return "template"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Template) GetId() string {
|
func (t *Template) GetId() string {
|
||||||
return t.Id
|
return t.Id
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user