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
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
23 changed files with 1320 additions and 1097 deletions

View File

@ -2,30 +2,14 @@ package gql
import ( import (
"context" "context"
"fmt"
"net/http"
"os"
"reflect"
"runtime/debug"
"time"
"github.com/99designs/gqlgen/graphql" "github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/gorilla/websocket"
"github.com/ravilushqa/otelgqlgen"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/sour-is/ev/app/gql/graphiql"
"github.com/sour-is/ev/app/gql/playground"
"github.com/sour-is/ev/app/msgbus" "github.com/sour-is/ev/app/msgbus"
"github.com/sour-is/ev/app/salty" "github.com/sour-is/ev/app/salty"
"github.com/sour-is/ev/internal/graph/generated" "github.com/sour-is/ev/internal/graph/generated"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es" "github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/gql" "github.com/sour-is/ev/pkg/gql"
"github.com/sour-is/ev/pkg/gql/resolver"
) )
type Resolver struct { type Resolver struct {
@ -34,36 +18,6 @@ type Resolver struct {
es.EventResolver es.EventResolver
} }
func New(ctx context.Context, resolvers ...interface{ RegisterHTTP(*http.ServeMux) }) (*Resolver, error) {
_, span := lg.Span(ctx)
defer span.End()
r := &Resolver{}
v := reflect.ValueOf(r)
v = reflect.Indirect(v)
noop := reflect.ValueOf(&noop{})
outer:
for _, idx := range reflect.VisibleFields(v.Type()) {
field := v.FieldByIndex(idx.Index)
for i := range resolvers {
rs := reflect.ValueOf(resolvers[i])
if field.IsNil() && rs.Type().Implements(field.Type()) {
span.AddEvent(fmt.Sprint("found ", field.Type().Name()))
field.Set(rs)
continue outer
}
}
span.AddEvent(fmt.Sprint("default ", field.Type().Name()))
field.Set(noop)
}
return r, nil
}
// Query returns generated.QueryResolver implementation. // Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return r } func (r *Resolver) Query() generated.QueryResolver { return r }
@ -73,53 +27,21 @@ func (r *Resolver) Mutation() generated.MutationResolver { return r }
// Subscription returns generated.SubscriptionResolver implementation. // Subscription returns generated.SubscriptionResolver implementation.
func (r *Resolver) Subscription() generated.SubscriptionResolver { return r } func (r *Resolver) Subscription() generated.SubscriptionResolver { return r }
// ChainMiddlewares will check all embeded resolvers for a GetMiddleware func and add to handler. // func (r *Resolver) isResolver() {}
func (r *Resolver) ChainMiddlewares(h http.Handler) http.Handler { func (r *Resolver) ExecutableSchema() graphql.ExecutableSchema {
v := reflect.ValueOf(r) // Get reflected value of *Resolver return generated.NewExecutableSchema(generated.Config{Resolvers: r})
v = reflect.Indirect(v) // Get the pointed value (returns a zero value on nil)
n := v.NumField() // Get number of fields to iterate over.
for i := 0; i < n; i++ {
f := v.Field(i)
if !f.CanInterface() { // Skip non-interface types.
continue
}
if iface, ok := f.Interface().(interface {
GetMiddleware() func(http.Handler) http.Handler
}); ok {
h = iface.GetMiddleware()(h) // Append only items that fulfill the interface.
}
}
return h
} }
func (r *Resolver) BaseResolver() resolver.IsResolver {
func (r *Resolver) RegisterHTTP(mux *http.ServeMux) { return &noop{}
gql := NewServer(generated.NewExecutableSchema(generated.Config{Resolvers: r}))
gql.SetRecoverFunc(NoopRecover)
gql.Use(otelgqlgen.Middleware())
mux.Handle("/graphiql", graphiql.Handler("GraphiQL playground", "/gql"))
mux.Handle("/gql", lg.Htrace(r.ChainMiddlewares(gql), "gql"))
mux.Handle("/playground", playground.Handler("GraphQL playground", "/gql"))
} }
type noop struct{} type noop struct{}
func NoopRecover(ctx context.Context, err interface{}) error {
if err, ok := err.(string); ok && err == "not implemented" {
return gqlerror.Errorf("not implemented")
}
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr)
debug.PrintStack()
return gqlerror.Errorf("internal system error")
}
var _ msgbus.MsgbusResolver = (*noop)(nil) var _ msgbus.MsgbusResolver = (*noop)(nil)
var _ salty.SaltyResolver = (*noop)(nil) var _ salty.SaltyResolver = (*noop)(nil)
var _ es.EventResolver = (*noop)(nil) var _ es.EventResolver = (*noop)(nil)
func (*noop) IsResolver() {}
func (*noop) CreateSaltyUser(ctx context.Context, nick string, pubkey string) (*salty.SaltyUser, error) { func (*noop) CreateSaltyUser(ctx context.Context, nick string, pubkey string) (*salty.SaltyUser, error) {
panic("not implemented") panic("not implemented")
} }
@ -141,35 +63,3 @@ func (*noop) EventAdded(ctx context.Context, streamID string, after int64) (<-ch
func (*noop) TruncateStream(ctx context.Context, streamID string, index int64) (bool, error) { func (*noop) TruncateStream(ctx context.Context, streamID string, index int64) (bool, error) {
panic("not implemented") panic("not implemented")
} }
func (*noop) RegisterHTTP(*http.ServeMux) {}
func NewServer(es graphql.ExecutableSchema) *handler.Server {
srv := handler.New(es)
srv.AddTransport(transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
switch r.Header.Get("Origin") {
case "https://ev.sour.is", "https://www.graphqlbin.com":
return true
default:
return false
}
},
},
KeepAlivePingInterval: 10 * time.Second,
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})
srv.SetQueryCache(lru.New(1000))
srv.Use(extension.Introspection{})
srv.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
return srv
}

View File

@ -1,8 +1,8 @@
extend type Query{ # extend type Query{
keys(namespace: String!) [String!]! # keys(namespace: String!) [String!]!
get(namespace: String! keys: [String!]) [String]! # get(namespace: String! keys: [String!]) [String]!
} # }
extend type Mutation{ # extend type Mutation{
set(namespace: String! key: String! value: String): Bool! # set(namespace: String! key: String! value: String): Bool!
} # }

View File

@ -35,7 +35,7 @@ type service struct {
type MsgbusResolver interface { type MsgbusResolver interface {
Posts(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error) Posts(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error)
PostAdded(ctx context.Context, streamID string, after int64) (<-chan *PostEvent, error) PostAdded(ctx context.Context, streamID string, after int64) (<-chan *PostEvent, error)
RegisterHTTP(mux *http.ServeMux) IsResolver()
} }
func New(ctx context.Context, es *es.EventStore) (*service, error) { func New(ctx context.Context, es *es.EventStore) (*service, error) {
@ -87,6 +87,7 @@ var upgrader = websocket.Upgrader{
}, },
} }
func (s *service) IsResolver() {}
func (s *service) RegisterHTTP(mux *http.ServeMux) { func (s *service) RegisterHTTP(mux *http.ServeMux) {
mux.Handle("/inbox/", lg.Htrace(http.StripPrefix("/inbox/", s), "inbox")) mux.Handle("/inbox/", lg.Htrace(http.StripPrefix("/inbox/", s), "inbox"))
} }
@ -197,7 +198,7 @@ func (r *service) PostAdded(ctx context.Context, streamID string, after int64) (
}() }()
} }
for sub.Recv(ctx) { for <-sub.Recv(ctx) {
events, err := sub.Events(ctx) events, err := sub.Events(ctx)
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)
@ -425,7 +426,7 @@ func (s *service) websocket(w http.ResponseWriter, r *http.Request) {
} }
span.AddEvent("start ws") span.AddEvent("start ws")
for sub.Recv(ctx) { for <-sub.Recv(ctx) {
events, err := sub.Events(ctx) events, err := sub.Events(ctx)
if err != nil { if err != nil {
break break

File diff suppressed because one or more lines are too long

View File

@ -30,29 +30,11 @@ body {
border-top: 1px solid #e5e5e5; border-top: 1px solid #e5e5e5;
} }
/* Customize container */ .panel-heading a {
@media (min-width: 768px) { color: white;
.container { font-weight: bold;
max-width: 730px;
}
} }
.container-narrow > hr { .container-narrow > hr {
margin: 30px 0; margin: 30px 0;
} }
/* Responsive: Portrait tablets and up */
@media screen and (min-width: 768px) {
/* Remove the padding we set earlier */
.header,
.footer {
padding-right: 0;
padding-left: 0;
}
/* Space out the masthead */
.header {
margin-bottom: 30px;
}
}
.panel-primary a { color: white; font-weight:bold }

75
app/peerfinder/ev-info.go Normal file
View File

@ -0,0 +1,75 @@
package peerfinder
import (
"bytes"
"encoding/json"
"github.com/tj/go-semver"
"github.com/sour-is/ev/pkg/es/event"
)
type Info struct {
ScriptVersion string `json:"script_version"`
event.AggregateRoot
}
var _ event.Aggregate = (*Info)(nil)
func (a *Info) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *VersionChanged:
a.ScriptVersion = e.ScriptVersion
}
}
}
func (a *Info) MarshalEnviron() ([]byte, error) {
var b bytes.Buffer
b.WriteString("SCRIPT_VERSION=")
b.WriteString(a.ScriptVersion)
b.WriteRune('\n')
return b.Bytes(), nil
}
func (a *Info) OnUpsert() error {
if a.StreamVersion() == 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
current, _ := semver.Parse(initVersion)
previous, _ := semver.Parse(a.ScriptVersion)
if current.Compare(previous) > 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
return nil
}
type VersionChanged struct {
ScriptVersion string `json:"script_version"`
eventMeta event.Meta
}
var _ event.Event = (*VersionChanged)(nil)
func (e *VersionChanged) EventMeta() event.Meta {
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
func (e *VersionChanged) SetEventMeta(m event.Meta) {
if e != nil {
e.eventMeta = m
}
}
func (e *VersionChanged) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *VersionChanged) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}

