```go
func serverWriter(s *Server, w io.Writer, clientAddr string, responsesChan <-chan *serverMessage, stopChan <-chan struct{}, done chan<- struct{}, enabledCompression bool) {
defer func() { close(done) }()
e := newMessageEncoder(w, s.SendBufferSize, enabledCompression, &s.Stats)
defer e.Close()
var flushChan <-chan time.Time
t := time.NewTimer(s.FlushDelay)
var wr wireResponse
for {
var m *serverMessage
select {
case m = <-responsesChan:
default:
// Give the last chance for ready goroutines filling responsesChan :)
runtime.Gosched()
select {
case <-stopChan:
return
case m = <-responsesChan:
case <-flushChan:
if err := e.Flush(); err != nil {
if !isServerStop(stopChan) {
s.LogError("gorpc.Server: [%s]->[%s]: Cannot flush responses to underlying stream: [%s]", clientAddr, s.Addr, err)
}
return
}
flushChan = nil
continue
}
}
if flushChan == nil {
flushChan = getFlushChan(t, s.FlushDelay)
}
wr.ID = m.ID
wr.Response = m.Response
wr.Error = m.Error
m.Response = nil
m.Error = ""
serverMessagePool.Put(m)
if err := e.Encode(wr); err != nil {
s.LogError("gorpc.Server: [%s]->[%s]. Cannot send response to wire: [%s]", clientAddr, s.Addr, err)
return
}
wr.Response = nil
wr.Error = ""
s.Stats.incRPCCalls()
}
}
```
- 和Reader不同的是,没有宕机保护。相同的是,在函数结束时通过(close(done))通知上层。
- 申请了一个messageEncoder,对消息进行处理
- 接受从responsesChan接受来自Reader的消息。
- runtime.Gosched()让出时间片给Reader写入channel。并且接收stopChan通知结束。
- flushChan通过FlushDelay定义,主要是执行e.Flush()
- 同时定义了wireResponses交由Encode处理,并将用完的msg放回消息池中。
1. 接下来需要研究一下 decode和encoder
2. 同时需要研究一下gob编码,和实际的rpc处理内容