commit 0ad0fbf10602762f41cc3cfc9e1488736c18ec44
parent 8c3e701c9b2e97b3b60f0e0ce8e327d2b977b68c
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 24 May 2023 07:07:20 -0700
simplify code
Diffstat:
2 files changed, 25 insertions(+), 43 deletions(-)
diff --git a/pkg/pubsub/pubsub.go b/pkg/pubsub/pubsub.go
@@ -84,7 +84,7 @@ type Sub[T any] struct {
}
// ReceiveTimeout2 returns a message received on the channel or timeout
-func (s *Sub[T]) ReceiveTimeout2(timeout time.Duration, c1, c2 chan bool) (topic string, msg T, err error) {
+func (s *Sub[T]) ReceiveTimeout2(timeout time.Duration, c1 <-chan struct{}) (topic string, msg T, err error) {
select {
case p := <-s.ch:
return p.topic, p.msg, nil
@@ -92,8 +92,6 @@ func (s *Sub[T]) ReceiveTimeout2(timeout time.Duration, c1, c2 chan bool) (topic
return topic, msg, ErrTimeout
case <-c1:
return topic, msg, ErrCancelled
- case <-c2:
- return topic, msg, ErrCancelled
case <-s.ctx.Done():
return topic, msg, ErrCancelled
}
diff --git a/pkg/web/handlers/handlers.go b/pkg/web/handlers/handlers.go
@@ -4802,28 +4802,7 @@ func ChessGameHandler(c echo.Context) error {
return nil
}
- quit := make(chan bool)
- quit1 := make(chan bool)
-
- // Listen to the closing of HTTP connection via CloseNotifier
- notify := c.Request().Context().Done()
- utils.SGo(func() {
- select {
- case <-notify:
- case <-quit1:
- }
- close(quit)
- })
-
- notify1 := make(chan os.Signal)
- signal.Notify(notify1, syscall.SIGINT, syscall.SIGTERM)
- utils.SGo(func() {
- select {
- case <-notify1:
- case <-quit:
- }
- close(quit1)
- })
+ quit := closeSignalChan(c)
if err := usersStreamsManager.Add(authUser.ID); err != nil {
return nil
@@ -4857,8 +4836,6 @@ Loop:
select {
case <-quit:
break Loop
- case <-quit1:
- break Loop
default:
}
@@ -4866,7 +4843,7 @@ Loop:
break
}
- _, _, err := sub.ReceiveTimeout2(1*time.Second, quit, quit1)
+ _, _, err := sub.ReceiveTimeout2(1*time.Second, quit)
if err != nil {
if err == pubsub.ErrCancelled {
break Loop
@@ -4950,24 +4927,17 @@ func (m *UsersStreamsManager) Remove(userID database.UserID) {
var usersStreamsManager = NewUsersStreamsManager()
-func ChatStreamMessagesHandler(c echo.Context) error {
- db := c.Get("database").(*database.DkfDB)
- authUser := c.Get("authUser").(*database.User)
-
- roomName := c.Param("roomName")
- room, err := db.GetChatRoomByName(roomName)
- if err != nil {
- return c.Redirect(http.StatusFound, "/")
- }
-
- quit := make(chan bool)
- quit1 := make(chan bool)
+func closeSignalChan(c echo.Context) <-chan struct{} {
+ out := make(chan struct{})
+ quit := make(chan struct{})
+ quit1 := make(chan struct{})
// Listen to the closing of HTTP connection via CloseNotifier
notify := c.Request().Context().Done()
utils.SGo(func() {
select {
case <-notify:
+ close(out)
case <-quit1:
}
close(quit)
@@ -4978,11 +4948,27 @@ func ChatStreamMessagesHandler(c echo.Context) error {
utils.SGo(func() {
select {
case <-notify1:
+ close(out)
case <-quit:
}
close(quit1)
})
+ return out
+}
+
+func ChatStreamMessagesHandler(c echo.Context) error {
+ db := c.Get("database").(*database.DkfDB)
+ authUser := c.Get("authUser").(*database.User)
+
+ roomName := c.Param("roomName")
+ room, err := db.GetChatRoomByName(roomName)
+ if err != nil {
+ return c.Redirect(http.StatusFound, "/")
+ }
+
+ quit := closeSignalChan(c)
+
if err := usersStreamsManager.Add(authUser.ID); err != nil {
return nil
}
@@ -5030,8 +5016,6 @@ Loop:
select {
case <-quit:
break Loop
- case <-quit1:
- break Loop
default:
}
@@ -5047,7 +5031,7 @@ Loop:
c.Response().Flush()
indicatorSelector = !indicatorSelector
- _, msgTyp, err := sub.ReceiveTimeout2(5*time.Second, quit, quit1)
+ _, msgTyp, err := sub.ReceiveTimeout2(5*time.Second, quit)
if err != nil {
if err == pubsub.ErrCancelled {
break Loop