2022-08-23 21:24:13 -06:00
|
|
|
package peerfinder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-20 10:28:50 -07:00
|
|
|
"fmt"
|
2022-12-19 10:50:38 -07:00
|
|
|
"sync/atomic"
|
2022-11-20 10:28:50 -07:00
|
|
|
"time"
|
2022-08-23 21:24:13 -06:00
|
|
|
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev"
|
|
|
|
"go.sour.is/ev/internal/lg"
|
|
|
|
"go.sour.is/ev/pkg/es/event"
|
|
|
|
"go.sour.is/ev/pkg/locker"
|
2022-11-23 13:51:55 -07:00
|
|
|
"go.uber.org/multierr"
|
2022-08-23 21:24:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-20 10:28:50 -07:00
|
|
|
aggInfo = "pf-info"
|
|
|
|
queueRequests = "pf-requests"
|
|
|
|
queueResults = "pf-results"
|
|
|
|
initVersion = "1.2.1"
|
2022-08-23 21:24:13 -06:00
|
|
|
)
|
|
|
|
|
2022-11-20 10:28:50 -07:00
|
|
|
func aggRequest(id string) string { return "pf-request-" + id }
|
|
|
|
func aggPeer(id string) string { return "pf-peer-" + id }
|
|
|
|
|
2022-08-23 21:24:13 -06:00
|
|
|
type service struct {
|
2023-01-09 11:30:02 -07:00
|
|
|
es *ev.EventStore
|
2022-11-20 10:28:50 -07:00
|
|
|
statusURL string
|
2022-10-30 09:18:08 -06:00
|
|
|
|
2022-11-20 10:28:50 -07:00
|
|
|
state *locker.Locked[state]
|
2022-12-19 10:50:38 -07:00
|
|
|
up atomic.Bool
|
2022-11-23 13:51:55 -07:00
|
|
|
stop func()
|
2022-10-30 09:18:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type state struct {
|
2022-11-23 13:51:55 -07:00
|
|
|
peers map[string]*Peer
|
|
|
|
requests map[string]*Request
|
2022-08-23 21:24:13 -06:00
|
|
|
}
|
|
|
|
|
2023-01-09 11:30:02 -07:00
|
|
|
func New(ctx context.Context, es *ev.EventStore, statusURL string) (*service, error) {
|
2022-09-06 10:35:14 -06:00
|
|
|
ctx, span := lg.Span(ctx)
|
2022-08-23 21:24:13 -06:00
|
|
|
defer span.End()
|
|
|
|
|
2022-11-20 10:28:50 -07:00
|
|
|
loadTemplates()
|
|
|
|
|
2022-10-30 09:18:08 -06:00
|
|
|
if err := event.Register(ctx, &RequestSubmitted{}, &ResultSubmitted{}, &VersionChanged{}); err != nil {
|
2022-08-23 21:24:13 -06:00
|
|
|
span.RecordError(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
svc := &service{
|
|
|
|
es: es,
|
|
|
|
statusURL: statusURL,
|
|
|
|
state: locker.New(&state{
|
|
|
|
peers: make(map[string]*Peer),
|
|
|
|
requests: make(map[string]*Request),
|
|
|
|
})}
|
2022-08-23 21:24:13 -06:00
|
|
|
|
|
|
|
return svc, nil
|
|
|
|
}
|
2022-12-19 10:50:38 -07:00
|
|
|
func (s *service) loadResult(ctx context.Context, request *Request) (*Request, error) {
|
|
|
|
if request == nil {
|
|
|
|
return request, nil
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
return request, s.state.Modify(ctx, func(ctx context.Context, t *state) error {
|
2022-12-19 10:50:38 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
for i := range request.Responses {
|
2022-12-19 10:50:38 -07:00
|
|
|
res := request.Responses[i]
|
2022-11-23 13:51:55 -07:00
|
|
|
if peer, ok := t.peers[res.PeerID]; ok {
|
|
|
|
res.Peer = peer
|
|
|
|
res.Peer.ID = ""
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2022-11-23 13:51:55 -07:00
|
|
|
func (s *service) Run(ctx context.Context) (err error) {
|
|
|
|
var errs error
|
2022-11-20 10:28:50 -07:00
|
|
|
|
|
|
|
ctx, span := lg.Span(ctx)
|
|
|
|
defer span.End()
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
ctx, s.stop = context.WithCancel(ctx)
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
subReq, e := s.es.EventStream().Subscribe(ctx, queueRequests, 0)
|
|
|
|
errs = multierr.Append(errs, e)
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
subRes, e := s.es.EventStream().Subscribe(ctx, queueResults, 0)
|
|
|
|
errs = multierr.Append(errs, e)
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
defer func() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
|
|
defer cancel()
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
err = multierr.Combine(subReq.Close(ctx), subRes.Close(ctx), err)
|
|
|
|
}()
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
if errs != nil {
|
|
|
|
return errs
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2022-11-23 13:51:55 -07:00
|
|
|
var events event.Events
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case ok := <-subReq.Recv(ctx):
|
|
|
|
if ok {
|
|
|
|
events, err = subReq.Events(ctx)
|
|
|
|
}
|
|
|
|
case ok := <-subRes.Recv(ctx):
|
|
|
|
if ok {
|
|
|
|
events, err = subRes.Events(ctx)
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
s.state.Modify(ctx, func(ctx context.Context, state *state) error {
|
|
|
|
return state.ApplyEvents(events)
|
|
|
|
})
|
|
|
|
events = events[:0]
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-23 13:51:55 -07:00
|
|
|
func (s *service) Stop(ctx context.Context) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
err = fmt.Errorf("PANIC: %v", p)
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
2022-11-23 13:51:55 -07:00
|
|
|
}()
|
2022-11-20 10:28:50 -07:00
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
s.stop()
|
|
|
|
return err
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
|
|
|
|
2022-11-23 13:51:55 -07:00
|
|
|
func (s *state) ApplyEvents(events event.Events) error {
|
|
|
|
for _, e := range events {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *RequestSubmitted:
|
|
|
|
if _, ok := s.requests[e.RequestID()]; !ok {
|
|
|
|
s.requests[e.RequestID()] = &Request{}
|
2022-11-20 10:28:50 -07:00
|
|
|
}
|
2022-11-23 13:51:55 -07:00
|
|
|
s.requests[e.RequestID()].ApplyEvent(e)
|
|
|
|
case *ResultSubmitted:
|
|
|
|
if _, ok := s.requests[e.RequestID]; !ok {
|
|
|
|
s.requests[e.RequestID] = &Request{}
|
|
|
|
}
|
|
|
|
s.requests[e.RequestID].ApplyEvent(e)
|
2022-12-19 10:50:38 -07:00
|
|
|
case *RequestTruncated:
|
|
|
|
delete(s.requests, e.RequestID)
|
2022-08-23 21:24:13 -06:00
|
|
|
}
|
|
|
|
}
|
2022-10-30 09:18:08 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Projector(e event.Event) []event.Event {
|
|
|
|
m := e.EventMeta()
|
|
|
|
streamID := m.StreamID
|
|
|
|
streamPos := m.Position
|
|
|
|
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *RequestSubmitted:
|
|
|
|
e1 := event.NewPtr(streamID, streamPos)
|
2022-11-20 10:28:50 -07:00
|
|
|
event.SetStreamID(aggRequest(e.RequestID()), e1)
|
2022-10-30 09:18:08 -06:00
|
|
|
|
|
|
|
return []event.Event{e1}
|
|
|
|
case *ResultSubmitted:
|
|
|
|
e1 := event.NewPtr(streamID, streamPos)
|
2022-11-20 10:28:50 -07:00
|
|
|
event.SetStreamID(aggRequest(e.RequestID), e1)
|
2022-10-30 09:18:08 -06:00
|
|
|
|
|
|
|
e2 := event.NewPtr(streamID, streamPos)
|
2022-11-20 10:28:50 -07:00
|
|
|
event.SetStreamID(aggPeer(e.PeerID), e2)
|
2022-10-30 09:18:08 -06:00
|
|
|
|
|
|
|
return []event.Event{e1, e2}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|