mirror of
https://github.com/yibaiba/hideck.git
synced 2026-12-06 08:02:21 +00:00
[webdav-archive-cleanup-SMS-01] fix: keep SMS persistence failures message-scoped
Report durable-storage results synchronously to imscore so a failed message receives RP temporary failure without marking the whole runtime memory-full. Preserve explicit memory-full and RP-SMMA behavior.
This commit is contained in:
1 parent
8cb13cf83c
commit
88a339bfbb
7 files changed
+70
-58
No files matched your search
@@ -13,16 +13,13 @@ type poolVoWiFiRuntimeDispatcher struct {
|
||||
pool *Pool
|
||||
}
|
||||
|
||||
func applyVoWiFiSMSMemoryPressure(set func(bool), persistErr error, retained vowifiSMSRecordResult) {
|
||||
if set == nil {
|
||||
func reportVoWiFiSMSPersistence(result chan<- error, persistErr error) {
|
||||
if result == nil {
|
||||
return
|
||||
}
|
||||
if persistErr != nil {
|
||||
set(true)
|
||||
return
|
||||
}
|
||||
if retained.Stored || retained.Duplicate || retained.Suppressed {
|
||||
set(false)
|
||||
select {
|
||||
case result <- persistErr:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,9 +51,7 @@ func (d poolVoWiFiRuntimeDispatcher) Dispatch(ctx context.Context, e eventhost.E
|
||||
if err == nil && !res.Suppressed {
|
||||
res, err = recorder.recordReceived(v, receivedOn)
|
||||
}
|
||||
applyVoWiFiSMSMemoryPressure(func(full bool) {
|
||||
d.pool.SetVoWiFiSMSMemoryFull(v.DevID, full)
|
||||
}, err, res)
|
||||
reportVoWiFiSMSPersistence(v.PersistenceResult, err)
|
||||
if err != nil {
|
||||
logger.Warn("VoWiFi 上层入库入站短信失败", "device", v.DevID, "sender", v.Sender, "err", err)
|
||||
}
|
||||
|
||||
@@ -10,45 +10,25 @@ import (
|
||||
"github.com/yibaiba/hideck/internal/db"
|
||||
)
|
||||
|
||||
func TestApplyVoWiFiSMSMemoryPressure(t *testing.T) {
|
||||
func TestReportVoWiFiSMSPersistence(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
retained vowifiSMSRecordResult
|
||||
want *bool
|
||||
name string
|
||||
err error
|
||||
}{
|
||||
{name: "persist error", err: errors.New("db down"), want: boolPtr(true)},
|
||||
{name: "stored", retained: vowifiSMSRecordResult{Stored: true}, want: boolPtr(false)},
|
||||
{name: "duplicate", retained: vowifiSMSRecordResult{Duplicate: true}, want: boolPtr(false)},
|
||||
{name: "suppressed", retained: vowifiSMSRecordResult{Suppressed: true}, want: boolPtr(false)},
|
||||
{name: "empty identity"},
|
||||
{name: "success"},
|
||||
{name: "persist error", err: errors.New("db down")},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var (
|
||||
set *bool
|
||||
calls int
|
||||
)
|
||||
applyVoWiFiSMSMemoryPressure(func(full bool) {
|
||||
calls++
|
||||
value := full
|
||||
set = &value
|
||||
}, test.err, test.retained)
|
||||
if test.want == nil {
|
||||
if calls != 0 {
|
||||
t.Fatalf("unexpected set calls=%d full=%v", calls, set)
|
||||
}
|
||||
return
|
||||
}
|
||||
if calls != 1 || set == nil || *set != *test.want {
|
||||
t.Fatalf("full=%v calls=%d, want %v", set, calls, *test.want)
|
||||
result := make(chan error, 1)
|
||||
reportVoWiFiSMSPersistence(result, test.err)
|
||||
if got := <-result; !errors.Is(got, test.err) {
|
||||
t.Fatalf("result=%v, want %v", got, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool { return &v }
|
||||
|
||||
func TestVoWiFiSMSHistoryRecorderPersistsSentSMS(t *testing.T) {
|
||||
initDevicePhoneNumberTestDB(t)
|
||||
if err := db.UpdateSIMCardVoWiFiPhoneNumberByIMSI("imsi-vowifi-1", "+8613800000000"); err != nil {
|
||||
|
||||
@@ -29,6 +29,11 @@ type EventSMSReceived struct {
|
||||
Incomplete bool
|
||||
ICCID string
|
||||
IMSI string
|
||||
// PersistenceResult lets the synchronous host dispatcher report whether
|
||||
// the complete message reached durable storage. A nil result means the
|
||||
// message is safe to acknowledge; a non-nil result is handled as a
|
||||
// per-message temporary failure and must not poison later deliveries.
|
||||
PersistenceResult chan<- error
|
||||
}
|
||||
|
||||
// EventSMSSent is published when an SMS is sent.
|
||||
|
||||
@@ -179,30 +179,46 @@ func TestNotifySMSMemoryAvailableAbortOnStop(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInboundPersistFailureSendsRPError22(t *testing.T) {
|
||||
func TestInboundPersistFailureSendsTemporaryErrorWithoutBlockingNextSMS(t *testing.T) {
|
||||
service, subscriber, outbound := newInboundSMSTestService(t)
|
||||
persistCalls := 0
|
||||
subscriber.onEvent = func(event events.Event) {
|
||||
if _, ok := event.(*events.EventSMSReceived); ok {
|
||||
service.SetSMSMemoryFull(true)
|
||||
if received, ok := event.(*events.EventSMSReceived); ok {
|
||||
persistCalls++
|
||||
if persistCalls == 1 {
|
||||
received.PersistenceResult <- errors.New("database temporarily unavailable")
|
||||
return
|
||||
}
|
||||
received.PersistenceResult <- nil
|
||||
}
|
||||
}
|
||||
raw := inboundSMSRequest(t, imsSMSContentType, inboundRPData(t, 0x61, "+447700900123", "held"))
|
||||
if err := service.dispatchInboundSIP(raw, func(string) error { return nil }); err != nil {
|
||||
first := inboundSMSRequest(t, imsSMSContentType, inboundRPData(t, 0x61, "+447700900123", "retry me"))
|
||||
if err := service.dispatchInboundSIP(first, func(string) error { return nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-subscriber.events:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("inbound SMS event was not published")
|
||||
}
|
||||
<-subscriber.events
|
||||
request := waitForOutboundSMSControl(t, outbound)
|
||||
body, err := rawSIPBody(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(body, smscodec.BuildRPError(0x61, smscodec.RPCauseMemoryCapacityExceeded)) {
|
||||
if !bytes.Equal(body, smscodec.BuildRPError(0x61, smscodec.RPCauseTemporaryFailure)) {
|
||||
t.Fatalf("persist-failure RP body = %x", body)
|
||||
}
|
||||
|
||||
second := inboundSMSRequest(t, imsSMSContentType, inboundRPData(t, 0x62, "+447700900123", "store me"))
|
||||
if err := service.dispatchInboundSIP(second, func(string) error { return nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
<-subscriber.events
|
||||
request = waitForOutboundSMSControl(t, outbound)
|
||||
body, err = rawSIPBody(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(body, smscodec.BuildRPAck(0x62)) {
|
||||
t.Fatalf("next-message RP body = %x", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutboundSIPURIUsesMultipartDummyMSISDN(t *testing.T) {
|
||||
|
||||
@@ -285,8 +285,9 @@ func (s *Service) finalizeInboundSMSData(
|
||||
sendRPError: true, err: assembleErr, peerConn: message.peerConn,
|
||||
})
|
||||
}
|
||||
var persistenceErr error
|
||||
if shouldDispatch {
|
||||
s.publishInboundSMSWithFragment(message, message.fragmentSessionID, false)
|
||||
persistenceErr, _ = s.publishInboundSMSWithFragment(message, message.fragmentSessionID, false)
|
||||
}
|
||||
s.reportMTQueueBlocked(mtSMSIdentity(message))
|
||||
fingerprint := buildMTSMSFingerprint(message, raw)
|
||||
@@ -294,9 +295,6 @@ func (s *Service) finalizeInboundSMSData(
|
||||
smsReceived: true,
|
||||
response: response,
|
||||
afterReply: func() {
|
||||
if fragmentKey != "" {
|
||||
s.markFragmentAcked(fragmentKey, message.partNo)
|
||||
}
|
||||
if s.smsMemoryIsFull() {
|
||||
s.rememberSMSMemoryDenied(raw)
|
||||
s.sendRPReportWithRetry(s.rpReportForInbound(
|
||||
@@ -304,6 +302,15 @@ func (s *Service) finalizeInboundSMSData(
|
||||
))
|
||||
return
|
||||
}
|
||||
if persistenceErr != nil {
|
||||
s.sendRPReportWithRetry(s.rpReportForInbound(
|
||||
raw, message, smscodec.BuildRPError(message.rpMR, smscodec.RPCauseTemporaryFailure),
|
||||
))
|
||||
return
|
||||
}
|
||||
if fragmentKey != "" {
|
||||
s.markFragmentAcked(fragmentKey, message.partNo)
|
||||
}
|
||||
report := s.rpReportForInbound(raw, message, smscodec.BuildRPAck(message.rpMR))
|
||||
report.Fingerprint = fingerprint
|
||||
s.sendRPReportWithRetry(report)
|
||||
@@ -422,23 +429,31 @@ func (s *Service) handleInboundSMSProtocolFailure(failure inboundSMSProtocolFail
|
||||
}
|
||||
|
||||
func (s *Service) publishInboundSMS(message inboundSMS) {
|
||||
s.publishInboundSMSWithFragment(message, "", false)
|
||||
_, _ = s.publishInboundSMSWithFragment(message, "", false)
|
||||
}
|
||||
|
||||
func (s *Service) publishInboundSMSWithFragment(
|
||||
message inboundSMS,
|
||||
fragmentSessionKey string,
|
||||
incomplete bool,
|
||||
) {
|
||||
) (error, bool) {
|
||||
if message.timestamp.IsZero() {
|
||||
message.timestamp = time.Now()
|
||||
}
|
||||
result := make(chan error, 1)
|
||||
s.bus.Publish(&events.EventSMSReceived{
|
||||
DevID: s.cfg.DeviceID, Sender: message.sender, TargetURI: message.targetURI,
|
||||
Content: message.content, Time: message.timestamp,
|
||||
FragmentSessionKey: strings.TrimSpace(fragmentSessionKey), Incomplete: incomplete,
|
||||
ICCID: strings.TrimSpace(s.cfg.ICCID), IMSI: strings.TrimSpace(s.cfg.IMSI),
|
||||
PersistenceResult: result,
|
||||
})
|
||||
select {
|
||||
case err := <-result:
|
||||
return err, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) rpReportForInbound(raw string, message inboundSMS, rpdu []byte) rpReportRequest {
|
||||
|
||||
+1
-1
@@ -70,7 +70,7 @@ func publicSMSReceived(value events.EventSMSReceived) eventhost.SMSReceived {
|
||||
DevID: value.DevID, Sender: value.Sender, TargetURI: value.TargetURI,
|
||||
Content: value.Content, Time: value.Time,
|
||||
FragmentSessionKey: value.FragmentSessionKey, Incomplete: value.Incomplete,
|
||||
ICCID: value.ICCID, IMSI: value.IMSI,
|
||||
ICCID: value.ICCID, IMSI: value.IMSI, PersistenceResult: value.PersistenceResult,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ type SMSReceived struct {
|
||||
Incomplete bool
|
||||
ICCID string
|
||||
IMSI string
|
||||
PersistenceResult chan<- error
|
||||
}
|
||||
|
||||
// SMSSent is published when an SMS is sent.
|
||||
|
||||
Reference in new issue
Block a user