Skip to content

Commit

Permalink
Merge pull request #63 from kayac/fcm-v1-bulk
Browse files Browse the repository at this point in the history
Allow multiple payload in fcm/v1 request body.
  • Loading branch information
FUJIWARA Shunichiro authored Dec 20, 2019
2 parents 06c9753 + 14f3fa4 commit 4db2141
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Response example:
{"result": "ok"}
```

FCM v1 endpoint allows multiple payloads in a single request body. You can build request body simply concat multiple JSON payloads. Gunfish sends for each that payloads to FCM server. Limitation: Max count of payloads in a request body is 500.

### GET /stats/app

```json
Expand Down
3 changes: 3 additions & 0 deletions fcmv1/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ import (
type Payload struct {
Message messaging.Message `json:"message"`
}

// MaxBulkRequests represens max count of request payloads in a request body.
const MaxBulkRequests = 500
29 changes: 19 additions & 10 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gunfish
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -307,24 +308,32 @@ func (prov *Provider) PushFCMHandler(v1 bool) http.HandlerFunc {

func newFCMRequests(src io.Reader, v1 bool) ([]Request, error) {
dec := json.NewDecoder(src)
reqs := []Request{
Request{
Tries: 0,
},
}
reqs := []Request{}
if v1 {
var payload fcmv1.Payload
if err := dec.Decode(&payload); err != nil {
return nil, err
count := 0
PAYLOADS:
for {
var payload fcmv1.Payload
if err := dec.Decode(&payload); err != nil {
if err == io.EOF {
break PAYLOADS
} else {
return nil, err
}
}
count++
if count >= fcmv1.MaxBulkRequests {
return nil, errors.New("Too many requests")
}
reqs = append(reqs, Request{Notification: payload, Tries: 0})
}
reqs[0].Notification = payload
return reqs, nil
} else {
var payload fcm.Payload
if err := dec.Decode(&payload); err != nil {
return nil, err
}
reqs[0].Notification = payload
reqs = append(reqs, Request{Notification: payload, Tries: 0})
return reqs, nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestEnqueuRequestToSupervisor(t *testing.T) {
for range []int{0, 1, 2, 3, 4, 5, 6} {
sup.EnqueueClientRequest(&reqs)
}
time.Sleep(time.Millisecond * 500)
time.Sleep(time.Millisecond * 1000)
wg.Wait()
if g, w := str.Get("success"), 70; g != w {
t.Errorf("not match success count: got %d want %d", g, w)
Expand Down

0 comments on commit 4db2141

Please sign in to comment.