Просмотр исходного кода

tweak: clean up Socket.Close()

tags/v0.11.0-beta
Shivaram Lingamneni 6 лет назад
Родитель
Сommit
8fd1446627
1 измененных файлов: 12 добавлений и 16 удалений
  1. 12
    16
      irc/socket.go

+ 12
- 16
irc/socket.go Просмотреть файл

@@ -52,18 +52,11 @@ func NewSocket(conn net.Conn, maxSendQBytes uint64) Socket {
52 52
 
53 53
 // Close stops a Socket from being able to send/receive any more data.
54 54
 func (socket *Socket) Close() {
55
-	alreadyClosed := func() bool {
56
-		socket.Lock()
57
-		defer socket.Unlock()
58
-		result := socket.closed
59
-		socket.closed = true
60
-		return result
61
-	}()
62
-
63
-	if !alreadyClosed {
64
-		// force close loop to happen if it hasn't already
65
-		socket.Write("")
66
-	}
55
+	socket.Lock()
56
+	socket.closed = true
57
+	socket.Unlock()
58
+
59
+	socket.wakeWriter()
67 60
 }
68 61
 
69 62
 // CertFP returns the fingerprint of the certificate provided by the client.
@@ -131,14 +124,17 @@ func (socket *Socket) Write(data string) (err error) {
131 124
 	}
132 125
 	socket.Unlock()
133 126
 
134
-	// notify the consumer that data is available
127
+	socket.wakeWriter()
128
+	return
129
+}
130
+
131
+// wakeWriter wakes up the goroutine that actually performs the write, without blocking
132
+func (socket *Socket) wakeWriter() {
133
+	// nonblocking send to the channel, no-op if it's full
135 134
 	select {
136 135
 	case socket.lineToSendExists <- true:
137 136
 	default:
138
-		// a notification is already in the queue, this is fine
139 137
 	}
140
-
141
-	return
142 138
 }
143 139
 
144 140
 // SetFinalData sets the final data to send when the SocketWriter closes.

Загрузка…
Отмена
Сохранить