87
app/peerfinder/ev-peer.go Normal file
View File

@ -0,0 +1,87 @@
package peerfinder
import (
"net"
"strconv"
"strings"
"time"
)
type Time time.Time
func (t *Time) UnmarshalJSON(b []byte) error {
time, err := time.Parse(`"2006-01-02 15:04:05"`, string(b))
*t = Time(time)
return err
}
func (t *Time) MarshalJSON() ([]byte, error) {
if t == nil {
return nil, nil
}
i := *t
return time.Time(i).MarshalJSON()
}
type ipFamily string
const (
ipFamilyV4 ipFamily = "IPv4"
ipFamilyV6 ipFamily = "IPv6"
ipFamilyBoth ipFamily = "both"
ipFamilyNone ipFamily = "none"
)
func (t *ipFamily) UnmarshalJSON(b []byte) error {
i, err := strconv.Atoi(strings.Trim(string(b), `"`))
switch i {
case 1:
*t = ipFamilyV4
case 2:
*t = ipFamilyV6
case 3:
*t = ipFamilyBoth
default:
*t = ipFamilyNone
}
return err
}
type peerType []string
func (t *peerType) UnmarshalJSON(b []byte) error {
*t = strings.Split(strings.Trim(string(b), `"`), ",")
return nil
}
type Peer struct {
ID string `json:"peer_id,omitempty"`
Owner string `json:"peer_owner"`
Nick string `json:"peer_nick"`
Name string `json:"peer_name"`
Country string `json:"peer_country"`
Note string `json:"peer_note"`
Family ipFamily `json:"peer_family"`
Type peerType `json:"peer_type"`
Created Time `json:"peer_created"`
}
func (p *Peer) CanSupport(ip string) bool {
addr := net.ParseIP(ip)
if addr == nil {
return false
}
if !addr.IsGlobalUnicast() {
return false
}
switch p.Family {
case ipFamilyV4:
return addr.To4() != nil
case ipFamilyV6:
return addr.To16() != nil
case ipFamilyNone:
return false
}
return true
}

View File

@ -4,15 +4,11 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"net/netip" "net/netip"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/tj/go-semver" "github.com/oklog/ulid"
"github.com/oklog/ulid/v2"
"github.com/sour-is/ev/pkg/es/event" "github.com/sour-is/ev/pkg/es/event"
) )
@ -54,85 +50,6 @@ func (a *Request) ApplyEvent(lis ...event.Event) {
} }
} }
type Time time.Time
func (t *Time) UnmarshalJSON(b []byte) error {
time, err := time.Parse(`"2006-01-02 15:04:05"`, string(b))
*t = Time(time)
return err
}
func (t *Time) MarshalJSON() ([]byte, error) {
if t == nil {
return nil, nil
}
i := *t
return time.Time(i).MarshalJSON()
}
type ipFamily string
const (
ipFamilyV4 ipFamily = "IPv4"
ipFamilyV6 ipFamily = "IPv6"
ipFamilyBoth ipFamily = "both"
ipFamilyNone ipFamily = "none"
)
func (t *ipFamily) UnmarshalJSON(b []byte) error {
i, err := strconv.Atoi(strings.Trim(string(b), `"`))
switch i {
case 1:
*t = ipFamilyV4
case 2:
*t = ipFamilyV6
case 3:
*t = ipFamilyBoth
default:
*t = ipFamilyNone
}
return err
}
type peerType []string
func (t *peerType) UnmarshalJSON(b []byte) error {
*t = strings.Split(strings.Trim(string(b), `"`), ",")
return nil
}
type Peer struct {
ID string `json:"peer_id,omitempty"`
Owner string `json:"peer_owner"`
Nick string `json:"peer_nick"`
Name string `json:"peer_name"`
Country string `json:"peer_country"`
Note string `json:"peer_note"`
Family ipFamily `json:"peer_family"`
Type peerType `json:"peer_type"`
Created Time `json:"peer_created"`
}
func (p *Peer) CanSupport(ip string) bool {
addr := net.ParseIP(ip)
if addr == nil {
return false
}
if !addr.IsGlobalUnicast() {
return false
}
switch p.Family {
case ipFamilyV4:
return addr.To4() != nil
case ipFamilyV6:
return addr.To16() != nil
case ipFamilyNone:
return false
}
return true
}
type Response struct { type Response struct {
Peer *Peer `json:"peer"` Peer *Peer `json:"peer"`
PeerID string `json:"-"` PeerID string `json:"-"`
@ -149,6 +66,18 @@ type Response struct {
Created time.Time `json:"res_created"` Created time.Time `json:"res_created"`
} }
type ListResponse []Response
func (lis ListResponse) Len() int {
return len(lis)
}
func (lis ListResponse) Less(i, j int) bool {
return lis[i].Latency < lis[j].Latency
}
func (lis ListResponse) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i]
}
type RequestSubmitted struct { type RequestSubmitted struct {
eventMeta event.Meta eventMeta event.Meta
@ -271,68 +200,3 @@ func (e *ResultSubmitted) UnmarshalBinary(b []byte) error {
func (e *ResultSubmitted) String() string { func (e *ResultSubmitted) String() string {
return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency) return fmt.Sprintf("id: %s\npeer: %s\nversion: %s\nlatency: %0.4f", e.RequestID, e.PeerID, e.PeerVersion, e.Latency)
} }
type Info struct {
ScriptVersion string `json:"script_version"`
event.AggregateRoot
}
var _ event.Aggregate = (*Info)(nil)
func (a *Info) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *VersionChanged:
a.ScriptVersion = e.ScriptVersion
}
}
}
func (a *Info) MarshalEnviron() ([]byte, error) {
var b bytes.Buffer
b.WriteString("SCRIPT_VERSION=")
b.WriteString(a.ScriptVersion)
b.WriteRune('\n')
return b.Bytes(), nil
}
func (a *Info) OnUpsert() error {
if a.StreamVersion() == 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
current, _ := semver.Parse(initVersion)
previous, _ := semver.Parse(a.ScriptVersion)
if current.Compare(previous) > 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
return nil
}
type VersionChanged struct {
ScriptVersion string `json:"script_version"`
eventMeta event.Meta
}
var _ event.Event = (*VersionChanged)(nil)
func (e *VersionChanged) EventMeta() event.Meta {
if e == nil {
return event.Meta{}
}
return e.eventMeta
}
func (e *VersionChanged) SetEventMeta(m event.Meta) {
if e != nil {
e.eventMeta = m
}
}
func (e *VersionChanged) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *VersionChanged) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}

559
app/peerfinder/http.go Normal file
View File

