refactor: replace custom array search utility with slices.Contains for improved readability and performance.

This commit is contained in:
Miroslav Šedivý
2026-08-04 12:49:38 +02:00
parent d2ec13a236
commit db8452416b
6 changed files with 18 additions and 32 deletions
+8 -11
View File
@@ -2,12 +2,11 @@ package config
import (
"path"
"slices"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/m1k1o/neko/server/pkg/utils"
)
type Server struct {
@@ -121,8 +120,7 @@ func (s *Server) Set() {
s.Metrics = viper.GetBool("server.metrics")
s.CORS = viper.GetStringSlice("server.cors")
in, _ := utils.ArrayIn("*", s.CORS)
if len(s.CORS) == 0 || in {
if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") {
s.CORS = []string{"*"}
}
}
@@ -162,8 +160,7 @@ func (s *Server) SetV2() {
}
if viper.IsSet("cors") {
s.CORS = viper.GetStringSlice("cors")
in, _ := utils.ArrayIn("*", s.CORS)
if len(s.CORS) == 0 || in {
if len(s.CORS) == 0 || slices.Contains(s.CORS, "*") {
s.CORS = []string{"*"}
}
log.Warn().Msg("you are using v2 configuration 'NEKO_CORS' which is deprecated, please use 'NEKO_SERVER_CORS' instead")
@@ -182,12 +179,12 @@ func (s *Server) HasCors() bool {
}
func (s *Server) AllowOrigin(origin string) bool {
// if CORS is disabled, allow all origins
if len(s.CORS) == 0 {
// if CORS is not configured, or a wildcard is present,
// allow WebSocket connections from any origin
if len(s.CORS) == 0 || s.CORS[0] == "*" {
return true
}
// if CORS is enabled, allow only origins in the list
in, _ := utils.ArrayIn(origin, s.CORS)
return in || s.CORS[0] == "*"
// allow only origins in the list
return slices.Contains(s.CORS, origin)
}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"net/http"
"slices"
"strings"
"github.com/m1k1o/neko/server/pkg/types"
@@ -55,7 +56,7 @@ func (b *batchHandler) Handle(w http.ResponseWriter, r *http.Request) error {
continue
}
if exists, _ := utils.ArrayIn(request.Path, b.Excluded); exists {
if slices.Contains(b.Excluded, request.Path) {
res.Error(utils.HttpBadRequest("this path is excluded from batch requests"))
responses[i] = res
continue
+4 -3
View File
@@ -2,6 +2,7 @@ package session
import (
"errors"
"slices"
"sync"
"sync/atomic"
"time"
@@ -303,7 +304,7 @@ func (manager *SessionManagerCtx) Broadcast(event string, payload any, exclude .
}
if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
if slices.Contains(exclude, session.ID()) {
continue
}
}
@@ -319,7 +320,7 @@ func (manager *SessionManagerCtx) AdminBroadcast(event string, payload any, excl
}
if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
if slices.Contains(exclude, session.ID()) {
continue
}
}
@@ -335,7 +336,7 @@ func (manager *SessionManagerCtx) InactiveCursorsBroadcast(event string, payload
}
if len(exclude) > 0 {
if in, _ := utils.ArrayIn(session.ID(), exclude); in {
if slices.Contains(exclude, session.ID()) {
continue
}
}
+2 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"net/http"
"slices"
"sync"
"time"
@@ -333,7 +334,7 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer type
}
// log events if not ignored
if ok, _ := utils.ArrayIn(data.Event, nologEvents); !ok {
if !slices.Contains(nologEvents, data.Event) {
payload := data.Payload
if len(payload) > maxPayloadLogLength {
payload = []byte("<truncated>")
+2 -2
View File
@@ -3,6 +3,7 @@ package websocket
import (
"encoding/json"
"errors"
"slices"
"sync"
"github.com/gorilla/websocket"
@@ -11,7 +12,6 @@ import (
"github.com/m1k1o/neko/server/pkg/types"
"github.com/m1k1o/neko/server/pkg/types/event"
"github.com/m1k1o/neko/server/pkg/types/message"
"github.com/m1k1o/neko/server/pkg/utils"
)
type WebSocketPeerCtx struct {
@@ -51,7 +51,7 @@ func (peer *WebSocketPeerCtx) Send(event string, payload any) {
}
// log events if not ignored
if ok, _ := utils.ArrayIn(event, nologEvents); !ok {
if !slices.Contains(nologEvents, event) {
if len(raw) > maxPayloadLogLength {
raw = []byte("<truncated>")
}
-14
View File
@@ -1,14 +0,0 @@
package utils
func ArrayIn[T comparable](val T, array []T) (exists bool, index int) {
exists, index = false, -1
for i, a := range array {
if a == val {
exists, index = true, i
return
}
}
return
}