timehop / apns

A Go package to interface with the Apple Push Notification Service
https://godoc.org/github.com/timehop/apns
MIT License
185 stars 47 forks source link

Hanging on send() #46

Open ynnadkrap opened 9 years ago

ynnadkrap commented 9 years ago

c.Send(m) isn't returning and I'm not sure why. I created a client without error and am not receiving any errors on sent PNs.

    c, err := apns.NewClientWithFiles(apns.SandboxGateway, CERT_PEM, KEY_PEM)
    if err != nil {
        log.Println("Client error: ", err.Error())
    }

    go func() {
        for f := range c.FailedNotifs {
            log.Println("Notif", f.Notif.ID, " failed with ", f.Err.Error())
        }
    }()

    p := apns.NewPayload()
    p.APS.Alert.Body = content
    badge := 1
    p.APS.Badge = &badge
    p.APS.Sound = "bingbong.aiff"

    m := apns.NewNotification()
    m.Payload = p
    m.DeviceToken = author.PnId
    m.Priority = apns.PriorityImmediate
    m.ID = content

    log.Println("---")
    c.Send(m)
    log.Println("...")
nathany commented 9 years ago

That's odd. All Send does is add the message to an internal channel. When you use NewClientWithFiles it also launches a goroutine with a RunLoop waiting to pull messages off that channel. I'm not sure what's happening here yet.

taylortrimble commented 9 years ago

We're doing a blocking send on the notifs channel, so if the notifs channel becomes full, we'll block until a notification is dequeued.

The alternative is to use the select syntax of posting to the channel, and reporting dropped messages. Something like this.

select {
case c.notifs <- n:
    return nil;
default:
    // Send would block, drop the notification.
    return ErrNotificationDropped
}

or this:

select {
case c.notifs <- n:
default:
    // Send would block, drop the notification.
    if c.DropHandler != nil {
        c.DropHandler(n)
    }
}
taylortrimble commented 9 years ago

Only read this comment if you're in the mood for crazy talk that isn't roadmapped and may never materialize:

Idea: the "drop" case could be used as a signal to open additional connections to the APNS gateway as needed for dealing with heavy load.

nathany commented 9 years ago

Yes, I'm just not sure why notifs would be blocking if there is only one notification being sent as in the example. @ynnadkrap Is there more to that example than you are showing?