@ -0,0 +1,559 @@
package peerfinder
import (
"context"
"embed"
"encoding/json"
"fmt"
"html/template"
"io"
"io/fs"
"log"
"net"
"net/http"
"sort"
"strconv"
"strings"
"github.com/oklog/ulid"
contentnegotiation "gitlab.com/jamietanna/content-negotiation-go"
"go.opentelemetry.io/otel/attribute"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event"
)
var (
//go:embed pages/* layouts/* assets/*
files embed.FS
templates map[string]*template.Template
)
// Args passed to templates
type Args struct {
RemoteIP string
Requests []*Request
CountPeers int
}
// requestArgs builds args from http.Request
func requestArgs(r *http.Request) Args {
remoteIP, _, _ := strings.Cut(r.RemoteAddr, ":")
if s := r.Header.Get("X-Forwarded-For"); s != "" {
s, _, _ = strings.Cut(s, ", ")
remoteIP = s
}
return Args{
RemoteIP: remoteIP,
}
}
// RegisterHTTP adds handler paths to the ServeMux
func (s *service) RegisterHTTP(mux *http.ServeMux) {
a, _ := fs.Sub(files, "assets")
assets := http.StripPrefix("/peers/assets/", http.FileServer(http.FS(a)))
mux.Handle("/peers/assets/", lg.Htrace(assets, "peer-assets"))
mux.Handle("/peers/", lg.Htrace(s, "peers"))
}
// ServeHTTP handle HTTP requests
func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, span := lg.Span(ctx)
defer span.End()
r = r.WithContext(ctx)
switch r.Method {
case http.MethodGet:
switch {
case strings.HasPrefix(r.URL.Path, "/peers/pending/"):
s.getPending(w, r, strings.TrimPrefix(r.URL.Path, "/peers/pending/"))
return
case strings.HasPrefix(r.URL.Path, "/peers/req/"):
s.getResultsForRequest(w, r, strings.TrimPrefix(r.URL.Path, "/peers/req/"))
return
case strings.HasPrefix(r.URL.Path, "/peers/status"):
s.state.Modify(r.Context(), func(ctx context.Context, state *state) error {
for id, p := range state.requests {
fmt.Fprintln(w, "REQ: ", id, p.RequestIP, len(p.Responses))
for id, r := range p.Responses {
fmt.Fprintln(w, " RES: ", id, r.PeerID[24:], r.Latency, r.Jitter)
}
}
return nil
})
default:
s.getResults(w, r)
return
}
case http.MethodPost:
switch {
case strings.HasPrefix(r.URL.Path, "/peers/req/"):
s.postResult(w, r, strings.TrimPrefix(r.URL.Path, "/peers/req/"))
return
case strings.HasPrefix(r.URL.Path, "/peers/req"):
s.postRequest(w, r)
return
default:
w.WriteHeader(http.StatusNotFound)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}
func (s *service) getPending(w http.ResponseWriter, r *http.Request, uuid string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
var peer *Peer
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
var ok bool
if peer, ok = state.peers[uuid]; !ok {
return fmt.Errorf("peer not found: %s", uuid)
}
return nil
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotFound)
return
}
info, err := es.Upsert(ctx, s.es, aggInfo, func(ctx context.Context, agg *Info) error {
return agg.OnUpsert() // initialize if not exists
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
requests, err := s.es.Read(ctx, queueRequests, -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
responses, err := s.es.Read(ctx, aggPeer(uuid), -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
span.AddEvent(fmt.Sprintf("req = %d, res = %d", len(requests), len(responses)))
req := filter(peer, requests, responses)
negotiator := contentnegotiation.NewNegotiator("application/json", "text/environment", "text/plain", "text/html")
negotiated, _, err := negotiator.Negotiate(r.Header.Get("Accept"))
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
span.AddEvent(negotiated.String())
mime := negotiated.String()
switch mime {
case "text/environment":
w.Header().Set("content-type", negotiated.String())
_, err = encodeTo(w, info.MarshalEnviron, req.MarshalEnviron)
case "application/json":
w.Header().Set("content-type", negotiated.String())
var out interface{} = info
if req != nil {
out = struct {
ScriptVersion string `json:"script_version"`
RequestID string `json:"req_id"`
RequestIP string `json:"req_ip"`
Family string `json:"req_family"`
Created string `json:"req_created"`
}{
info.ScriptVersion,
req.RequestID(),
req.RequestIP,
strconv.Itoa(req.Family()),
req.CreatedString(),
}
}
err = json.NewEncoder(w).Encode(out)
}
span.RecordError(err)
}
func (s *service) getResults(w http.ResponseWriter, r *http.Request) {
ctx, span := lg.Span(r.Context())
defer span.End()
events, err := s.es.Read(ctx, queueRequests, -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
requests := make([]*Request, len(events))
for i, req := range events {
if req, ok := req.(*RequestSubmitted); ok {
requests[i], err = s.loadResult(ctx, req.RequestID())
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
args := requestArgs(r)
args.Requests = requests
s.state.Modify(ctx, func(ctx context.Context, state *state) error {
args.CountPeers = len(state.peers)
return nil
})
t := templates["home.tpl"]
t.Execute(w, args)
}
func (s *service) getResultsForRequest(w http.ResponseWriter, r *http.Request, uuid string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
request, err := s.loadResult(ctx, uuid)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
negotiator := contentnegotiation.NewNegotiator("application/json", "text/environment", "text/csv", "text/plain", "text/html")
negotiated, _, err := negotiator.Negotiate(r.Header.Get("Accept"))
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
return
}
span.AddEvent(negotiated.String())
switch negotiated.String() {
// case "text/environment":
// encodeTo(w, responses.MarshalBinary)
case "application/json":
json.NewEncoder(w).Encode(request)
return
default:
args := requestArgs(r)
args.Requests = append(args.Requests, request)
span.AddEvent(fmt.Sprint(args))
err := renderTo(w, "req.tpl", args)
span.RecordError(err)
return
}
}
func (s *service) postRequest(w http.ResponseWriter, r *http.Request) {
ctx, span := lg.Span(r.Context())
defer span.End()
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
args := requestArgs(r)
requestIP := args.RemoteIP
if ip := r.Form.Get("req_ip"); ip != "" {
requestIP = ip
}
ip := net.ParseIP(requestIP)
if ip == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
req := &RequestSubmitted{
RequestIP: ip.String(),
}
if hidden, err := strconv.ParseBool(r.Form.Get("req_hidden")); err != nil {
req.Hidden = hidden
}
span.SetAttributes(
attribute.Stringer("req_ip", ip),
)
s.es.Append(ctx, queueRequests, event.NewEvents(req))
http.Redirect(w, r, "/peers/req/"+req.RequestID(), http.StatusSeeOther)
}
func (s *service) postResult(w http.ResponseWriter, r *http.Request, id string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("id", id),
)
if _, err := ulid.ParseStrict(id); err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
form := make([]string, 0, len(r.Form))
for k, vals := range r.Form {
for _, v := range vals {
form = append(form, fmt.Sprint(k, v))
}
}
span.SetAttributes(
attribute.StringSlice("form", form),
)
peerID := r.Form.Get("peer_id")
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
var ok bool
if _, ok = state.peers[peerID]; !ok {
// fmt.Printf("peer not found: %s\n", peerID)
return fmt.Errorf("peer not found: %s", peerID)
}
return nil
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotFound)
return
}
var unreach bool
latency, err := strconv.ParseFloat(r.Form.Get("res_latency"), 64)
if err != nil {
unreach = true
}
req := &ResultSubmitted{
RequestID: id,
PeerID: r.Form.Get("peer_id"),
PeerVersion: r.Form.Get("peer_version"),
Latency: latency,
Unreachable: unreach,
}
if jitter, err := strconv.ParseFloat(r.Form.Get("res_jitter"), 64); err == nil {
req.Jitter = jitter
} else {
span.RecordError(err)
}
if minrtt, err := strconv.ParseFloat(r.Form.Get("res_minrtt"), 64); err == nil {
req.MinRTT = minrtt
} else {
span.RecordError(err)
}
if maxrtt, err := strconv.ParseFloat(r.Form.Get("res_maxrtt"), 64); err == nil {
req.MaxRTT = maxrtt
} else {
span.RecordError(err)
}
span.SetAttributes(
attribute.Stringer("result", req),
)
s.state.Modify(ctx, func(ctx context.Context, state *state) error {
return nil
})
idx, err := s.es.LastIndex(ctx, aggRequest(id))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if idx == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
s.es.Append(ctx, queueResults, event.NewEvents(req))
}
func renderTo(w io.Writer, name string, args any) (err error) {
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("panic: %s", p)
}
if err != nil {
fmt.Fprint(w, err)
}
}()
t, ok := templates[name]
if !ok || t == nil {
return fmt.Errorf("missing template")
}
return t.Execute(w, args)
}
func encodeTo(w io.Writer, fns ...func() ([]byte, error)) (int, error) {
i := 0
for _, fn := range fns {
b, err := fn()
if err != nil {
return i, err
}
j, err := w.Write(b)
i += j
if err != nil {
return i, err
}
}
return i, nil
}
func loadTemplates() error {
if templates != nil {
return nil
}
templates = make(map[string]*template.Template)
tmplFiles, err := fs.ReadDir(files, "pages")
if err != nil {
return err
}
for _, tmpl := range tmplFiles {
if tmpl.IsDir() {
continue
}
pt := template.New(tmpl.Name())
pt.Funcs(funcMap)
pt, err = pt.ParseFS(files, "pages/"+tmpl.Name(), "layouts/*.tpl")
if err != nil {
log.Println(err)
return err
}
templates[tmpl.Name()] = pt
}
return nil
}
var funcMap = map[string]any{
"orderByPeer": fnOrderByPeer,
"countResponses": fnCountResponses,
}
func fnOrderByPeer(rq *Request) any {
type peerResult struct {
Name string
Country string
Latency float64
Jitter float64
}
type peer struct {
Name string
Note string
Nick string
Country string
Latency float64
Jitter float64
VPNTypes []string
Results []peerResult
}
peers := make(map[string]peer)
sort.Sort(ListResponse(rq.Responses))
for _, rs := range rq.Responses {
p, ok := peers[rs.Peer.Owner]
if !ok {
p.Country = rs.Peer.Country
p.Name = rs.Peer.Name
p.Nick = rs.Peer.Nick
p.Note = rs.Peer.Note
p.Latency = rs.Latency
p.Jitter = rs.Jitter
p.VPNTypes = rs.Peer.Type
}
p.Results = append(p.Results, peerResult{
Name: rs.Peer.Name,
Country: rs.Peer.Country,
Latency: rs.Latency,
Jitter: rs.Jitter,
})
peers[rs.Peer.Owner] = p
}
return peers
}
func fnCountResponses(rq *Request) int {
count := 0
for _, res := range rq.Responses {
if !res.Unreachable {
count++
}
}
return count
}
func filter(peer *Peer, requests, responses event.Events) *RequestSubmitted {
have := make(map[string]struct{}, len(responses))
for _, res := range toList[ResultSubmitted](responses...) {
have[res.RequestID] = struct{}{}
}
for _, req := range reverse(toList[RequestSubmitted](requests...)...) {
if _, ok := have[req.RequestID()]; !ok {
if !peer.CanSupport(req.RequestIP) {
continue
}
return req
}
}
return nil
}
func toList[E any, T es.PE[E]](lis ...event.Event) []T {
newLis := make([]T, 0, len(lis))
for i := range lis {
if e, ok := lis[i].(T); ok {
newLis = append(newLis, e)
}
}
return newLis
}
func reverse[T any](s ...T) []T {
first, last := 0, len(s)-1
for first < last {
s[first], s[last] = s[last], s[first]
first++
last--
}
return s
}

