feat: add tryEnqueue function. (#406)

* feat: add tryEnqueue function.

tryEnqueue tries to enqueue a job to the given job channel. Returns true if
the operation was successful, and false if enqueuing would not have been
possible without blocking. Job is not enqueued in the latter case.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* remove inital worker

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2019-04-25 23:10:12 +08:00 committed by GitHub
parent 5b526a73bd
commit 27ec42543b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 3 deletions

View File

@ -25,8 +25,6 @@ func TestCorrectConf(t *testing.T) {
func TestSenMultipleNotifications(t *testing.T) {
PushConf, _ = config.LoadConf("")
InitWorkers(int64(2), 2)
PushConf.Ios.Enabled = true
PushConf.Ios.KeyPath = "../certificate/certificate-valid.pem"
err := InitAPNSClient()

View File

@ -57,7 +57,9 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) {
notification.log = &log
notification.AddWaitCount()
}
QueueNotification <- *notification
if !tryEnqueue(*notification, QueueNotification) {
LogError.Error("max capacity reached")
}
count += len(notification.Tokens)
// Count topic message
if notification.To != "" {
@ -73,3 +75,15 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) {
return count, log
}
// tryEnqueue tries to enqueue a job to the given job channel. Returns true if
// the operation was successful, and false if enqueuing would not have been
// possible without blocking. Job is not enqueued in the latter case.
func tryEnqueue(job PushNotification, jobChan chan<- PushNotification) bool {
select {
case jobChan <- job:
return true
default:
return false
}
}

17
gorush/worker_test.go Normal file
View File

@ -0,0 +1,17 @@
package gorush
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTryEnqueue(t *testing.T) {
chn := make(chan PushNotification, 2)
assert.True(t, tryEnqueue(PushNotification{}, chn))
assert.Equal(t, 1, len(chn))
assert.True(t, tryEnqueue(PushNotification{}, chn))
assert.Equal(t, 2, len(chn))
assert.False(t, tryEnqueue(PushNotification{}, chn))
assert.Equal(t, 2, len(chn))
}