From 90aecdeb14078457f59594c966b3958470f411d4 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 24 Jun 2024 16:50:41 -0300 Subject: src/lib.go: Add simplistic scaffold of the connection WriteLoop() --- src/lib.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/lib.go b/src/lib.go index 60b0742..2bcb98c 100644 --- a/src/lib.go +++ b/src/lib.go @@ -7,6 +7,7 @@ import ( "errors" "flag" "fmt" + "io" "io/ioutil" "log/slog" "net" @@ -31,9 +32,10 @@ var ( Colour string ) -var EmitActiveConnection = g.MakeGauge("active-connections") -var EmitNicksInChannel = g.MakeGauge("nicks-in-channel") -var EmitReceivedMessage = g.MakeCounter("received-message") +var EmitActiveConnection = g.MakeGauge("active-connections") +var EmitNicksInChannel = g.MakeGauge("nicks-in-channel") +var EmitReceivedMessage = g.MakeCounter("received-message") +var EmitWriteToClientError = g.MakeCounter("write-to-client") const pingFrequency = time.Duration(30) * time.Second const pongMaxLatency = time.Duration(5) * time.Second @@ -50,6 +52,7 @@ type Context struct { type Connection struct { conn net.Conn + replyChan chan string // id *UUID id string isAuthenticated bool @@ -281,7 +284,21 @@ func ReadLoop(ctx *Context, connection *Connection) { } func WriteLoop(ctx *Context, connection *Connection) { - // fmt.Println("WriteLoop") + for message := range connection.replyChan { + _, err := io.WriteString(connection.conn, message) + if err != nil { + g.Error( + "Failed to send data to user", + "user-reply-error", + "err", err, + ) + EmitWriteToClientError() + continue + } + } + + EmitActiveConnection.Dec() + connection.conn.Close() } func PingLoop(ctx *Context, connection *Connection) { -- cgit v1.2.3