mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 16:01:13 +00:00
[restore-vowifi-25.08] retain late SMS final responses
This commit is contained in:
1 parent
adfac4b966
commit
3411f0a2cd
11 files changed
+484
-50
No files matched your search
@@ -23,8 +23,12 @@ func (s *Service) runOutboundMessageDispatcher() {
|
||||
logging.RunDebug("IMS outbound MESSAGE",
|
||||
"flow", task.flow, "call_id", outboundRequestCallID(task.req),
|
||||
"sip", outboundRequestDebugText(task.req))
|
||||
response, seq, err := s.dispatchOutboundRequest(
|
||||
task.ctx, task.flow, task.req, time.Duration(task.timeout), true,
|
||||
response, seq, err := s.dispatchOutboundRequestWithCallbacks(
|
||||
outboundDispatchOptions{
|
||||
Context: task.ctx, Flow: task.flow, Request: task.req,
|
||||
Timeout: time.Duration(task.timeout), Callbacks: task.callbacks,
|
||||
},
|
||||
true,
|
||||
)
|
||||
result := outboundMessageResult{DispatchSeq: seq}
|
||||
if response != nil {
|
||||
@@ -58,20 +62,30 @@ func (s *Service) dispatchOutboundMESSAGE(
|
||||
req *sip.Request,
|
||||
timeout time.Duration,
|
||||
) (outboundMessageResult, error) {
|
||||
if req == nil {
|
||||
return s.dispatchOutboundMESSAGEWithCallbacks(
|
||||
outboundDispatchOptions{Context: ctx, Flow: flow, Request: req, Timeout: timeout},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Service) dispatchOutboundMESSAGEWithCallbacks(
|
||||
options outboundDispatchOptions,
|
||||
) (outboundMessageResult, error) {
|
||||
if options.Request == nil {
|
||||
return outboundMessageResult{}, errors.New("imscore: nil outbound MESSAGE")
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
if options.Context == nil {
|
||||
options.Context = context.Background()
|
||||
}
|
||||
s.ensureOutboundRequestDispatchers()
|
||||
task := outboundMessageTask{
|
||||
ctx: ctx, flow: flow, req: req.Clone(), timeout: int64(timeout),
|
||||
done: make(chan outboundMessageReply, 1),
|
||||
ctx: options.Context, flow: options.Flow,
|
||||
req: options.Request.Clone(), timeout: int64(options.Timeout),
|
||||
callbacks: options.Callbacks,
|
||||
done: make(chan outboundMessageReply, 1),
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return outboundMessageResult{}, ctx.Err()
|
||||
case <-options.Context.Done():
|
||||
return outboundMessageResult{}, options.Context.Err()
|
||||
case <-s.stop:
|
||||
return outboundMessageResult{}, errors.New("imscore: service stopped")
|
||||
case s.outboundMsgCh <- task:
|
||||
@@ -82,8 +96,8 @@ func (s *Service) dispatchOutboundMESSAGE(
|
||||
select {
|
||||
case reply := <-task.done:
|
||||
return reply.result, reply.err
|
||||
case <-ctx.Done():
|
||||
return outboundMessageResult{}, ctx.Err()
|
||||
case <-options.Context.Done():
|
||||
return outboundMessageResult{}, options.Context.Err()
|
||||
case <-s.stop:
|
||||
return outboundMessageResult{}, errors.New("imscore: service stopped")
|
||||
}
|
||||
|
||||
@@ -26,10 +26,11 @@ type directTCPWriteOptions struct {
|
||||
}
|
||||
|
||||
type outboundSendOperation struct {
|
||||
Context context.Context
|
||||
Mode outboundModeContext
|
||||
Request *sip.Request
|
||||
Timeout time.Duration
|
||||
Context context.Context
|
||||
Mode outboundModeContext
|
||||
Request *sip.Request
|
||||
Timeout time.Duration
|
||||
Callbacks sipTransactionCallbacks
|
||||
}
|
||||
|
||||
func transportForRequest(configuredTransport, securityMode string, method sip.RequestMethod) string {
|
||||
@@ -194,6 +195,9 @@ func (s *Service) sendByMode(operation outboundSendOperation) (*sipResponse, err
|
||||
}
|
||||
setOutboundDestination(built, operation.Mode)
|
||||
if operation.Mode.Client != nil {
|
||||
if operation.Callbacks.onLateFinal != nil {
|
||||
return nil, errors.New("late SIP final retention is unavailable through sipgo client mode")
|
||||
}
|
||||
response, err := operation.Mode.Client.Do(operation.Context, built)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -206,7 +210,9 @@ func (s *Service) sendByMode(operation outboundSendOperation) (*sipResponse, err
|
||||
)
|
||||
}
|
||||
sender := func(raw string) error { return operation.Mode.send(operation.Context, raw) }
|
||||
return s.transport.roundTripWithSender(operation.Context, built.String(), sender)
|
||||
return s.transport.roundTripWithSenderAndCallbacks(
|
||||
operation.Context, built.String(), sender, operation.Callbacks,
|
||||
)
|
||||
}
|
||||
|
||||
func setOutboundDestination(request *sip.Request, modeCtx outboundModeContext) {
|
||||
|
||||
+44
-25
@@ -68,7 +68,10 @@ func (s *Service) runOutboundRequestTask(shard int, task outboundRequestTask) {
|
||||
logging.WarnRate("smsip_outbound_queue_wait_high", "IMS outbound request queue delay is high",
|
||||
"shard", shard, "delay", delay, "dispatch_seq", task.dispatchSeq)
|
||||
}
|
||||
response, err := s.executeOutboundRequest(task.ctx, task.req, task.modeCtx, time.Duration(task.timeout))
|
||||
response, err := s.executeOutboundRequestWithCallbacks(outboundSendOperation{
|
||||
Context: task.ctx, Mode: task.modeCtx, Request: task.req,
|
||||
Timeout: time.Duration(task.timeout), Callbacks: task.callbacks,
|
||||
})
|
||||
reply := outboundRequestReply{res: response, err: err, dispatchSeq: task.dispatchSeq}
|
||||
if task.done == nil {
|
||||
return
|
||||
@@ -85,22 +88,28 @@ func (s *Service) executeOutboundRequest(
|
||||
modeCtx outboundModeContext,
|
||||
timeout time.Duration,
|
||||
) (*sip.Response, error) {
|
||||
if req == nil {
|
||||
return nil, errors.New("imscore: nil outbound request")
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
}
|
||||
response, err := s.sendByMode(outboundSendOperation{
|
||||
return s.executeOutboundRequestWithCallbacks(outboundSendOperation{
|
||||
Context: ctx, Mode: modeCtx, Request: req, Timeout: timeout,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) executeOutboundRequestWithCallbacks(
|
||||
operation outboundSendOperation,
|
||||
) (*sip.Response, error) {
|
||||
if operation.Request == nil {
|
||||
return nil, errors.New("imscore: nil outbound request")
|
||||
}
|
||||
if operation.Context == nil {
|
||||
operation.Context = context.Background()
|
||||
}
|
||||
if operation.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
operation.Context, cancel = context.WithTimeout(operation.Context, operation.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
response, err := s.sendByMode(operation)
|
||||
if err != nil {
|
||||
s.handleOutboundRequestError(req, err)
|
||||
s.handleOutboundRequestError(operation.Request, err)
|
||||
return nil, err
|
||||
}
|
||||
if response == nil {
|
||||
@@ -158,13 +167,22 @@ func (s *Service) dispatchOutboundRequest(
|
||||
timeout time.Duration,
|
||||
wait bool,
|
||||
) (*sip.Response, uint64, error) {
|
||||
if req == nil {
|
||||
return s.dispatchOutboundRequestWithCallbacks(
|
||||
outboundDispatchOptions{Context: ctx, Flow: flow, Request: req, Timeout: timeout}, wait,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Service) dispatchOutboundRequestWithCallbacks(
|
||||
options outboundDispatchOptions,
|
||||
wait bool,
|
||||
) (*sip.Response, uint64, error) {
|
||||
if options.Request == nil {
|
||||
return nil, 0, errors.New("imscore: nil outbound request")
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
if options.Context == nil {
|
||||
options.Context = context.Background()
|
||||
}
|
||||
modeCtx, err := s.resolveOutboundModeContext(flow, req)
|
||||
modeCtx, err := s.resolveOutboundModeContext(options.Flow, options.Request)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -178,13 +196,14 @@ func (s *Service) dispatchOutboundRequest(
|
||||
done = nil
|
||||
}
|
||||
task := outboundRequestTask{
|
||||
ctx: ctx, flow: flow, req: req.Clone(), timeout: int64(timeout),
|
||||
modeCtx: modeCtx, dispatchSeq: seq, enqueuedAt: time.Now(), done: done,
|
||||
ctx: options.Context, flow: options.Flow, req: options.Request.Clone(), timeout: int64(options.Timeout),
|
||||
modeCtx: modeCtx, callbacks: options.Callbacks,
|
||||
dispatchSeq: seq, enqueuedAt: time.Now(), done: done,
|
||||
}
|
||||
queue := shards[outboundDispatchShardIndex(outboundDispatchKey(req, flow), len(shards))]
|
||||
queue := shards[outboundDispatchShardIndex(outboundDispatchKey(options.Request, options.Flow), len(shards))]
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, seq, ctx.Err()
|
||||
case <-options.Context.Done():
|
||||
return nil, seq, options.Context.Err()
|
||||
case <-s.stop:
|
||||
return nil, seq, errors.New("imscore: service stopped")
|
||||
case queue <- task:
|
||||
@@ -198,8 +217,8 @@ func (s *Service) dispatchOutboundRequest(
|
||||
select {
|
||||
case reply := <-done:
|
||||
return reply.res, reply.dispatchSeq, reply.err
|
||||
case <-ctx.Done():
|
||||
return nil, seq, ctx.Err()
|
||||
case <-options.Context.Done():
|
||||
return nil, seq, options.Context.Err()
|
||||
case <-s.stop:
|
||||
return nil, seq, errors.New("imscore: service stopped")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package imscore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/iniwex5/vowifi-go/internal/vowifi/logging"
|
||||
)
|
||||
|
||||
func shouldRetainClientTransaction(ctx context.Context, transaction *clientSIPTransaction) bool {
|
||||
if ctx == nil || transaction == nil || transaction.invite {
|
||||
return false
|
||||
}
|
||||
callbacks := transaction.callbacks
|
||||
if callbacks.onLateFinal == nil || callbacks.retainFinalAfterContext == nil {
|
||||
return false
|
||||
}
|
||||
return callbacks.retainFinalAfterContext(context.Cause(ctx))
|
||||
}
|
||||
|
||||
func (t *sipTransport) waitLateClientTransaction(
|
||||
transaction *clientSIPTransaction,
|
||||
timers sipTransactionTimers,
|
||||
) {
|
||||
retention := transaction.callbacks.lateFinalRetention
|
||||
if retention <= 0 || retention > timers.bf {
|
||||
retention = timers.bf
|
||||
}
|
||||
timeout := time.NewTimer(retention)
|
||||
defer timeout.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case response := <-transaction.responses:
|
||||
if t.handleLateClientResponse(transaction, response, timers) {
|
||||
return
|
||||
}
|
||||
case err := <-transaction.terminated:
|
||||
t.failTransaction(transaction, err)
|
||||
return
|
||||
case <-timeout.C:
|
||||
t.abandonClientTransaction(transaction)
|
||||
return
|
||||
case <-t.closed:
|
||||
t.abandonClientTransaction(transaction)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *sipTransport) handleLateClientResponse(
|
||||
transaction *clientSIPTransaction,
|
||||
response *sipResponse,
|
||||
timers sipTransactionTimers,
|
||||
) bool {
|
||||
if response == nil {
|
||||
return false
|
||||
}
|
||||
if response.StatusCode < 200 {
|
||||
transaction.markProceeding()
|
||||
if err := notifyProvisional(transaction, response); err != nil {
|
||||
t.failTransaction(transaction, err)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
final, err := t.completeClientTransaction(transaction, response, timers)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if err := transaction.callbacks.onLateFinal(final); err != nil {
|
||||
logging.WarnRate("ims-late-sip-final-handler", "IMS late SIP final handler failed",
|
||||
"method", transaction.key.Method, "status", response.StatusCode, "err", err)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *sipTransport) abandonClientTransaction(transaction *clientSIPTransaction) {
|
||||
t.removeTransaction(transaction)
|
||||
transaction.finish()
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package imscore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSIPTransactionRetainsLateFinalWhenCallbackApproves(t *testing.T) {
|
||||
transport := newSIPTransport()
|
||||
t.Cleanup(func() { _ = transport.Close() })
|
||||
transport.SetSendFn(func(string) error { return nil })
|
||||
request := transactionRequest("MESSAGE", "late-final-call")
|
||||
probeTimeout := errors.New("probe timeout")
|
||||
finals := make(chan int, 1)
|
||||
callbacks := sipTransactionCallbacks{
|
||||
onLateFinal: func(response *sipResponse) error {
|
||||
finals <- response.StatusCode
|
||||
return nil
|
||||
},
|
||||
retainFinalAfterContext: func(cause error) bool { return errors.Is(cause, probeTimeout) },
|
||||
lateFinalRetention: 100 * time.Millisecond,
|
||||
}
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Millisecond, probeTimeout)
|
||||
defer cancel()
|
||||
_, err := transport.roundTripWithCallbacks(ctx, request, callbacks)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("round trip error = %v", err)
|
||||
}
|
||||
assertTransactionCount(t, transport, 1)
|
||||
|
||||
transport.DeliverResponse(transactionResponse(request, 202))
|
||||
select {
|
||||
case status := <-finals:
|
||||
if status != 202 {
|
||||
t.Fatalf("late final status = %d", status)
|
||||
}
|
||||
case <-time.After(transactionTestWait):
|
||||
t.Fatal("late final callback was not called")
|
||||
}
|
||||
waitTransactionCount(t, transport, 0)
|
||||
}
|
||||
|
||||
func TestSIPTransactionDoesNotRetainCallerCancellation(t *testing.T) {
|
||||
transport := newSIPTransport()
|
||||
t.Cleanup(func() { _ = transport.Close() })
|
||||
transport.SetSendFn(func(string) error { return nil })
|
||||
probeTimeout := errors.New("probe timeout")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := transport.roundTripWithCallbacks(
|
||||
ctx,
|
||||
transactionRequest("MESSAGE", "caller-cancel-call"),
|
||||
sipTransactionCallbacks{
|
||||
onLateFinal: func(*sipResponse) error { return nil },
|
||||
retainFinalAfterContext: func(cause error) bool {
|
||||
return errors.Is(cause, probeTimeout)
|
||||
},
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("round trip error = %v", err)
|
||||
}
|
||||
assertTransactionCount(t, transport, 0)
|
||||
}
|
||||
|
||||
func TestSIPTransactionLateFinalWindowAndCloseCleanWaiter(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
close bool
|
||||
}{
|
||||
{name: "window"},
|
||||
{name: "close", close: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
transport := newSIPTransport()
|
||||
transport.SetSendFn(func(string) error { return nil })
|
||||
probeTimeout := errors.New("probe timeout")
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Millisecond, probeTimeout)
|
||||
defer cancel()
|
||||
_, err := transport.roundTripWithCallbacks(
|
||||
ctx,
|
||||
transactionRequest("MESSAGE", "late-cleanup-"+test.name),
|
||||
sipTransactionCallbacks{
|
||||
onLateFinal: func(*sipResponse) error { return nil },
|
||||
retainFinalAfterContext: func(cause error) bool {
|
||||
return errors.Is(cause, probeTimeout)
|
||||
},
|
||||
lateFinalRetention: 20 * time.Millisecond,
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("round trip error = %v", err)
|
||||
}
|
||||
assertTransactionCount(t, transport, 1)
|
||||
if test.close {
|
||||
_ = transport.Close()
|
||||
}
|
||||
waitTransactionCount(t, transport, 0)
|
||||
_ = transport.Close()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceStopCleansRetainedLateFinalTransaction(t *testing.T) {
|
||||
service, err := New(&IMSConfig{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service.transport.SetSendFn(func(string) error { return nil })
|
||||
probeTimeout := errors.New("probe timeout")
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Millisecond, probeTimeout)
|
||||
defer cancel()
|
||||
_, err = service.transport.roundTripWithCallbacks(
|
||||
ctx,
|
||||
transactionRequest("MESSAGE", "service-stop-late-final"),
|
||||
sipTransactionCallbacks{
|
||||
onLateFinal: func(*sipResponse) error { return nil },
|
||||
retainFinalAfterContext: func(cause error) bool {
|
||||
return errors.Is(cause, probeTimeout)
|
||||
},
|
||||
lateFinalRetention: time.Second,
|
||||
},
|
||||
)
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("round trip error = %v", err)
|
||||
}
|
||||
assertTransactionCount(t, service.transport, 1)
|
||||
service.Stop()
|
||||
waitTransactionCount(t, service.transport, 0)
|
||||
}
|
||||
@@ -164,6 +164,10 @@ func (t *sipTransport) waitClientTransaction(
|
||||
return result, nil
|
||||
case <-ctxDone:
|
||||
if !transaction.invite {
|
||||
if shouldRetainClientTransaction(ctx, transaction) {
|
||||
go t.waitLateClientTransaction(transaction, timers)
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
return nil, t.failTransaction(transaction, ctx.Err())
|
||||
}
|
||||
cancelCause = ctx.Err()
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emiago/sipgo/sip"
|
||||
)
|
||||
@@ -17,8 +18,11 @@ type sipTransactionKey struct {
|
||||
}
|
||||
|
||||
type sipTransactionCallbacks struct {
|
||||
onProvisional func(*sipResponse) error
|
||||
onFinalRetransmit func(*sipResponse) error
|
||||
onProvisional func(*sipResponse) error
|
||||
onFinalRetransmit func(*sipResponse) error
|
||||
onLateFinal func(*sipResponse) error
|
||||
retainFinalAfterContext func(error) bool
|
||||
lateFinalRetention time.Duration
|
||||
}
|
||||
|
||||
func (t *sipTransport) RoundTrip(ctx context.Context, request string) (*sipResponse, error) {
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package imscore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type lateMOSIPFinalContext struct {
|
||||
messageID string
|
||||
part outboundSMSPart
|
||||
pending *smsPendingInfo
|
||||
}
|
||||
|
||||
func (s *Service) moSubmitTransactionCallbacks(
|
||||
messageID string,
|
||||
part outboundSMSPart,
|
||||
pending *smsPendingInfo,
|
||||
) sipTransactionCallbacks {
|
||||
lateContext := lateMOSIPFinalContext{messageID: messageID, part: part, pending: pending}
|
||||
return sipTransactionCallbacks{
|
||||
onLateFinal: func(response *sipResponse) error {
|
||||
return s.handleLateMOSIPFinal(lateContext, response)
|
||||
},
|
||||
retainFinalAfterContext: func(cause error) bool {
|
||||
return errors.Is(cause, errMOSMSFinalProbeTimeout)
|
||||
},
|
||||
lateFinalRetention: s.smsReportTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) handleLateMOSIPFinal(
|
||||
lateContext lateMOSIPFinalContext,
|
||||
response *sipResponse,
|
||||
) error {
|
||||
if response == nil {
|
||||
return errors.New("imscore: nil late MESSAGE final response")
|
||||
}
|
||||
if response.StatusCode >= 200 && response.StatusCode < 300 {
|
||||
return s.handleLateMOAccepted(lateContext, response.StatusCode)
|
||||
}
|
||||
reason := fmt.Sprintf(
|
||||
"MESSAGE rejected with status %d (%s)", response.StatusCode, strings.TrimSpace(response.Reason),
|
||||
)
|
||||
persistErr := s.persistOutboundSIPResult(
|
||||
lateContext.messageID, lateContext.part.number,
|
||||
response.StatusCode, smsDeliveryStateFailed, reason,
|
||||
)
|
||||
s.takePendingSMSByCallID(lateContext.part.callID)
|
||||
notifySMSPending(lateContext.pending, smsSendResult{
|
||||
Code: response.StatusCode, Status: smsDeliveryStateFailed,
|
||||
Reason: errors.Join(errors.New(reason), persistErr).Error(), At: time.Now(),
|
||||
})
|
||||
return persistErr
|
||||
}
|
||||
|
||||
func (s *Service) handleLateMOAccepted(
|
||||
lateContext lateMOSIPFinalContext,
|
||||
sipCode int,
|
||||
) error {
|
||||
state, err := s.persistAcceptedSIPPart(lateContext.messageID, lateContext.part, sipCode)
|
||||
if err != nil {
|
||||
s.takePendingSMSByCallID(lateContext.part.callID)
|
||||
notifySMSPending(lateContext.pending, smsSendResult{
|
||||
Code: sipCode, Status: smsDeliveryStateFailed, Reason: err.Error(), At: time.Now(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
notifySMSPending(lateContext.pending, smsSendResult{Code: sipCode, Status: state, At: time.Now()})
|
||||
s.expirePendingSMSAfter(lateContext.part.callID, pendingSMSReportRetention)
|
||||
return nil
|
||||
}
|
||||
|
||||
func notifySMSPending(pending *smsPendingInfo, result smsSendResult) {
|
||||
if pending == nil || pending.RespCh == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case pending.RespCh <- result:
|
||||
default:
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ const (
|
||||
smsDeliveryStateFailed = "failed"
|
||||
)
|
||||
|
||||
var errMOSMSFinalProbeTimeout = errors.New("MO SMS final response probe timeout")
|
||||
|
||||
type outboundSMSPart struct {
|
||||
number int
|
||||
rpMR byte
|
||||
@@ -248,7 +250,9 @@ func (s *Service) sendOutboundSMSPart(
|
||||
if s.smsTransactionTimeout <= 0 {
|
||||
return nil, smsDeliveryStateFailed, s.recordOutboundSMSFailure(messageID, part, errors.New("SMS transaction timeout is not configured"))
|
||||
}
|
||||
transactionCtx, cancel := context.WithTimeout(ctx, s.smsTransactionTimeout)
|
||||
transactionCtx, cancel := context.WithTimeoutCause(
|
||||
ctx, s.smsTransactionTimeout, errMOSMSFinalProbeTimeout,
|
||||
)
|
||||
defer cancel()
|
||||
pending, response, err := s.dispatchSubmitPartWithRetry(transactionCtx, messageID, part, sentAt)
|
||||
if err != nil {
|
||||
@@ -396,7 +400,13 @@ func (s *Service) dispatchSubmitPartWithRetry(
|
||||
}
|
||||
s.registerPendingSMS(part.callID, pending)
|
||||
s.recordOutboundSMSAudit(common.TraceID(ctx), part.callID, pending.To, len(request.Body()))
|
||||
result, dispatchErr := s.dispatchOutboundMESSAGE(ctx, "mo-submit", request, s.smsTransactionTimeout)
|
||||
callbacks := s.moSubmitTransactionCallbacks(messageID, part, pending)
|
||||
result, dispatchErr := s.dispatchOutboundMESSAGEWithCallbacks(
|
||||
outboundDispatchOptions{
|
||||
Context: ctx, Flow: "mo-submit", Request: request,
|
||||
Timeout: s.smsTransactionTimeout, Callbacks: callbacks,
|
||||
},
|
||||
)
|
||||
var response *sip.Response
|
||||
if result.SIPCode > 0 {
|
||||
response = sip.NewResponse(result.SIPCode, SIPStatusText(result.SIPCode))
|
||||
|
||||
@@ -320,6 +320,77 @@ func TestTCPSubmitProbeTimeoutWaitsForLateRPReport(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPSubmitProbeTimeoutAcceptsLateSIPFinal(t *testing.T) {
|
||||
service, _, store := newOutboundSMSTestService(t)
|
||||
service.smsTransactionTimeout = 20 * time.Millisecond
|
||||
service.smsReportTimeout = 250 * time.Millisecond
|
||||
requests := make(chan string, 1)
|
||||
service.transport.SetSendFn(func(request string) error {
|
||||
requests <- request
|
||||
return nil
|
||||
})
|
||||
|
||||
resultCh := make(chan SendOutcome, 1)
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
outcome, err := service.SendSMSWithResult(context.Background(), "+447700900123", "late final")
|
||||
resultCh <- outcome
|
||||
errCh <- err
|
||||
}()
|
||||
request := waitForOutboundSMSControl(t, requests)
|
||||
time.Sleep(2 * service.smsTransactionTimeout)
|
||||
assertTransactionCount(t, service.transport, 1)
|
||||
service.transport.DeliverResponse(registerResponseForRequest(request, 202, nil))
|
||||
|
||||
if err := <-errCh; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if outcome := <-resultCh; outcome.DeliveryState != smsDeliveryStatePending {
|
||||
t.Fatalf("outcome = %+v", outcome)
|
||||
}
|
||||
waitTransactionCount(t, service.transport, 0)
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if len(store.sipResults) != 1 || store.sipResults[0].code != 202 ||
|
||||
store.sipResults[0].state != smsDeliveryStatePending {
|
||||
t.Fatalf("SIP results = %+v", store.sipResults)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPSubmitProbeTimeoutRejectsLateSIPFinal(t *testing.T) {
|
||||
service, _, store := newOutboundSMSTestService(t)
|
||||
service.smsTransactionTimeout = 20 * time.Millisecond
|
||||
service.smsReportTimeout = 250 * time.Millisecond
|
||||
requests := make(chan string, 1)
|
||||
service.transport.SetSendFn(func(request string) error {
|
||||
requests <- request
|
||||
return nil
|
||||
})
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := service.SendSMSWithResult(context.Background(), "+447700900123", "late reject")
|
||||
errCh <- err
|
||||
}()
|
||||
request := waitForOutboundSMSControl(t, requests)
|
||||
time.Sleep(2 * service.smsTransactionTimeout)
|
||||
assertTransactionCount(t, service.transport, 1)
|
||||
response := registerResponseForRequest(request, 503, nil)
|
||||
response.Reason = "Service Unavailable"
|
||||
service.transport.DeliverResponse(response)
|
||||
|
||||
if err := <-errCh; err == nil || !strings.Contains(err.Error(), "503") {
|
||||
t.Fatalf("send error = %v", err)
|
||||
}
|
||||
waitTransactionCount(t, service.transport, 0)
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
if len(store.sipResults) != 1 || store.sipResults[0].code != 503 ||
|
||||
store.sipResults[0].state != smsDeliveryStateFailed || store.finalState != smsDeliveryStateFailed {
|
||||
t.Fatalf("SIP results = %+v, final state = %q", store.sipResults, store.finalState)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTCPSubmitReportWaitHonorsCallerDeadline(t *testing.T) {
|
||||
service, _, store := newOutboundSMSTestService(t)
|
||||
service.cfg.Transport = "tcp"
|
||||
|
||||
@@ -19,12 +19,21 @@ type outboundMessageReply struct {
|
||||
err error
|
||||
}
|
||||
|
||||
type outboundDispatchOptions struct {
|
||||
Context context.Context
|
||||
Flow string
|
||||
Request *sip.Request
|
||||
Timeout time.Duration
|
||||
Callbacks sipTransactionCallbacks
|
||||
}
|
||||
|
||||
type outboundMessageTask struct {
|
||||
ctx context.Context
|
||||
flow string
|
||||
req *sip.Request
|
||||
timeout int64
|
||||
done chan outboundMessageReply
|
||||
ctx context.Context
|
||||
flow string
|
||||
req *sip.Request
|
||||
timeout int64
|
||||
callbacks sipTransactionCallbacks
|
||||
done chan outboundMessageReply
|
||||
}
|
||||
|
||||
type outboundRequestReply struct {
|
||||
@@ -39,6 +48,7 @@ type outboundRequestTask struct {
|
||||
req *sip.Request
|
||||
modeCtx outboundModeContext
|
||||
timeout int64
|
||||
callbacks sipTransactionCallbacks
|
||||
dispatchSeq uint64
|
||||
enqueuedAt time.Time
|
||||
done chan outboundRequestReply
|
||||
|
||||
Reference in new issue
Block a user