Skip to content

Commit

Permalink
some basic starting fixes on the relay state data
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Jan 2, 2024
1 parent 5fb320c commit 07275d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
23 changes: 11 additions & 12 deletions cmd/khatru/examples/basic-badger/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"fmt"
"net/http"
"os"

"github.com/Hubmakerlabs/replicatr/cmd/khatru"
"github.com/fiatjaf/eventstore/badger"
Expand All @@ -11,18 +11,17 @@ import (
const appName = "replicatr"

func main() {
relay := khatru.NewRelay(appName)
r := khatru.NewRelay(appName)

db := badger.BadgerBackend{Path: "/tmp/khatru-badgern-tmp"}
db := badger.BadgerBackend{Path: "/tmp/replicatr-badger"}
if err := db.Init(); err != nil {
panic(err)
r.Log.E.F("unable to start database: '%s'", err)
os.Exit(1)
}

relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)

fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
r.StoreEvent = append(r.StoreEvent, db.SaveEvent)
r.QueryEvents = append(r.QueryEvents, db.QueryEvents)
r.CountEvents = append(r.CountEvents, db.CountEvents)
r.DeleteEvent = append(r.DeleteEvent, db.DeleteEvent)
r.Log.I.Ln("running on :3334")
r.Log.E.Chk(http.ListenAndServe(":3334", r))
}
42 changes: 19 additions & 23 deletions cmd/khatru/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,39 @@ import (
"github.com/puzpuzpuz/xsync/v2"
)

const (
WriteWait = 10 * time.Second
PongWait = 60 * time.Second
PingPeriod = 30 * time.Second
ReadBufferSize = 4096
WriteBufferSize = 4096
MaxMessageSize int64 = 512000 // ???
)

func NewRelay(appName string) *Relay {
return &Relay{
Log: log.New(os.Stderr, appName, 0),

Info: &nip11.RelayInformationDocument{
Software: "https://github.com/Hubmakerlabs/replicatr/cmd/khatru",
Version: "n/a",
SupportedNIPs: make([]int, 0),
},

upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
ReadBufferSize: ReadBufferSize,
WriteBufferSize: WriteBufferSize,
CheckOrigin: func(r *http.Request) bool { return true },
},

clients: xsync.NewTypedMapOf[*websocket.Conn, struct{}](pointerHasher[websocket.Conn]),
serveMux: &http.ServeMux{},

WriteWait: 10 * time.Second,
PongWait: 60 * time.Second,
PingPeriod: 30 * time.Second,
MaxMessageSize: 512000,
clients: xsync.NewTypedMapOf[*websocket.Conn, struct{}](pointerHasher[websocket.Conn]),
serveMux: &http.ServeMux{},
WriteWait: WriteWait,
PongWait: PongWait,
PingPeriod: PingPeriod,
MaxMessageSize: MaxMessageSize,
}
}

type Relay struct {
ServiceURL string

ServiceURL string
RejectEvent []func(ctx context.Context, event *nostr.Event) (reject bool, msg string)
RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
Expand All @@ -58,25 +62,17 @@ type Relay struct {
OnConnect []func(ctx context.Context)
OnDisconnect []func(ctx context.Context)
OnEventSaved []func(ctx context.Context, event *nostr.Event)

// editing info will affect
Info *nip11.RelayInformationDocument

// Default logger, as set by NewServer, is a stdlib logger prefixed with "[khatru-relay] ",
// outputting to stderr.
Log *log.Logger

Log *log.Logger
// for establishing websockets
upgrader websocket.Upgrader

// keep a connection reference to all connected clients for Server.Shutdown
clients *xsync.MapOf[*websocket.Conn, struct{}]

// in case you call Server.Start
Addr string
serveMux *http.ServeMux
httpServer *http.Server

// websocket options
WriteWait time.Duration // Time allowed to write a message to the peer.
PongWait time.Duration // Time allowed to read the next pong message from the peer.
Expand Down

0 comments on commit 07275d5

Please sign in to comment.