| 149 | } |
| 150 | |
| 151 | func (r *rpcStream) Close() error { |
| 152 | r.Lock() |
| 153 | |
| 154 | select { |
| 155 | case <-r.closed: |
| 156 | r.Unlock() |
| 157 | return nil |
| 158 | default: |
| 159 | close(r.closed) |
| 160 | r.Unlock() |
| 161 | |
| 162 | // send the end of stream message |
| 163 | if r.sendEOS { |
| 164 | // no need to check for error |
| 165 | //nolint:errcheck,gosec |
| 166 | r.codec.Write(&codec.Message{ |
| 167 | Id: r.id, |
| 168 | Target: r.request.Service(), |
| 169 | Method: r.request.Method(), |
| 170 | Endpoint: r.request.Endpoint(), |
| 171 | Type: codec.Error, |
| 172 | Error: lastStreamResponseError, |
| 173 | }, nil) |
| 174 | } |
| 175 | |
| 176 | err := r.codec.Close() |
| 177 | |
| 178 | rerr := r.Error() |
| 179 | if r.close && rerr == nil { |
| 180 | rerr = errors.New("connection header set to close") |
| 181 | } |
| 182 | // release the connection |
| 183 | r.release(rerr) |
| 184 | |
| 185 | return err |
| 186 | } |
| 187 | } |