refactor: moving items around into related files/packages

This commit is contained in:
Jon Lundy
2022-11-23 13:51:55 -07:00
parent bbb45c8854
commit 7315759b20
23 changed files with 1320 additions and 1097 deletions

View File

@@ -28,7 +28,7 @@ type EventLogWithUpdate interface {
}
type Subscription interface {
Recv(context.Context) bool
Recv(context.Context) <-chan bool
Events(context.Context) (event.Events, error)
Close(context.Context) error
}

View File

@@ -4,6 +4,7 @@ package streamer
import (
"context"
"fmt"
"sync"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
@@ -67,6 +68,7 @@ func (s *streamer) Subscribe(ctx context.Context, streamID string, start int64)
if err != nil {
return nil, err
}
sub := &subscription{topic: streamID, events: events}
sub.position = locker.New(&position{
idx: start,
@@ -86,12 +88,14 @@ func (s *streamer) Send(ctx context.Context, streamID string, events event.Event
return s.state.Modify(ctx, func(ctx context.Context, state *state) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent(fmt.Sprint("subscribers=", len(state.subscribers[streamID])))
for _, sub := range state.subscribers[streamID] {
err := sub.position.Modify(ctx, func(ctx context.Context, position *position) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.SetAttributes(
attribute.String("streamID", streamID),
attribute.Int64("actualPosition", int64(events.Last().EventMeta().ActualPosition)),
@@ -106,6 +110,7 @@ func (s *streamer) Send(ctx context.Context, streamID string, events event.Event
position.link = trace.LinkFromContext(ctx, attribute.String("src", "event"))
position.wait = nil
}
return nil
})
if err != nil {
@@ -210,56 +215,65 @@ type subscription struct {
events driver.EventLog
unsub func(context.Context) error
once sync.Once
}
func (s *subscription) Recv(ctx context.Context) bool {
func (s *subscription) Recv(ctx context.Context) <-chan bool {
ctx, span := lg.Span(ctx)
defer span.End()
var wait func(context.Context) bool
done := make(chan bool)
err := s.position.Modify(ctx, func(ctx context.Context, position *position) error {
_, span := lg.Span(ctx)
defer span.End()
go func() {
var wait func(context.Context) bool
defer close(done)
if position.size == es.AllEvents {
return nil
}
if position.size == 0 {
position.wait = make(chan struct{})
wait = func(ctx context.Context) bool {
ctx, span := lg.Span(ctx)
defer span.End()
err := s.position.Modify(ctx, func(ctx context.Context, position *position) error {
_, span := lg.Span(ctx)
defer span.End()
select {
case <-position.wait:
if position.link.SpanContext.IsValid() {
_, span := lg.Span(ctx, trace.WithLinks(position.link))
span.AddEvent("recv event")
span.End()
position.link = trace.Link{}
if position.size == es.AllEvents {
return nil
}
if position.size == 0 {
position.wait = make(chan struct{})
wait = func(ctx context.Context) bool {
ctx, span := lg.Span(ctx)
defer span.End()
select {
case <-position.wait:
if position.link.SpanContext.IsValid() {
_, span := lg.Span(ctx, trace.WithLinks(position.link))
span.AddEvent("recv event")
span.End()
position.link = trace.Link{}
}
return true
case <-ctx.Done():
return false
}
return true
case <-ctx.Done():
return false
}
}
position.idx += position.size
position.size = 0
return nil
})
if err != nil {
done <- false
return
}
position.idx += position.size
position.size = 0
return nil
})
if err != nil {
return false
}
if wait != nil {
return wait(ctx)
}
if wait != nil {
done <- wait(ctx)
return
}
return true
done <- true
}()
return done
}
func (s *subscription) Events(ctx context.Context) (event.Events, error) {
ctx, span := lg.Span(ctx)
@@ -293,5 +307,12 @@ func (s *subscription) Close(ctx context.Context) error {
ctx, span := lg.Span(ctx)
defer span.End()
return s.unsub(ctx)
if s == nil || s.unsub == nil {
return nil
}
var err error
s.once.Do(func() { err = s.unsub(ctx) })
return err
}

View File

@@ -23,6 +23,7 @@ type contextKey struct {
var esKey = contextKey{"event-store"}
func (es *EventStore) IsResolver() {}
func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error) {
ctx, span := lg.Span(ctx)
defer span.End()
@@ -51,10 +52,10 @@ func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.P
return &gql.Connection{
Paging: &gql.PageInfo{
Next: lis.Last().EventMeta().Position < last,
Prev: lis.First().EventMeta().Position > first,
Begin: lis.First().EventMeta().Position,
End: lis.Last().EventMeta().Position,
Next: lis.Last().EventMeta().ActualPosition < last,
Prev: lis.First().EventMeta().ActualPosition > first,
Begin: lis.First().EventMeta().ActualPosition,
End: lis.Last().EventMeta().ActualPosition,
},
Edges: edges,
}, nil
@@ -91,7 +92,7 @@ func (e *EventStore) EventAdded(ctx context.Context, streamID string, after int6
}()
}
for sub.Recv(ctx) {
for <-sub.Recv(ctx) {
events, err := sub.Events(ctx)
if err != nil {
span.RecordError(err)
@@ -120,7 +121,6 @@ func (es *EventStore) TruncateStream(ctx context.Context, streamID string, index
err := es.Truncate(ctx, streamID, index)
return err == nil, err
}
func (*EventStore) RegisterHTTP(*http.ServeMux) {}
func (e *EventStore) GetMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {