Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore repeated TransferSIPParticipant RPC request with same parameters #195

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions pkg/sip/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package sip

import (
"context"
"sync"
"time"

"github.com/emiago/sipgo"
Expand All @@ -36,18 +37,27 @@ type Service struct {
mon *stats.Monitor
cli *Client
srv *Server

mu sync.Mutex
pendingTransfers map[transferKey]struct{}
}

type transferKey struct {
SipCallId string
TransferTo string
}

func NewService(conf *config.Config, mon *stats.Monitor, log logger.Logger) *Service {
if log == nil {
log = logger.GetLogger()
}
s := &Service{
conf: conf,
log: log,
mon: mon,
cli: NewClient(conf, log, mon),
srv: NewServer(conf, log, mon),
conf: conf,
log: log,
mon: mon,
cli: NewClient(conf, log, mon),
srv: NewServer(conf, log, mon),
pendingTransfers: make(map[transferKey]struct{}),
}
return s
}
Expand Down Expand Up @@ -127,6 +137,25 @@ func (s *Service) TransferSIPParticipant(ctx context.Context, req *rpc.InternalT
ctx, done := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
defer done()

s.mu.Lock()
k := transferKey{
SipCallId: req.SipCallId,
TransferTo: req.TransferTo,
}
if _, ok := s.pendingTransfers[k]; ok {
s.mu.Unlock()
s.log.Debugw("repeated request for call transfer", "callID", req.SipCallId, "transferTo", req.TransferTo)
return &emptypb.Empty{}, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what are the semantics here for PSRPC. Will it unblock earlier if the second request immediately succeeds by going this code path? Maybe use map[transferKey]<-chan struct{} and wait here for the channel to close?

Also, you intentionally remove cancellation from the original context, which is desired for the main call. But maybe it's worth preserving the original context in a separate var, so that you can then select on this new done channel + the context. I assume PSRPC will cancel the request after a timeout. So at least the follow-up requests will be unblocked.

}
s.pendingTransfers[k] = struct{}{}
s.mu.Unlock()

defer func() {
s.mu.Lock()
delete(s.pendingTransfers, k)
s.mu.Unlock()
}()

// Look for call both in client (outbound) and server (inbound)
s.cli.cmu.Lock()
out := s.cli.activeCalls[LocalTag(req.SipCallId)]
Expand Down
Loading