217
app/peerfinder/jobs.go Normal file
View File

@ -0,0 +1,217 @@
package peerfinder
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/math"
"github.com/sour-is/ev/pkg/set"
)
// RefreshJob retrieves peer info from the peerdb
func (s *service) RefreshJob(ctx context.Context, _ time.Time) error {
ctx, span := lg.Span(ctx)
defer span.End()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.statusURL, nil)
span.RecordError(err)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
span.RecordError(err)
if err != nil {
return err
}
defer res.Body.Close()
var peers []*Peer
err = json.NewDecoder(res.Body).Decode(&peers)
span.RecordError(err)
if err != nil {
return err
}
span.AddEvent(fmt.Sprintf("processed %d peers", len(peers)))
err = s.state.Modify(ctx, func(ctx context.Context, t *state) error {
for _, peer := range peers {
t.peers[peer.ID] = peer
}
return nil
})
span.RecordError(err)
return err
}
// CleanJob truncates streams old request data
func (s *service) CleanJob(ctx context.Context, now time.Time) error {
ctx, span := lg.Span(ctx)
defer span.End()
span.AddEvent("clear peerfinder requests")
endRequestID, err := s.cleanRequests(ctx, now)
if err != nil {
return err
}
if err = s.cleanResults(ctx, endRequestID); err != nil {
return err
}
return s.cleanPeerJobs(ctx)
}
func (s *service) cleanPeerJobs(ctx context.Context) error {
ctx, span := lg.Span(ctx)
defer span.End()
peers := set.New[string]()
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
for id := range state.peers {
peers.Add(id)
}
return nil
})
if err != nil {
return err
}
// trunctate all the peer streams to last 30
for streamID := range peers {
streamID = aggPeer(streamID)
first, err := s.es.FirstIndex(ctx, streamID)
if err != nil {
return err
}
last, err := s.es.LastIndex(ctx, streamID)
if err != nil {
return err
}
newFirst := math.Max(int64(last-30), int64(first))
if last == 0 || newFirst == int64(first) {
// fmt.Println("SKIP", streamID, first, newFirst, last)
span.AddEvent(fmt.Sprint("SKIP", streamID, first, newFirst, last))
continue
}
// fmt.Println("TRUNC", streamID, first, newFirst, last)
span.AddEvent(fmt.Sprint("TRUNC", streamID, first, newFirst, last))
err = s.es.Truncate(ctx, streamID, int64(newFirst))
if err != nil {
return err
}
}
return nil
}
func (s *service) cleanRequests(ctx context.Context, now time.Time) (string, error) {
ctx, span := lg.Span(ctx)
defer span.End()
var streamIDs []string
var endPosition uint64
var endRequestID string
last, err := s.es.LastIndex(ctx, queueRequests)
if err != nil {
return "", err
}
end:
for {
events, err := s.es.Read(ctx, queueRequests, int64(endPosition), 1000) // read 1000 from the top each loop.
if err != nil && !errors.Is(err, es.ErrNotFound) {
span.RecordError(err)
return "", err
}
if len(events) == 0 {
break
}
endPosition = events.Last().EventMeta().ActualPosition
for _, event := range events {
switch e := event.(type) {
case *RequestSubmitted:
if e.eventMeta.ActualPosition < last-30 {
streamIDs = append(streamIDs, aggRequest(e.RequestID()))
} else {
endRequestID = e.RequestID()
endPosition = e.eventMeta.ActualPosition
break end
}
}
}
}
// truncate all reqs to found end position
// fmt.Println("TRUNC", queueRequests, int64(endPosition), last)
span.AddEvent(fmt.Sprint("TRUNC", queueRequests, int64(endPosition), last))
err = s.es.Truncate(ctx, queueRequests, int64(endPosition))
if err != nil {
return "", err
}
// truncate all the request streams
for _, streamID := range streamIDs {
last, err := s.es.LastIndex(ctx, streamID)
if err != nil {
return "", err
}
// fmt.Println("TRUNC", streamID, last)
span.AddEvent(fmt.Sprint("TRUNC", streamID, last))
err = s.es.Truncate(ctx, streamID, int64(last))
if err != nil {
return "", err
}
}
return endRequestID, nil
}
func (s *service) cleanResults(ctx context.Context, endRequestID string) error {
ctx, span := lg.Span(ctx)
defer span.End()
var endPosition uint64
done := false
for !done {
events, err := s.es.Read(ctx, queueResults, int64(endPosition), 1000) // read 30 from the top each loop.
if err != nil {
return err
}
if len(events) == 0 {
done = true
continue
}
endPosition = events.Last().EventMeta().ActualPosition
for _, event := range events {
switch e := event.(type) {
case *ResultSubmitted:
if e.RequestID == endRequestID {
done = true
endPosition = e.eventMeta.ActualPosition
}
}
}
}
// truncate all reqs to found end position
// fmt.Println("TRUNC", queueResults, int64(endPosition), last)
span.AddEvent(fmt.Sprint("TRUNC", queueResults, int64(endPosition)))
err := s.es.Truncate(ctx, queueResults, int64(endPosition))
if err != nil {
return err
}
return nil
}

View File

@ -8,7 +8,7 @@
<title>DN42 PingFinder</title> <title>DN42 PingFinder</title>
<link href="/peers/assets/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> <link href="/peers/assets/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href="/peers/assets/peerfinder.css" rel="stylesheet" integrity="sha384-ZsT4S9156eA60lsB4aOfffKowiaZ0NG7gIQfgIfGoxT6FxFocYH39kZgYaeZCvql" crossorigin="anonymous"> <link href="/peers/assets/peerfinder.css" rel="stylesheet" crossorigin="anonymous">
</head> </head>
<body> <body>

View File

@ -48,7 +48,7 @@
{{range $req := .Requests}} {{range $req := .Requests}}
<div class="panel panel-primary"> <div class="panel panel-primary">
<div class="panel-heading"> <div class="panel-heading">
<a href="/peers/req/{{ $req.RequestID }}">{{ $req.RequestIP }} on {{ $req.Created }}</a> <a href="/peers/req/{{ $req.RequestID }}">{{ $req.RequestIP }} on {{ $req.Created.Format "02 Jan 06 15:04 MST" }}</a>
<div style='float:right'><a href="/peers/req/{{ $req.RequestID }}" class='btn btn-success'>{{ countResponses $req }} / {{ $args.CountPeers }} </a></div> <div style='float:right'><a href="/peers/req/{{ $req.RequestID }}" class='btn btn-success'>{{ countResponses $req }} / {{ $args.CountPeers }} </a></div>
</div> </div>
<div class="panel-body"><b>Request ID:</b> {{ $req.RequestID }}</div> <div class="panel-body"><b>Request ID:</b> {{ $req.RequestID }}</div>

View File

