We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi @gobwas, thank you for creating awesome example ! I have a question about your sample code.
https://github.com/gobwas/ws-examples/blob/master/src/chat/chat.go#L79-L80
func (u *User) readRequest() (*Request, error) { u.io.Lock() defer u.io.Unlock() ... }
Why we need lock when read ? if I put the Receive (contain readRequest) method in for-loop. (I am not using poller.Start(desc, func(ev netpoll.Event))
Receive
readRequest
poller.Start(desc, func(ev netpoll.Event)
func (s *Socket) Start(conn net.Conn, name string) { // Register incoming user in chat. user := s.chat.Register(conn) go func() { retry := 0 defer conn.Close() defer func() { if r := recover(); r != nil { log.Println(r) } }() for { err := user.Receive() if err != nil { // When receive failed, we can only disconnect broken // connection and stop to receive events about it. log.Printf("Read user [%s] - client [%s] text failed: %v\n", user.Name, name, err) goto disconnect } log.Println("User receive successful") disconnect: if retry = retry + 1; retry > 3 { log.Printf("Remove user [%s] - client [%s]\n", user.Name, name) s.chat.Remove(user) break } delay := time.Millisecond log.Printf("Start receive error: %v; retrying in %s", err, delay) time.Sleep(delay) } }() }
===> its seem block write method such as
https://github.com/gobwas/ws-examples/blob/master/src/chat/chat.go#L135-L136
func (u *User) writeRaw(p []byte) error { u.io.Lock() defer u.io.Unlock() ... }
because readRequest have made u.io locked, and writeRaw can not aquire this lock.
writeRaw
If I remove u.io.Lock() in readRequest method, Have it any problem?
u.io.Lock()
Thank you !
The text was updated successfully, but these errors were encountered:
https://golang.org/pkg/net/#Conn mention
Conn is a generic stream-oriented network connection. Multiple goroutines may invoke methods on a Conn simultaneously.
Sorry, something went wrong.
No branches or pull requests
Hi @gobwas, thank you for creating awesome example !
I have a question about your sample code.
https://github.com/gobwas/ws-examples/blob/master/src/chat/chat.go#L79-L80
Why we need lock when read ? if I put the
Receive
(containreadRequest
) method in for-loop. (I am not usingpoller.Start(desc, func(ev netpoll.Event)
)===> its seem block write method such as
https://github.com/gobwas/ws-examples/blob/master/src/chat/chat.go#L135-L136
because
readRequest
have made u.io locked, andwriteRaw
can not aquire this lock.If I remove
u.io.Lock()
inreadRequest
method, Have it any problem?Thank you !
The text was updated successfully, but these errors were encountered: