wipwipwip
This commit is contained in:
@@ -142,17 +142,14 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.(http.Flusher).Flush()
|
||||
|
||||
notifier.AddKey(userId.String())
|
||||
defer notifier.RemoveKey(userId.String())
|
||||
// w.(http.Flusher).Flush()
|
||||
|
||||
if err := notifier.Create(userId.String()); err != nil {
|
||||
// TODO: this could be better.
|
||||
// EG: The user could attempt to create many connections
|
||||
// and they just get a 500, with no explanation.
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.(http.Flusher).Flush()
|
||||
// w.(http.Flusher).Flush()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -174,7 +171,7 @@ func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
|
||||
|
||||
fmt.Printf("Sending msg %s\n", msg)
|
||||
fmt.Fprintf(w, "event: data\ndata: %s\n\n", string(msgString))
|
||||
w.(http.Flusher).Flush()
|
||||
// w.(http.Flusher).Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ type Notifier[TNotification any] struct {
|
||||
bufferSize int
|
||||
|
||||
Listeners map[string]chan TNotification
|
||||
|
||||
AllowedKeys map[string]bool
|
||||
}
|
||||
|
||||
func (n *Notifier[TNotification]) Create(id string) error {
|
||||
@@ -17,10 +15,6 @@ func (n *Notifier[TNotification]) Create(id string) error {
|
||||
return errors.New("This listener already exists")
|
||||
}
|
||||
|
||||
if _, exists := n.AllowedKeys[id]; !exists {
|
||||
return errors.New("This key cannot be used to create a listener")
|
||||
}
|
||||
|
||||
n.Listeners[id] = make(chan TNotification, n.bufferSize)
|
||||
|
||||
return nil
|
||||
@@ -56,18 +50,9 @@ func (n *Notifier[TNotification]) Delete(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *Notifier[TNotification]) AddKey(id string) {
|
||||
n.AllowedKeys[id] = true
|
||||
}
|
||||
|
||||
func (n *Notifier[TNotification]) RemoveKey(id string) {
|
||||
delete(n.AllowedKeys, id)
|
||||
}
|
||||
|
||||
func NewNotifier[TNotification any](bufferSize int) Notifier[TNotification] {
|
||||
return Notifier[TNotification]{
|
||||
bufferSize: bufferSize,
|
||||
Listeners: make(map[string]chan TNotification),
|
||||
AllowedKeys: make(map[string]bool),
|
||||
bufferSize: bufferSize,
|
||||
Listeners: make(map[string]chan TNotification),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@ func TestSendingNotifications(t *testing.T) {
|
||||
|
||||
notifier := NewNotifier[string](3)
|
||||
|
||||
notifier.AddKey("1")
|
||||
|
||||
err := notifier.SendAndCreate("1", "a")
|
||||
require.NoError(err)
|
||||
|
||||
@@ -41,8 +39,6 @@ func TestFullBuffer(t *testing.T) {
|
||||
|
||||
notifier := NewNotifier[string](1)
|
||||
|
||||
notifier.AddKey("1")
|
||||
|
||||
err := notifier.SendAndCreate("1", "a")
|
||||
require.NoError(err)
|
||||
|
||||
@@ -50,11 +46,3 @@ func TestFullBuffer(t *testing.T) {
|
||||
|
||||
assert.Error(err)
|
||||
}
|
||||
|
||||
func TestNoAllowedKey(t *testing.T) {
|
||||
require := require.New(t)
|
||||
notifier := NewNotifier[string](1)
|
||||
|
||||
err := notifier.SendAndCreate("1", "a")
|
||||
require.Error(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user