@ -15,7 +15,7 @@
<div class="panel-heading"> <div class="panel-heading">
<b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b> <b> {{.Country}} :: {{.Name}} :: {{.Nick}} </b>
<div style='float:right'> <div style='float:right'>
<a class='btn btn-success' href="#peer-{{.Nick}}">{{printf "%0.3f ms" .Latency}}</a> <a class='btn btn-success' href="#peer-{{.Nick}}">{{ if eq .Latency 0.0 }}&mdash;{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</a>
</div> </div>
</div> </div>
<div class="panel-body"> <div class="panel-body">
@ -38,7 +38,7 @@
<tr> <tr>
<th>{{.Name}}</th> <th>{{.Name}}</th>
<td>{{.Country}}</td> <td>{{.Country}}</td>
<td>{{printf "%0.3f ms" .Latency}}</td> <td>{{ if eq .Latency 0.0 }}&mdash;{{ else }}{{printf "%0.3f ms" .Latency}}{{ end }}</td>
<td>{{ if eq .Jitter 0.0 }}&mdash;{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td> <td>{{ if eq .Jitter 0.0 }}&mdash;{{ else }}{{ printf "%0.3f ms" .Jitter }}{{ end }}</td>
</tr> </tr>
{{end}} {{end}}

View File

@ -2,31 +2,14 @@ package peerfinder
import ( import (
"context" "context"
"embed"
"encoding/json"
"errors"
"fmt" "fmt"
"html/template"
"io"
"io/fs"
"log"
"net"
"net/http"
"sort"
"strconv"
"strings"
"time" "time"
"github.com/oklog/ulid"
contentnegotiation "gitlab.com/jamietanna/content-negotiation-go"
"go.opentelemetry.io/otel/attribute"
"github.com/sour-is/ev/internal/lg" "github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/es" "github.com/sour-is/ev/pkg/es"
"github.com/sour-is/ev/pkg/es/event" "github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/locker" "github.com/sour-is/ev/pkg/locker"
"github.com/sour-is/ev/pkg/math" "go.uber.org/multierr"
"github.com/sour-is/ev/pkg/set"
) )
const ( const (
@ -39,21 +22,17 @@ const (
func aggRequest(id string) string { return "pf-request-" + id } func aggRequest(id string) string { return "pf-request-" + id }
func aggPeer(id string) string { return "pf-peer-" + id } func aggPeer(id string) string { return "pf-peer-" + id }
var (
//go:embed pages/* layouts/* assets/*
files embed.FS
templates map[string]*template.Template
)
type service struct { type service struct {
es *es.EventStore es *es.EventStore
statusURL string statusURL string
state *locker.Locked[state] state *locker.Locked[state]
stop func()
} }
type state struct { type state struct {
peers map[string]*Peer peers map[string]*Peer
requests map[string]*Request
} }
func New(ctx context.Context, es *es.EventStore, statusURL string) (*service, error) { func New(ctx context.Context, es *es.EventStore, statusURL string) (*service, error) {
@ -67,621 +46,109 @@ func New(ctx context.Context, es *es.EventStore, statusURL string) (*service, er
return nil, err return nil, err
} }
svc := &service{es: es, statusURL: statusURL, state: locker.New(&state{peers: make(map[string]*Peer)})} svc := &service{
es: es,
statusURL: statusURL,
state: locker.New(&state{
peers: make(map[string]*Peer),
requests: make(map[string]*Request),
})}
return svc, nil return svc, nil
} }
func (s *service) RegisterHTTP(mux *http.ServeMux) { func (s *service) loadResult(ctx context.Context, uuid string) (*Request, error) {
a, _ := fs.Sub(files, "assets") request := &Request{}
assets := http.StripPrefix("/peers/assets/", http.FileServer(http.FS(a))) request.SetStreamID(aggRequest(uuid))
err := s.es.Load(ctx, request)
if err != nil {
return nil, err
}
mux.Handle("/peers/assets/", lg.Htrace(assets, "peer-assets")) return request, s.state.Modify(ctx, func(ctx context.Context, t *state) error {
mux.Handle("/peers/", lg.Htrace(s, "peers")) for i := range request.Responses {
res := &request.Responses[i]
if peer, ok := t.peers[res.PeerID]; ok {
res.Peer = peer
res.Peer.ID = ""
}
}
return nil
})
} }
func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *service) Run(ctx context.Context) (err error) {
ctx := r.Context() var errs error
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
defer span.End() defer span.End()
r = r.WithContext(ctx) ctx, s.stop = context.WithCancel(ctx)
switch r.Method { subReq, e := s.es.EventStream().Subscribe(ctx, queueRequests, 0)
case http.MethodGet: errs = multierr.Append(errs, e)
switch {
case strings.HasPrefix(r.URL.Path, "/peers/pending/"):
s.getPending(w, r, strings.TrimPrefix(r.URL.Path, "/peers/pending/"))
return
case strings.HasPrefix(r.URL.Path, "/peers/req/"): subRes, e := s.es.EventStream().Subscribe(ctx, queueResults, 0)
s.getResultsForRequest(w, r, strings.TrimPrefix(r.URL.Path, "/peers/req/")) errs = multierr.Append(errs, e)
return
case strings.HasPrefix(r.URL.Path, "/peers/status"): defer func() {
s.state.Modify(r.Context(), func(ctx context.Context, state *state) error { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
for id, p := range state.peers { defer cancel()
fmt.Fprintln(w, "PEER: ", id, p.Owner, p.Name)
err = multierr.Combine(subReq.Close(ctx), subRes.Close(ctx), err)
}()
if errs != nil {
return errs
} }
return nil
})
default:
s.getResults(w, r)
return
}
case http.MethodPost:
switch {
case strings.HasPrefix(r.URL.Path, "/peers/req/"):
s.postResult(w, r, strings.TrimPrefix(r.URL.Path, "/peers/req/"))
return
case strings.HasPrefix(r.URL.Path, "/peers/req"):
s.postRequest(w, r)
return
default:
w.WriteHeader(http.StatusNotFound)
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
}
func (s *service) getPending(w http.ResponseWriter, r *http.Request, uuid string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
var peer *Peer
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
var ok bool
if peer, ok = state.peers[uuid]; !ok {
return fmt.Errorf("peer not found: %s", uuid)
}
return nil
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotFound)
return
}
info, err := es.Upsert(ctx, s.es, aggInfo, func(ctx context.Context, agg *Info) error {
return agg.OnUpsert() // initialize if not exists
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
requests, err := s.es.Read(ctx, queueRequests, -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
responses, err := s.es.Read(ctx, aggPeer(uuid), -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
span.AddEvent(fmt.Sprintf("req = %d, res = %d", len(requests), len(responses)))
req := filter(peer, requests, responses)
negotiator := contentnegotiation.NewNegotiator("application/json", "text/environment", "text/plain", "text/html")
negotiated, _, err := negotiator.Negotiate(r.Header.Get("Accept"))
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotAcceptable)
return
}
span.AddEvent(negotiated.String())
mime := negotiated.String()
switch mime {
case "text/environment":
w.Header().Set("content-type", negotiated.String())
_, err = encodeTo(w, info.MarshalEnviron, req.MarshalEnviron)
case "application/json":
w.Header().Set("content-type", negotiated.String())
var out interface{} = info
if req != nil {
out = struct {
ScriptVersion string `json:"script_version"`
RequestID string `json:"req_id"`
RequestIP string `json:"req_ip"`
Family string `json:"req_family"`
Created string `json:"req_created"`
}{
info.ScriptVersion,
req.RequestID(),
req.RequestIP,
strconv.Itoa(req.Family()),
req.CreatedString(),
}
}
err = json.NewEncoder(w).Encode(out)
}
span.RecordError(err)
}
func (s *service) getResults(w http.ResponseWriter, r *http.Request) {
ctx, span := lg.Span(r.Context())
defer span.End()
events, err := s.es.Read(ctx, queueRequests, -1, -30)
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
requests := make([]*Request, len(events))
for i, req := range events {
if req, ok := req.(*RequestSubmitted); ok {
requests[i], err = s.loadResult(ctx, req.RequestID())
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
args := requestArgs(r)
args.Requests = requests
s.state.Modify(ctx, func(ctx context.Context, state *state) error {
args.CountPeers = len(state.peers)
return nil
})
t := templates["home.tpl"]
t.Execute(w, args)
}
func (s *service) getResultsForRequest(w http.ResponseWriter, r *http.Request, uuid string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("uuid", uuid),
)
request, err := s.loadResult(ctx, uuid)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
negotiator := contentnegotiation.NewNegotiator("application/json", "text/environment", "text/csv", "text/plain", "text/html")
negotiated, _, err := negotiator.Negotiate(r.Header.Get("Accept"))
if err != nil {
w.WriteHeader(http.StatusNotAcceptable)
return
}
span.AddEvent(negotiated.String())
switch negotiated.String() {
// case "text/environment":
// encodeTo(w, responses.MarshalBinary)
case "application/json":
json.NewEncoder(w).Encode(request)
return
default:
args := requestArgs(r)
args.Requests = append(args.Requests, request)
span.AddEvent(fmt.Sprint(args))
err := renderTo(w, "req.tpl", args)
span.RecordError(err)
return
}
}
func (s *service) postRequest(w http.ResponseWriter, r *http.Request) {
ctx, span := lg.Span(r.Context())
defer span.End()
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
args := requestArgs(r)
requestIP := args.RemoteIP
if ip := r.Form.Get("req_ip"); ip != "" {
requestIP = ip
}
ip := net.ParseIP(requestIP)
if ip == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
req := &RequestSubmitted{
RequestIP: ip.String(),
}
if hidden, err := strconv.ParseBool(r.Form.Get("req_hidden")); err != nil {
req.Hidden = hidden
}
span.SetAttributes(
attribute.Stringer("req_ip", ip),
)
s.es.Append(ctx, queueRequests, event.NewEvents(req))
http.Redirect(w, r, "/peers/req/"+req.RequestID(), http.StatusSeeOther)
}
func (s *service) postResult(w http.ResponseWriter, r *http.Request, id string) {
ctx, span := lg.Span(r.Context())
defer span.End()
span.SetAttributes(
attribute.String("id", id),
)
if _, err := ulid.ParseStrict(id); err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
if err := r.ParseForm(); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
return
}
peerID := r.Form.Get("peer_id")
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
var ok bool
if _, ok = state.peers[peerID]; !ok {
// fmt.Printf("peer not found: %s\n", peerID)
return fmt.Errorf("peer not found: %s", peerID)
}
return nil
})
if err != nil {
span.RecordError(err)
w.WriteHeader(http.StatusNotFound)
return
}
var unreach bool
latency, err := strconv.ParseFloat(r.Form.Get("res_latency"), 64)
if err != nil {
unreach = true
}
req := &ResultSubmitted{
RequestID: id,
PeerID: r.Form.Get("peer_id"),
PeerVersion: r.Form.Get("peer_version"),
Latency: latency,
Unreachable: unreach,
}
if jitter, err := strconv.ParseFloat(r.Form.Get("res_jitter"), 64); err == nil {
req.Jitter = jitter
} else {
span.RecordError(err)
}
if minrtt, err := strconv.ParseFloat(r.Form.Get("res_minrtt"), 64); err == nil {
req.MinRTT = minrtt
} else {
span.RecordError(err)
}
if maxrtt, err := strconv.ParseFloat(r.Form.Get("res_maxrtt"), 64); err == nil {
req.MaxRTT = maxrtt
} else {
span.RecordError(err)
}
span.SetAttributes(
attribute.Stringer("result", req),
)
s.state.Modify(ctx, func(ctx context.Context, state *state) error {
return nil
})
idx, err := s.es.LastIndex(ctx, aggRequest(id))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if idx == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
s.es.Append(ctx, queueResults, event.NewEvents(req))
}
func (s *service) RefreshJob(ctx context.Context, _ time.Time) error {
ctx, span := lg.Span(ctx)
defer span.End()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.statusURL, nil)
span.RecordError(err)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req)
span.RecordError(err)
if err != nil {
return err
}
defer res.Body.Close()
var peers []*Peer
err = json.NewDecoder(res.Body).Decode(&peers)
span.RecordError(err)
if err != nil {
return err
}
span.AddEvent(fmt.Sprintf("processed %d peers", len(peers)))
err = s.state.Modify(ctx, func(ctx context.Context, t *state) error {
for _, peer := range peers {
t.peers[peer.ID] = peer
}
return nil
})
span.RecordError(err)
return err
}
func (s *service) CleanJob(ctx context.Context, now time.Time) error {
ctx, span := lg.Span(ctx)
defer span.End()
fmt.Println("clear peerfinder requests")
span.AddEvent("clear peerfinder requests")
endRequestID, err := s.cleanRequests(ctx, now)
if err != nil {
return err
}
if err = s.cleanResults(ctx, endRequestID); err != nil {
return err
}
return s.cleanPeerJobs(ctx)
}
func (s *service) cleanPeerJobs(ctx context.Context) error {
ctx, span := lg.Span(ctx)
defer span.End()
peers := set.New[string]()
err := s.state.Modify(ctx, func(ctx context.Context, state *state) error {
for id := range state.peers {
peers.Add(id)
}
return nil
})
if err != nil {
return err
}
// trunctate all the peer streams to last 30
for streamID := range peers {
streamID = aggPeer(streamID)
first, err := s.es.FirstIndex(ctx, streamID)
if err != nil {
return err
}
last, err := s.es.LastIndex(ctx, streamID)
if err != nil {
return err
}
newFirst := math.Max(math.Max(int64(last-30), int64(first)), 1)
if last == 0 || newFirst == int64(first) {
// fmt.Println("SKIP", streamID, first, newFirst, last)
span.AddEvent(fmt.Sprint("SKIP", streamID, first, newFirst, last))
continue
}
// fmt.Println("TRUNC", streamID, first, newFirst, last)
span.AddEvent(fmt.Sprint("TRUNC", streamID, first, newFirst, last))
err = s.es.Truncate(ctx, streamID, int64(newFirst))
if err != nil {
return err
}
}
return nil
}
func (s *service) cleanRequests(ctx context.Context, now time.Time) (string, error) {
ctx, span := lg.Span(ctx)
defer span.End()
var streamIDs []string
var endPosition uint64
var endRequestID string
last, err := s.es.LastIndex(ctx, queueRequests)
if err != nil {
return "", err
}
end:
for { for {
events, err := s.es.Read(ctx, queueRequests, int64(endPosition), 1000) // read 1000 from the top each loop. var events event.Events
if err != nil && !errors.Is(err, es.ErrNotFound) { select {
span.RecordError(err) case <-ctx.Done():
return "", err 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)
}
} }
if len(events) == 0 { s.state.Modify(ctx, func(ctx context.Context, state *state) error {
break return state.ApplyEvents(events)
})
events = events[:0]
} }
}
func (s *service) Stop(ctx context.Context) (err error) {
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("PANIC: %v", p)
}
}()
endPosition = events.Last().EventMeta().ActualPosition s.stop()
for _, event := range events { return err
switch e := event.(type) { }
func (s *state) ApplyEvents(events event.Events) error {
for _, e := range events {
switch e := e.(type) {
case *RequestSubmitted: case *RequestSubmitted:
if e.eventMeta.ActualPosition < last-30 || e.Created().Before(now.Add(-24*time.Hour)) { if _, ok := s.requests[e.RequestID()]; !ok {
streamIDs = append(streamIDs, aggRequest(e.RequestID())) s.requests[e.RequestID()] = &Request{}
} else {
endRequestID = e.RequestID()
endPosition = e.eventMeta.ActualPosition
break end
} }
} s.requests[e.RequestID()].ApplyEvent(e)
}
}
// truncate all reqs to found end position
// fmt.Println("TRUNC", queueRequests, int64(endPosition), last)
span.AddEvent(fmt.Sprint("TRUNC", queueRequests, int64(endPosition), last))
err = s.es.Truncate(ctx, queueRequests, int64(endPosition))
if err != nil {
return "", err
}
// truncate all the request streams
for _, streamID := range streamIDs {
last, err := s.es.LastIndex(ctx, streamID)
if err != nil {
return "", err
}
fmt.Println("TRUNC", streamID, last)
span.AddEvent(fmt.Sprint("TRUNC", streamID, last))
err = s.es.Truncate(ctx, streamID, int64(last))
if err != nil {
return "", err
}
}
return endRequestID, nil
}
func (s *service) cleanResults(ctx context.Context, endRequestID string) error {
ctx, span := lg.Span(ctx)
defer span.End()
var endPosition uint64
done := false
for !done {
events, err := s.es.Read(ctx, queueResults, int64(endPosition), 1000) // read 30 from the top each loop.
if err != nil {
return err
}
if len(events) == 0 {
done = true
continue
}
endPosition = events.Last().EventMeta().ActualPosition
for _, event := range events {
switch e := event.(type) {
case *ResultSubmitted: case *ResultSubmitted:
if e.RequestID == endRequestID { if _, ok := s.requests[e.RequestID]; !ok {
done = true s.requests[e.RequestID] = &Request{}
endPosition = e.eventMeta.ActualPosition
} }
s.requests[e.RequestID].ApplyEvent(e)
} }
} }
}
// truncate all reqs to found end position
// fmt.Println("TRUNC", queueResults, int64(endPosition), last)
span.AddEvent(fmt.Sprint("TRUNC", queueResults, int64(endPosition)))
err := s.es.Truncate(ctx, queueResults, int64(endPosition))
if err != nil {
return err
}
return nil
}
func filter(peer *Peer, requests, responses event.Events) *RequestSubmitted {
have := make(map[string]struct{}, len(responses))
for _, res := range toList[ResultSubmitted](responses...) {
have[res.RequestID] = struct{}{}
}
for _, req := range reverse(toList[RequestSubmitted](requests...)...) {
if _, ok := have[req.RequestID()]; !ok {
if !peer.CanSupport(req.RequestIP) {
continue
}
return req
}
}
return nil
}
func toList[E any, T es.PE[E]](lis ...event.Event) []T {
newLis := make([]T, 0, len(lis))
for i := range lis {
if e, ok := lis[i].(T); ok {
newLis = append(newLis, e)
}
}
return newLis
}
func reverse[T any](s ...T) []T {
first, last := 0, len(s)-1
for first < last {
s[first], s[last] = s[last], s[first]
first++
last--
}
return s
}
func encodeTo(w io.Writer, fns ...func() ([]byte, error)) (int, error) {
i := 0
for _, fn := range fns {
b, err := fn()
if err != nil {
return i, err
}
j, err := w.Write(b)
i += j
if err != nil {
return i, err
}
}
return i, nil
}
func loadTemplates() error {
if templates != nil {
return nil
}
templates = make(map[string]*template.Template)
tmplFiles, err := fs.ReadDir(files, "pages")
if err != nil {
return err
}
for _, tmpl := range tmplFiles {
if tmpl.IsDir() {
continue
}
pt := template.New(tmpl.Name())
pt.Funcs(funcMap)
pt, err = pt.ParseFS(files, "pages/"+tmpl.Name(), "layouts/*.tpl")
if err != nil {
log.Println(err)
return err
}
templates[tmpl.Name()] = pt
}
return nil return nil
} }
@ -707,130 +174,3 @@ func Projector(e event.Event) []event.Event {
} }
return nil return nil
} }
type Args struct {
RemoteIP string
Requests []*Request
CountPeers int
}
func requestArgs(r *http.Request) Args {
remoteIP, _, _ := strings.Cut(r.RemoteAddr, ":")
if s := r.Header.Get("X-Forwarded-For"); s != "" {
remoteIP = s
}
return Args{
RemoteIP: remoteIP,
}
}
func renderTo(w io.Writer, name string, args any) (err error) {
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("panic: %s", p)
}
if err != nil {
fmt.Fprint(w, err)
}
}()
t, ok := templates[name]
if !ok || t == nil {
return fmt.Errorf("missing template")
}
return t.Execute(w, args)
}
type ListResponse []Response
func (lis ListResponse) Len() int {
return len(lis)
}
func (lis ListResponse) Less(i, j int) bool {
return lis[i].Latency < lis[j].Latency
}
func (lis ListResponse) Swap(i, j int) {
lis[i], lis[j] = lis[j], lis[i]
}
func fnOrderByPeer(rq *Request) any {
type peerResult struct {
Name string
Country string
Latency float64
Jitter float64
}
type peer struct {
Name string
Note string
Nick string
Country string
Latency float64
Jitter float64
VPNTypes []string
Results []peerResult
}
peers := make(map[string]peer)
sort.Sort(ListResponse(rq.Responses))
for _, rs := range rq.Responses {
p, ok := peers[rs.Peer.Owner]
if !ok {
p.Country = rs.Peer.Country
p.Name = rs.Peer.Name
p.Nick = rs.Peer.Nick
p.Note = rs.Peer.Note
p.Latency = rs.Latency
p.Jitter = rs.Jitter
p.VPNTypes = rs.Peer.Type
}
p.Results = append(p.Results, peerResult{
Name: rs.Peer.Name,
Country: rs.Peer.Country,
Latency: rs.Latency,
Jitter: rs.Jitter,
})
peers[rs.Peer.Owner] = p
}
return peers
}
func fnCountResponses(rq *Request) int {
count := 0
for _, res := range rq.Responses {
if !res.Unreachable {
count++
}
}
return count
}
var funcMap = map[string]any{
"orderByPeer": fnOrderByPeer,
"countResponses": fnCountResponses,
}
func (s *service) loadResult(ctx context.Context, uuid string) (*Request, error) {
request := &Request{}
request.SetStreamID(aggRequest(uuid))
err := s.es.Load(ctx, request)
if err != nil {
return nil, err
}
return request, s.state.Modify(ctx, func(ctx context.Context, t *state) error {
for i := range request.Responses {
res := &request.Responses[i]
if peer, ok := t.peers[res.PeerID]; ok {
res.Peer = peer
res.Peer.ID = ""
}
}
return nil
})
}

