Skip to content

Commit

Permalink
Fix push expiration handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Aug 14, 2021
1 parent 5fe88c6 commit f46dad5
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions push/buford/buford.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type NewClient func(*tls.Certificate) (*http.Client, error)
// bufordFactory instantiates new buford Services to satisfy the PushProviderFactory interface.
type bufordFactory struct {
workers uint
expiration time.Time
expiration time.Duration
newClientCallback NewClient
}

Expand All @@ -34,7 +34,7 @@ func WithWorkers(workers uint) Option {
}

// WithExpiration sets the APNs expiration time for the push notifications.
func WithExpiration(expiration time.Time) Option {
func WithExpiration(expiration time.Duration) Option {
return func(f *bufordFactory) {
f.expiration = expiration
}
Expand Down Expand Up @@ -72,26 +72,33 @@ func (f *bufordFactory) NewPushProvider(cert *tls.Certificate) (push.PushProvide
return nil, err
}
prov := &bufordPushProvider{
service: bufordpush.NewService(client, bufordpush.Production),
workers: f.workers,
}
if !f.expiration.IsZero() {
prov.headers = &bufordpush.Headers{Expiration: f.expiration}
service: bufordpush.NewService(client, bufordpush.Production),
expiration: f.expiration,
workers: f.workers,
}
return prov, err
}

// bufordPushProvider wraps a buford Service to satisfy the PushProvider interface.
type bufordPushProvider struct {
service *bufordpush.Service
headers *bufordpush.Headers
workers uint
service *bufordpush.Service
expiration time.Duration
workers uint
}

func assemblePushData(magic string, exp time.Duration) (payload []byte, hdr *bufordpush.Headers) {
payload = []byte(`{"mdm":"` + magic + `"}`)
if exp > 0 {
expiration := time.Now().Add(exp)
hdr = &bufordpush.Headers{Expiration: expiration}
}
return
}

func (c *bufordPushProvider) pushSingle(pushInfo *mdm.Push) *push.Response {
resp := new(push.Response)
payload := []byte(`{"mdm":"` + pushInfo.PushMagic + `"}`)
resp.Id, resp.Err = c.service.Push(pushInfo.Token.String(), c.headers, payload)
payload, headers := assemblePushData(pushInfo.PushMagic, c.expiration)
resp.Id, resp.Err = c.service.Push(pushInfo.Token.String(), headers, payload)
return resp
}

Expand All @@ -103,8 +110,8 @@ func (c *bufordPushProvider) pushMulti(pushInfos []*mdm.Push) map[string]*push.R
queue := bufordpush.NewQueue(c.service, workers)
defer queue.Close()
for _, push := range pushInfos {
payload := []byte(`{"mdm":"` + push.PushMagic + `"}`)
go queue.Push(push.Token.String(), c.headers, payload)
payload, headers := assemblePushData(push.PushMagic, c.expiration)
go queue.Push(push.Token.String(), headers, payload)
}
responses := make(map[string]*push.Response)
for range pushInfos {
Expand Down

0 comments on commit f46dad5

Please sign in to comment.