Skip to content

Commit b4c1161

Browse files
committed
Fix message scheduling
1 parent ab04eb8 commit b4c1161

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

api/cmd/loadtest/main.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
package main
22

33
import (
4-
"bytes"
5-
"encoding/json"
4+
"context"
65
"fmt"
7-
"io/ioutil"
86
"log"
9-
"net/http"
107

8+
"github.com/carlmjohnson/requests"
119
"github.com/palantir/stacktrace"
1210
)
1311

1412
func main() {
15-
client := http.Client{}
16-
for i := 0; i < 100; i++ {
17-
payload, err := json.Marshal(map[string]string{
18-
"content": fmt.Sprintf("testing http api sample: [%d]", i),
19-
"from": "+37259139660",
20-
"to": "+37253517181",
21-
})
13+
for i := 0; i < 50; i++ {
14+
var responsePayload string
15+
err := requests.
16+
URL("/v1/messages/send").
17+
Host("localhost:8000").
18+
Scheme("http").
19+
Header("x-api-key", "Uv38ByGCZU8WP18PmmIdcpVmx00QA3xNe7sEB9HixkmBhVrYaB0NhtHpHgAWeTnL").
20+
BodyJSON(&map[string]string{
21+
"content": fmt.Sprintf("testing http api sample: [%d]", i),
22+
"from": "+37259139660",
23+
"to": "+37253517181",
24+
}).
25+
ToString(&responsePayload).
26+
Fetch(context.Background())
2227
if err != nil {
2328
log.Fatal(stacktrace.Propagate(err, "cannot create json payload"))
2429
}
25-
response, err := client.Post("https://api.httpsms.com/v1/messages/send", "application/json", bytes.NewBuffer(payload))
26-
if err != nil {
27-
log.Fatal(stacktrace.Propagate(err, "cannot perform http request"))
28-
}
29-
30-
if response.StatusCode != http.StatusOK {
31-
log.Fatal(stacktrace.NewError(fmt.Sprintf("status code [%d] is different from expected [%d]", response.StatusCode, http.StatusOK)))
32-
}
33-
34-
responsePayload, err := ioutil.ReadAll(response.Body)
35-
if err != nil {
36-
log.Fatal(stacktrace.NewError(fmt.Sprintf("cannot read response")))
37-
}
3830

39-
log.Println(string(responsePayload))
31+
log.Println(responsePayload)
4032
}
4133
}

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
firebase.google.com/go v3.13.0+incompatible
88
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.8.3
99
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
10+
github.com/carlmjohnson/requests v0.22.3
1011
github.com/cheggaaa/pb/v3 v3.0.8
1112
github.com/cloudevents/sdk-go/v2 v2.10.0
1213
github.com/cockroachdb/cockroach-go/v2 v2.2.11

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
9191
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
9292
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
9393
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
94+
github.com/carlmjohnson/requests v0.22.3 h1:ip16AKXNYuArdw9L5/1mL+mNorlZO5XhkLg617yOumc=
95+
github.com/carlmjohnson/requests v0.22.3/go.mod h1:iTsaX9TdFg2+L4WtZO/HFyDMPEfBnogV3i4A4gjDnvs=
9496
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
9597
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
9698
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=

api/pkg/repositories/gorm_phone_notification_repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (repository gormPhoneNotificationRepository) Schedule(ctx context.Context,
6565
err := crdbgorm.ExecuteTx(ctx, repository.db, nil, func(tx *gorm.DB) error {
6666
var messagesCount int64
6767
err := tx.WithContext(ctx).
68+
Model(&entities.PhoneNotification{}).
6869
Where("phone_id = ?", notification.PhoneID).
6970
Where("status = ?", entities.PhoneNotificationStatusPending).
7071
Count(&messagesCount).

0 commit comments

Comments
 (0)