View File

@ -51,7 +51,7 @@ var saltyKey = contextKey{"salty"}
type SaltyResolver interface { type SaltyResolver interface {
CreateSaltyUser(ctx context.Context, nick string, pub string) (*SaltyUser, error) CreateSaltyUser(ctx context.Context, nick string, pub string) (*SaltyUser, error)
SaltyUser(ctx context.Context, nick string) (*SaltyUser, error) SaltyUser(ctx context.Context, nick string) (*SaltyUser, error)
RegisterHTTP(mux *http.ServeMux) IsResolver()
} }
func New(ctx context.Context, es *es.EventStore, baseURL string) (*service, error) { func New(ctx context.Context, es *es.EventStore, baseURL string) (*service, error) {
@ -111,6 +111,8 @@ func New(ctx context.Context, es *es.EventStore, baseURL string) (*service, erro
return svc, errs return svc, errs
} }
func (s *service) IsResolver() {}
func (s *service) BaseURL() string { func (s *service) BaseURL() string {
if s == nil { if s == nil {
return "http://missing.context/" return "http://missing.context/"

View File

@ -600,6 +600,14 @@ directive @goTag(
key: String! key: String!
value: String value: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION`, BuiltIn: false}, ) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION`, BuiltIn: false},
{Name: "../../../app/mercury/mercury.graphqls", Input: `# extend type Query{
# keys(namespace: String!) [String!]!
# get(namespace: String! keys: [String!]) [String]!
# }
# extend type Mutation{
# set(namespace: String! key: String! value: String): Bool!
# }`, BuiltIn: false},
{Name: "../../../app/msgbus/msgbus.graphqls", Input: `extend type Query { {Name: "../../../app/msgbus/msgbus.graphqls", Input: `extend type Query {
posts(streamID: String! paging: PageInput): Connection! posts(streamID: String! paging: PageInput): Connection!
} }

59
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -28,6 +29,7 @@ import (
resolvelinks "github.com/sour-is/ev/pkg/es/driver/resolve-links" resolvelinks "github.com/sour-is/ev/pkg/es/driver/resolve-links"
"github.com/sour-is/ev/pkg/es/driver/streamer" "github.com/sour-is/ev/pkg/es/driver/streamer"
"github.com/sour-is/ev/pkg/es/event" "github.com/sour-is/ev/pkg/es/event"
"github.com/sour-is/ev/pkg/gql/resolver"
"github.com/sour-is/ev/pkg/set" "github.com/sour-is/ev/pkg/set"
) )
@ -35,18 +37,21 @@ func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
go func() { go func() {
<-ctx.Done() <-ctx.Done()
defer cancel() defer cancel() // restore interrupt function
}() }()
// Initialize logger
ctx, stop := lg.Init(ctx, appName) ctx, stop := lg.Init(ctx, appName)
defer stop() defer stop()
if err := run(ctx); err != nil && err != http.ErrServerClosed { // Run application
if err := run(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err) log.Fatal(err)
} }
} }
func run(ctx context.Context) error { func run(ctx context.Context) error {
g, ctx := errgroup.WithContext(ctx) g, ctx := errgroup.WithContext(ctx)
stop := &stopFns{}
cron := cron.New(cron.DefaultGranularity) cron := cron.New(cron.DefaultGranularity)
@ -96,8 +101,9 @@ func run(ctx context.Context) error {
enable := set.New(strings.Fields(env("EV_ENABLE", "salty msgbus gql peers"))...) enable := set.New(strings.Fields(env("EV_ENABLE", "salty msgbus gql peers"))...)
var svcs []interface{ RegisterHTTP(*http.ServeMux) } var svcs []interface{ RegisterHTTP(*http.ServeMux) }
var res []resolver.IsResolver
svcs = append(svcs, es) res = append(res, es)
if enable.Has("salty") { if enable.Has("salty") {
span.AddEvent("Enable Salty") span.AddEvent("Enable Salty")
@ -113,6 +119,7 @@ func run(ctx context.Context) error {
return err return err
} }
svcs = append(svcs, salty) svcs = append(svcs, salty)
res = append(res, salty)
} }
if enable.Has("msgbus") { if enable.Has("msgbus") {
@ -123,6 +130,7 @@ func run(ctx context.Context) error {
return err return err
} }
svcs = append(svcs, msgbus) svcs = append(svcs, msgbus)
res = append(res, msgbus)
} }
if enable.Has("peers") { if enable.Has("peers") {
@ -137,11 +145,15 @@ func run(ctx context.Context) error {
cron.NewJob("0,15,30,45", peers.RefreshJob) cron.NewJob("0,15,30,45", peers.RefreshJob)
cron.Once(ctx, peers.CleanJob) cron.Once(ctx, peers.CleanJob)
cron.NewJob("0 1", peers.CleanJob) cron.NewJob("0 1", peers.CleanJob)
g.Go(func() error {
return peers.Run(ctx)
})
stop.add(peers.Stop)
} }
if enable.Has("gql") { if enable.Has("gql") {
span.AddEvent("Enable GraphQL") span.AddEvent("Enable GraphQL")
gql, err := gql.New(ctx, svcs...) gql, err := resolver.New(ctx, &gql.Resolver{}, res...)
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)
return err return err
@ -164,18 +176,23 @@ func run(ctx context.Context) error {
Mup.Add(ctx, 1) Mup.Add(ctx, 1)
g.Go(s.ListenAndServe) g.Go(s.ListenAndServe)
stop.add(s.Shutdown)
g.Go(func() error {
<-ctx.Done()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return s.Shutdown(ctx)
})
span.End() span.End()
} }
g.Go(func() error { return cron.Run(ctx) }) g.Go(func() error {
<-ctx.Done()
// shutdown jobs
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return stop.stop(ctx)
})
g.Go(func() error {
return cron.Run(ctx)
})
if err := g.Wait(); err != nil && err != http.ErrServerClosed { if err := g.Wait(); err != nil && err != http.ErrServerClosed {
return err return err
@ -203,3 +220,21 @@ var appName, version = func() (string, string) {
return "sour.is-ev", "(devel)" return "sour.is-ev", "(devel)"
}() }()
type stopFns struct {
fns []func(context.Context) error
}
func (s *stopFns) add(fn func(context.Context) error) {
s.fns = append(s.fns, fn)
}
func (s *stopFns) stop(ctx context.Context) error {
g, _ := errgroup.WithContext(ctx)
for i := range s.fns {
fn := s.fns[i]
g.Go(func() error {
return fn(ctx)
})
}
return g.Wait()
}

View File

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

View File

@ -4,6 +4,7 @@ package streamer
import ( import (
"context" "context"
"fmt" "fmt"
"sync"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
@ -67,6 +68,7 @@ func (s *streamer) Subscribe(ctx context.Context, streamID string, start int64)
if err != nil { if err != nil {
return nil, err return nil, err
} }
sub := &subscription{topic: streamID, events: events} sub := &subscription{topic: streamID, events: events}
sub.position = locker.New(&position{ sub.position = locker.New(&position{
idx: start, 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 { return s.state.Modify(ctx, func(ctx context.Context, state *state) error {
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
defer span.End() defer span.End()
span.AddEvent(fmt.Sprint("subscribers=", len(state.subscribers[streamID]))) span.AddEvent(fmt.Sprint("subscribers=", len(state.subscribers[streamID])))
for _, sub := range state.subscribers[streamID] { for _, sub := range state.subscribers[streamID] {
err := sub.position.Modify(ctx, func(ctx context.Context, position *position) error { err := sub.position.Modify(ctx, func(ctx context.Context, position *position) error {
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
defer span.End() defer span.End()
span.SetAttributes( span.SetAttributes(
attribute.String("streamID", streamID), attribute.String("streamID", streamID),
attribute.Int64("actualPosition", int64(events.Last().EventMeta().ActualPosition)), 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.link = trace.LinkFromContext(ctx, attribute.String("src", "event"))
position.wait = nil position.wait = nil
} }
return nil return nil
}) })
if err != nil { if err != nil {
@ -210,13 +215,18 @@ type subscription struct {
events driver.EventLog events driver.EventLog
unsub func(context.Context) error 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) ctx, span := lg.Span(ctx)
defer span.End() defer span.End()
done := make(chan bool)
go func() {
var wait func(context.Context) bool var wait func(context.Context) bool
defer close(done)
err := s.position.Modify(ctx, func(ctx context.Context, position *position) error { err := s.position.Modify(ctx, func(ctx context.Context, position *position) error {
_, span := lg.Span(ctx) _, span := lg.Span(ctx)
@ -239,10 +249,9 @@ func (s *subscription) Recv(ctx context.Context) bool {
span.End() span.End()
position.link = trace.Link{} position.link = trace.Link{}
} }
return true return true
case <-ctx.Done():
case <-ctx.Done():
return false return false
} }
} }
@ -252,14 +261,19 @@ func (s *subscription) Recv(ctx context.Context) bool {
return nil return nil
}) })
if err != nil { if err != nil {
return false done <- false
return
} }
if wait != nil { if wait != nil {
return wait(ctx) done <- wait(ctx)
return
} }
return true done <- true
}()
return done
} }
func (s *subscription) Events(ctx context.Context) (event.Events, error) { func (s *subscription) Events(ctx context.Context) (event.Events, error) {
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
@ -293,5 +307,12 @@ func (s *subscription) Close(ctx context.Context) error {
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
defer span.End() 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"} var esKey = contextKey{"event-store"}
func (es *EventStore) IsResolver() {}
func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error) { func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.PageInput) (*gql.Connection, error) {
ctx, span := lg.Span(ctx) ctx, span := lg.Span(ctx)
defer span.End() defer span.End()
@ -51,10 +52,10 @@ func (es *EventStore) Events(ctx context.Context, streamID string, paging *gql.P
return &gql.Connection{ return &gql.Connection{
Paging: &gql.PageInfo{ Paging: &gql.PageInfo{
Next: lis.Last().EventMeta().Position < last, Next: lis.Last().EventMeta().ActualPosition < last,
Prev: lis.First().EventMeta().Position > first, Prev: lis.First().EventMeta().ActualPosition > first,
Begin: lis.First().EventMeta().Position, Begin: lis.First().EventMeta().ActualPosition,
End: lis.Last().EventMeta().Position, End: lis.Last().EventMeta().ActualPosition,
}, },
Edges: edges, Edges: edges,
}, nil }, 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) events, err := sub.Events(ctx)
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)
@ -120,7 +121,6 @@ func (es *EventStore) TruncateStream(ctx context.Context, streamID string, index
err := es.Truncate(ctx, streamID, index) err := es.Truncate(ctx, streamID, index)
return err == nil, err return err == nil, err
} }
func (*EventStore) RegisterHTTP(*http.ServeMux) {}
func (e *EventStore) GetMiddleware() func(http.Handler) http.Handler { func (e *EventStore) GetMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@ -0,0 +1,141 @@
package resolver
import (
"context"
"fmt"
"net/http"
"os"
"reflect"
"runtime/debug"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/gorilla/websocket"
"github.com/ravilushqa/otelgqlgen"
"github.com/sour-is/ev/internal/lg"
"github.com/sour-is/ev/pkg/gql/graphiql"
"github.com/sour-is/ev/pkg/gql/playground"
)
type BaseResolver interface {
ExecutableSchema() graphql.ExecutableSchema
BaseResolver() IsResolver
}
type Resolver[T BaseResolver] struct {
res T
}
type IsResolver interface {
IsResolver()
}
func New[T BaseResolver](ctx context.Context, base T, resolvers ...IsResolver) (*Resolver[T], error) {
_, span := lg.Span(ctx)
defer span.End()
v := reflect.ValueOf(base)
v = reflect.Indirect(v)
noop := reflect.ValueOf(base.BaseResolver())
outer:
for _, idx := range reflect.VisibleFields(v.Type()) {
field := v.FieldByIndex(idx.Index)
for i := range resolvers {
rs := reflect.ValueOf(resolvers[i])
if field.IsNil() && rs.Type().Implements(field.Type()) {
span.AddEvent(fmt.Sprint("found ", field.Type().Name()))
field.Set(rs)
continue outer
}
}
span.AddEvent(fmt.Sprint("default ", field.Type().Name()))
field.Set(noop)
}
return &Resolver[T]{base}, nil
}
func (r *Resolver[T]) Resolver() T {
return r.res
}
// ChainMiddlewares will check all embeded resolvers for a GetMiddleware func and add to handler.
func (r *Resolver[T]) ChainMiddlewares(h http.Handler) http.Handler {
v := reflect.ValueOf(r) // Get reflected value of *Resolver
v = reflect.Indirect(v) // Get the pointed value (returns a zero value on nil)
n := v.NumField() // Get number of fields to iterate over.
for i := 0; i < n; i++ {
f := v.Field(i)
if !f.CanInterface() { // Skip non-interface types.
continue
}
if iface, ok := f.Interface().(interface {
GetMiddleware() func(http.Handler) http.Handler
}); ok {
h = iface.GetMiddleware()(h) // Append only items that fulfill the interface.
}
}
return h
}
func (r *Resolver[T]) RegisterHTTP(mux *http.ServeMux) {
gql := NewServer(r.res.ExecutableSchema())
gql.SetRecoverFunc(NoopRecover)
gql.Use(otelgqlgen.Middleware())
mux.Handle("/graphiql", graphiql.Handler("GraphiQL playground", "/gql"))
mux.Handle("/gql", lg.Htrace(r.ChainMiddlewares(gql), "gql"))
mux.Handle("/playground", playground.Handler("GraphQL playground", "/gql"))
}
func NoopRecover(ctx context.Context, err interface{}) error {
if err, ok := err.(string); ok && err == "not implemented" {
return gqlerror.Errorf("not implemented")
}
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr)
debug.PrintStack()
return gqlerror.Errorf("internal system error")
}
func NewServer(es graphql.ExecutableSchema) *handler.Server {
srv := handler.New(es)
srv.AddTransport(transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
switch r.Header.Get("Origin") {
case "https://ev.sour.is", "https://www.graphqlbin.com", "http://localhost:8080":
return true
default:
return false
}
},
},
KeepAlivePingInterval: 10 * time.Second,
})
srv.AddTransport(transport.Options{})
srv.AddTransport(transport.GET{})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.MultipartForm{})
srv.SetQueryCache(lru.New(1000))
srv.Use(extension.Introspection{})
srv.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
return srv
}