Skip to content

Commit 21bce22

Browse files
committed
Use session_modified_on instead of session_wait_started_on to gate timeout updates
1 parent 41ac1ad commit 21bce22

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

Diff for: backends/rapidpro/backend.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ func (b *backend) OnSendComplete(ctx context.Context, msg courier.MsgOut, status
515515

516516
// if message was successfully sent, and we have a session timeout, update it
517517
wasSuccess := status.Status() == courier.MsgStatusWired || status.Status() == courier.MsgStatusSent || status.Status() == courier.MsgStatusDelivered || status.Status() == courier.MsgStatusRead
518-
if wasSuccess && dbMsg.SessionWaitStartedOn_ != nil {
519-
if err := updateSessionTimeout(ctx, b, dbMsg.SessionID_, *dbMsg.SessionWaitStartedOn_, dbMsg.SessionTimeout_); err != nil {
518+
if wasSuccess && dbMsg.SessionTimeout_ != 0 {
519+
if err := updateSessionTimeout(ctx, b, dbMsg.SessionID_, *dbMsg.SessionModifiedOn_, dbMsg.SessionTimeout_); err != nil {
520520
slog.Error("unable to update session timeout", "error", err, "session_id", dbMsg.SessionID_)
521521
}
522522
}

Diff for: backends/rapidpro/backend_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ func (ts *BackendTestSuite) TestSessionTimeout() {
13381338
ctx := context.Background()
13391339

13401340
// parse from an iso date
1341-
t, err := time.Parse("2006-01-02 15:04:05.000000-07", "2018-12-04 11:52:20.958955-08")
1341+
t, err := time.Parse("2006-01-02 15:04:05.000000-07", "2018-12-04 11:52:20.900234-08")
13421342
ts.NoError(err)
13431343

13441344
err = updateSessionTimeout(ctx, ts.b, SessionID(1), t, 300)

Diff for: backends/rapidpro/msg.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ type Msg struct {
8787
ContactLastSeenOn_ *time.Time `json:"contact_last_seen_on"`
8888

8989
// extra fields used to allow courier to update a session's timeout to *after* the message has been sent
90-
SessionID_ SessionID `json:"session_id"`
91-
SessionTimeout_ int `json:"session_timeout"`
92-
SessionWaitStartedOn_ *time.Time `json:"session_wait_started_on"`
93-
SessionStatus_ string `json:"session_status"`
90+
SessionID_ SessionID `json:"session_id"`
91+
SessionTimeout_ int `json:"session_timeout"`
92+
SessionStatus_ string `json:"session_status"`
93+
SessionModifiedOn_ *time.Time `json:"session_modified_on"`
9494

9595
ContactName_ string `json:"contact_name"`
9696
URNAuthTokens_ map[string]string `json:"auth_tokens"`

Diff for: backends/rapidpro/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ DROP TABLE IF EXISTS flows_flowsession CASCADE;
132132
CREATE TABLE flows_flowsession (
133133
id serial primary key,
134134
status character varying(1) NOT NULL,
135+
modified_on timestamp with time zone NOT NULL,
135136
timeout_on timestamp with time zone NULL,
136137
wait_started_on timestamp with time zone
137138
);

Diff for: backends/rapidpro/sessions.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ const updateSessionTimeoutSQL = `
1515
timeout_on = NOW() + $3 * interval '1 second'
1616
WHERE
1717
id = $1 AND
18-
extract(epoch from wait_started_on) = extract(epoch from $2::timestamp with time zone) AND
18+
extract(epoch from modified_on) = extract(epoch from $2::timestamptz) AND
1919
status = 'W'
2020
`
2121

2222
// updateSessionTimeout updates the timeout_on value on a db session if our session's wait on hasn't changed
23-
func updateSessionTimeout(ctx context.Context, b *backend, sessionID SessionID, waitStartedOn time.Time, timeoutSeconds int) error {
24-
_, err := b.db.ExecContext(ctx, updateSessionTimeoutSQL, sessionID, waitStartedOn.In(time.UTC), timeoutSeconds)
23+
func updateSessionTimeout(ctx context.Context, b *backend, sessionID SessionID, sessionModifiedOn time.Time, timeoutSeconds int) error {
24+
_, err := b.db.ExecContext(ctx, updateSessionTimeoutSQL, sessionID, sessionModifiedOn.In(time.UTC), timeoutSeconds)
2525
return err
2626
}

Diff for: backends/rapidpro/testdata.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ INSERT INTO msgs_media("id", "uuid", "org_id", "content_type", "url", "path", "s
6868

6969
/** Simple session */
7070
DELETE from flows_flowsession;
71-
INSERT INTO flows_flowsession("id", "status", "wait_started_on")
72-
VALUES(1, 'W', '2018-12-04 11:52:20.958955-08'),
73-
(2, 'C', '2018-12-04 11:52:20.958955-08');
71+
INSERT INTO flows_flowsession("id", "status", "modified_on", "wait_started_on")
72+
VALUES(1, 'W', '2018-12-04 11:52:20.900234-08', '2018-12-04 11:52:20.900123-08'),
73+
(2, 'C', '2018-12-04 11:52:20.900456-08', '2018-12-04 11:52:20.900345-08');

0 commit comments

Comments
 (0)