Skip to content

Add solution for Challenge 8 by imankhodadi - #1843

Open
imankhodadi wants to merge 6 commits into
RezaSi:mainfrom
imankhodadi:challenge-8-imankhodadi
Open

Add solution for Challenge 8 by imankhodadi#1843
imankhodadi wants to merge 6 commits into
RezaSi:mainfrom
imankhodadi:challenge-8-imankhodadi

Conversation

@imankhodadi

Copy link
Copy Markdown
Contributor

Challenge 8 Solution

Submitted by: @imankhodadi
Challenge: Challenge 8

Description

This PR contains my solution for Challenge 8.

Changes

  • Added solution file to challenge-8/submissions/imankhodadi/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds a new challenge8 package submission implementing a concurrent Go chat server. The implementation defines ChatServer and Client types with mutex-protected state, buffered message channels, exported sentinel errors, and methods for connecting/disconnecting clients, broadcasting to all clients, and sending private messages.

Changes

Challenge 8: Concurrent Go Chat Server

Layer / File(s) Summary
Types, sentinel errors, and Client message primitives
challenge-8/submissions/imankhodadi/solution-template.go
Defines ChatServer (server-level mutex, client map) and Client (per-client mutex, disconnected flag, buffered messages channel) structs. Exports ErrUsernameAlreadyTaken, ErrRecipientNotFound, and ErrClientDisconnected. Implements Client.Send with a disconnection guard and non-blocking channel send, and Client.Receive with closed-channel handling.
Server lifecycle: Connect and Disconnect
challenge-8/submissions/imankhodadi/solution-template.go
NewChatServer initializes the internal client map. Connect trims/validates the username, rejects duplicates via ErrUsernameAlreadyTaken, allocates a buffered channel, and registers the client. Disconnect no-ops on nil or mismatched entries, deletes from the registry, then marks the client disconnected and closes its channel under the client lock. isConnectedClient verifies pointer identity and disconnection state.
Broadcast and PrivateMessage routing
challenge-8/submissions/imankhodadi/solution-template.go
Broadcast optionally validates sender connection, prefixes the message with the sender's username when a sender is present, and calls Send on every registered client. PrivateMessage validates sender connection, looks up the recipient (returning ErrRecipientNotFound if absent), formats the message as a PM-tagged string including the sender's username, and returns ErrClientDisconnected on sender failure.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • RezaSi/go-interview-practice#630: Adds the same Challenge 8 chat server API (ChatServer/Client, Connect, Disconnect, Broadcast, PrivateMessage, sentinel errors) in a different submission's solution-template.go.
  • RezaSi/go-interview-practice#1834: Adds the same Challenge 8 chat server implementation in the same file path (challenge-8/submissions/imankhodadi/solution-template.go) with identical types and methods.
  • RezaSi/go-interview-practice#1788: Adds the same exported challenge8 chat server API with matching ChatServer/Client types, Connect/Disconnect/Broadcast/PrivateMessage behaviors, and sentinel errors.

Poem

🐇 A server of chats, now spun up with care,
Mutexes and channels fill the air,
Usernames checked, duplicates denied,
Messages broadcast far and wide!
Private whispers tagged with "PM" delight,
This bunny approves — concurrent and right! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately names the Challenge 8 solution submission and matches the change set.
Description check ✅ Passed The description is directly related and correctly states that this PR adds the Challenge 8 solution file.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch challenge-8-imankhodadi

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
challenge-8/submissions/imankhodadi/solution-template.go (1)

33-37: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Non-blocking send silently drops messages when the buffer is full.

The default branch discards the message if Messages (cap 100) is full, e.g. for a slow/non-consuming receiver. This is a reasonable choice to avoid blocking the broadcast path, but a slow client will silently miss messages. If acceptable for this challenge, no change is needed; otherwise consider surfacing or counting drops.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9ba81482-0e92-4c25-83f9-f89cccaad81f

📥 Commits

Reviewing files that changed from the base of the PR and between a4d6825 and 49b73df.

📒 Files selected for processing (1)
  • challenge-8/submissions/imankhodadi/solution-template.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant