MCPcopy Create free account
hub / github.com/coder/coder / unaryRPC

Method unaryRPC

vpn/speaker.go:191–221  ·  view source on GitHub ↗

unaryRPC sends a request/response style RPC over the protocol, waits for the response, then returns the response

(ctx context.Context, req S)

Source from the content-addressed store, hash-verified

189// unaryRPC sends a request/response style RPC over the protocol, waits for the response, then
190// returns the response
191func (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
223func (s *speaker[_, R, _]) newRPC() (uint64, chan R) {
224 s.mu.Lock()

Calls 6

newRPCMethod · 0.95
rmResponseChanMethod · 0.95
ErrMethod · 0.80
EnsureRPCMethod · 0.65
DoneMethod · 0.45
ErrorfMethod · 0.45