server/client_output.go (3546B)
1 package main 2 3 import ( 4 "fmt" 5 "io" 6 "log" 7 "net" 8 "sync" 9 "time" 10 ) 11 12 type clientOutput struct { 13 mutex sync.Mutex 14 outgoing []byte 15 pendingBytes int 16 writeWake chan struct{} 17 writeDone chan struct{} 18 writeClosed bool 19 writeDiscarded bool 20 } 21 22 func (client *clientOutput) startWriter(conn net.Conn) { 23 // Start once, before this connection is published to other sessions. 24 client.writeWake = make(chan struct{}, 1) 25 client.writeDone = make(chan struct{}) 26 go client.writePackets(conn) 27 } 28 29 func (client *clientOutput) finishWriter() { 30 client.mutex.Lock() 31 client.writeClosed = true 32 select { 33 case client.writeWake <- struct{}{}: 34 default: 35 } 36 client.mutex.Unlock() 37 <-client.writeDone 38 } 39 40 func (client *clientOutput) disconnect(conn net.Conn) { 41 client.mutex.Lock() 42 defer client.mutex.Unlock() 43 // Standalone protocol fixtures have no writer; live sessions start one first. 44 if client.writeWake == nil { 45 conn.Close() 46 return 47 } 48 client.discard_locked() 49 } 50 51 func (client *clientOutput) discard_locked() { 52 // The writer owns socket shutdown, including any transport close handshake. 53 client.writeClosed = true 54 client.writeDiscarded = true 55 client.outgoing = nil 56 client.pendingBytes = 0 57 select { 58 case client.writeWake <- struct{}{}: 59 default: 60 } 61 } 62 63 func writeClientPacket(conn net.Conn, packet []byte) error { 64 if err := conn.SetWriteDeadline(time.Now().Add(5 * time.Second)); err != nil { 65 return err 66 } 67 count, err := conn.Write(packet) 68 if err == nil && count != len(packet) { 69 return io.ErrShortWrite 70 } 71 return err 72 } 73 74 func (client *clientOutput) enqueue_locked(conn net.Conn, packet []byte) error { 75 // The caller holds mutex; the queue copies the packet before returning. 76 // Standalone protocol fixtures have no writer; live sessions start one first. 77 if client.writeWake == nil { 78 return writeClientPacket(conn, packet) 79 } 80 if client.writeClosed { 81 return net.ErrClosed 82 } 83 // Include the detached batch so a slow transport cannot double the limit. 84 if client.pendingBytes+len(packet) > 8*1024*1024 { 85 client.discard_locked() 86 return fmt.Errorf("client output queue full") 87 } 88 client.outgoing = append(client.outgoing, packet...) 89 client.pendingBytes += len(packet) 90 select { 91 case client.writeWake <- struct{}{}: 92 default: 93 } 94 return nil 95 } 96 97 func (client *clientOutput) writePackets(conn net.Conn) { 98 defer func() { 99 client.mutex.Lock() 100 client.writeClosed = true 101 client.outgoing = nil 102 client.pendingBytes = 0 103 client.mutex.Unlock() 104 conn.Close() 105 close(client.writeDone) 106 }() 107 108 // Swap buffers so producers can enqueue while the writer sends a batch. Reuse 109 // small buffers, but release large admission snapshots after they are sent. 110 var buffer []byte 111 for range client.writeWake { 112 for { 113 client.mutex.Lock() 114 if len(client.outgoing) == 0 { 115 closed := client.writeClosed 116 client.mutex.Unlock() 117 if closed { 118 return 119 } 120 break 121 } 122 buffer, client.outgoing = client.outgoing, buffer[:0] 123 client.mutex.Unlock() 124 for offset := 0; offset < len(buffer); offset += 32768 { 125 client.mutex.Lock() 126 discarded := client.writeDiscarded 127 client.mutex.Unlock() 128 if discarded { 129 return 130 } 131 end := min(offset+32768, len(buffer)) 132 if err := writeClientPacket(conn, buffer[offset:end]); err != nil { 133 log.Printf("Client output: %v", err) 134 return 135 } 136 client.mutex.Lock() 137 if !client.writeDiscarded { 138 client.pendingBytes -= end - offset 139 } 140 client.mutex.Unlock() 141 } 142 if cap(buffer) > 8192 { 143 buffer = nil 144 } 145 } 146 } 147 }