unaryRPC sends a request/response style RPC over the protocol, waits for the response, then returns the response
(ctx context.Context, req S)
| 189 | // unaryRPC sends a request/response style RPC over the protocol, waits for the response, then |
| 190 | // returns the response |
| 191 | func (s *speaker[S, R, _]) unaryRPC(ctx context.Context, req S) (resp R, err error) { |
| 192 | rpc := req.EnsureRPC() |
| 193 | msgID, respCh := s.newRPC() |
| 194 | rpc.MsgId = msgID |
| 195 | logger := s.logger.With(slog.F("msg_id", msgID)) |
| 196 | select { |
| 197 | case <-ctx.Done(): |
| 198 | return resp, ctx.Err() |
| 199 | case <-s.ctx.Done(): |
| 200 | return resp, xerrors.Errorf("vpn protocol closed: %w", s.ctx.Err()) |
| 201 | case <-s.recvLoopDone: |
| 202 | logger.Debug(s.ctx, "recvLoopDone while sending request") |
| 203 | return resp, io.ErrUnexpectedEOF |
| 204 | case s.sendCh <- req: |
| 205 | logger.Debug(s.ctx, "sent rpc request", slog.F("req", req)) |
| 206 | } |
| 207 | select { |
| 208 | case <-ctx.Done(): |
| 209 | s.rmResponseChan(msgID) |
| 210 | return resp, ctx.Err() |
| 211 | case <-s.ctx.Done(): |
| 212 | s.rmResponseChan(msgID) |
| 213 | return resp, xerrors.Errorf("vpn protocol closed: %w", s.ctx.Err()) |
| 214 | case <-s.recvLoopDone: |
| 215 | logger.Debug(s.ctx, "recvLoopDone while waiting for response") |
| 216 | return resp, io.ErrUnexpectedEOF |
| 217 | case resp = <-respCh: |
| 218 | logger.Debug(s.ctx, "got response", slog.F("resp", resp)) |
| 219 | return resp, nil |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | func (s *speaker[_, R, _]) newRPC() (uint64, chan R) { |
| 224 | s.mu.Lock() |