Skip to content

Commit

Permalink
chore: refactor naming var that ambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
elskow committed Oct 21, 2024
1 parent 92c719b commit ef3f834
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
14 changes: 7 additions & 7 deletions editor-cp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ type Server struct {
logger *zap.Logger
}

func (s *Server) getLogger(ctx context.Context) *zap.Logger {
if requestID, ok := ctx.Value("requestID").(string); ok {
return s.logger.With(zap.String("requestID", requestID))
}
return s.logger
}

type Room struct {
clients map[*websocket.Conn]bool
clientsMutex sync.RWMutex
Expand Down Expand Up @@ -213,3 +206,10 @@ func (s *Server) Shutdown(ctx context.Context) error {
return err
}
}

func (s *Server) getLogger(ctx context.Context) *zap.Logger {
if requestID, ok := ctx.Value("requestID").(string); ok {
return s.logger.With(zap.String("requestID", requestID))
}
return s.logger
}
8 changes: 0 additions & 8 deletions videochat-cp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ func main() {
}))

app.Use(requestid.New())

app.Use(func(c *fiber.Ctx) error {
requestID := c.GetRespHeader("X-Request-Id")
ctx := context.WithValue(c.Context(), "requestID", requestID)
c.SetUserContext(ctx)
return c.Next()
})

app.Use(recover.New())
app.Use(healthcheck.New())
app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))
Expand Down
29 changes: 16 additions & 13 deletions videochat-cp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,14 @@ type Server struct {
logger *zap.Logger
}

func (s *Server) getLogger(ctx context.Context) *zap.Logger {
if requestID, ok := ctx.Value("requestID").(string); ok {
return s.logger.With(zap.String("requestID", requestID))
}
return s.logger
}

func NewServer(app *fiber.App, logger *zap.Logger) *Server {
server := &Server{
app: app,
rooms: make(map[string]*Room),
logger: logger,
}

go server.removeInactiveClients()
go server.cleanupInactiveClients()

return server
}
Expand Down Expand Up @@ -70,7 +63,7 @@ func (s *Server) handleWebSocket(c *websocket.Conn) {
s.addClientToRoom(roomID, c)
logger.Info("Client connected to room", zap.String("roomID", roomID))

go s.handleICECandidates(ctx, c, pc, roomID)
go s.manageICECandidateEvents(ctx, c, pc, roomID)
s.handleIncomingMessages(ctx, c, pc, roomID)

c.SetCloseHandler(func(code int, text string) error {
Expand Down Expand Up @@ -126,7 +119,7 @@ func (s *Server) removeClientFromRoom(roomID string, c *websocket.Conn) {
}
}

func (s *Server) removeInactiveClients() {
func (s *Server) cleanupInactiveClients() {
ticker := time.NewTicker(5 * time.Minute)
defer ticker.Stop()

Expand All @@ -153,7 +146,7 @@ func (s *Server) removeInactiveClients() {
}
}

func (s *Server) handleICECandidates(ctx context.Context, c *websocket.Conn, pc *webrtc.PeerConnection, roomID string) {
func (s *Server) manageICECandidateEvents(ctx context.Context, c *websocket.Conn, pc *webrtc.PeerConnection, roomID string) {
logger := s.getLogger(ctx)

pc.OnICECandidate(func(candidate *webrtc.ICECandidate) {
Expand Down Expand Up @@ -186,6 +179,7 @@ func (s *Server) handleIncomingMessages(ctx context.Context, c *websocket.Conn,
if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
return
}
logger.Error("Failed to read message", zap.Error(err))
return
}

Expand All @@ -201,7 +195,7 @@ func (s *Server) handleIncomingMessages(ctx context.Context, c *websocket.Conn,
s.handleSDP(ctx, c, pc, string(sdpStr))
} else if candidate, ok := signal["candidate"].(map[string]interface{}); ok {
candidateStr, _ := json.Marshal(candidate)
s.handleICECandidate(ctx, pc, string(candidateStr))
s.addRemoteICECandidate(ctx, pc, string(candidateStr))
}

s.broadcastMessageToRoom(ctx, roomID, c, msg)
Expand All @@ -219,7 +213,9 @@ func (s *Server) broadcastMessageToRoom(ctx context.Context, roomID string, send

for client := range room.clients {
if client != sender {
client.WriteMessage(websocket.TextMessage, message)
if err := client.WriteMessage(websocket.TextMessage, message); err != nil {
s.logger.Error("Failed to write message to client", zap.Error(err))
}
}
}
}
Expand Down Expand Up @@ -249,3 +245,10 @@ func (s *Server) Shutdown(ctx context.Context) error {
return err
}
}

func (s *Server) getLogger(ctx context.Context) *zap.Logger {
if requestID, ok := ctx.Value("requestID").(string); ok {
return s.logger.With(zap.String("requestID", requestID))
}
return s.logger
}
6 changes: 4 additions & 2 deletions videochat-cp/webrtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *Server) handleSDP(ctx context.Context, c *websocket.Conn, pc *webrtc.Pe
}
}

func (s *Server) handleICECandidate(ctx context.Context, pc *webrtc.PeerConnection, candidateStr string) {
func (s *Server) addRemoteICECandidate(ctx context.Context, pc *webrtc.PeerConnection, candidateStr string) {
logger := s.getLogger(ctx)

var candidate webrtc.ICECandidateInit
Expand All @@ -71,5 +71,7 @@ func (s *Server) handleICECandidate(ctx context.Context, pc *webrtc.PeerConnecti
return
}

pc.AddICECandidate(candidate)
if err := pc.AddICECandidate(candidate); err != nil {
logger.Error("Failed to add ICE candidate", zap.Error(err))
}
}

0 comments on commit ef3f834

Please sign in to comment.