Skip to content

DeliveryQueue is a queue with embedded rate limiter that guarantees that delivery function will not be triggered more than specified number of times per second.

License

Notifications You must be signed in to change notification settings

bazuker/deliveryQueue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DeliveryQueue

Go Report Card

DeliveryQueue is a queue with embedded rate limiter that guarantees that delivery function will not be triggered more than specified number of times per second.

go get -u github.com/bazuker/deliveryQueue

Example

const (
	MaxMessagesPerSecond = 30
	MessagesToDeliver = 120
)

func main() {
	wg := &sync.WaitGroup{}

	dq, _ := queue.NewDeliveryQueue(MaxMessagesPerSecond, func(item interface{}) {
		log.Println(item)
		wg.Done()
	})

	go dq.Poll()

	for i := 1; i <= MessagesToDeliver; i++ {
		dq.Add(i)
	}

	wg.Add(MessagesToDeliver)

	log.Println("All messages are submitted")

	start := time.Now()
	wg.Wait()
	log.Printf("Delivered %d messages in %f seconds", MessagesToDeliver, time.Now().Sub(start).Seconds())
}
2019/07/13 17:25:57 All messages are submitted
2019/07/13 17:25:57 1
2019/07/13 17:25:57 2
...
2019/07/13 17:26:01 119
2019/07/13 17:26:01 120
2019/07/13 17:26:01 Delivered 120 messages in 4.148081 seconds

About

DeliveryQueue is a queue with embedded rate limiter that guarantees that delivery function will not be triggered more than specified number of times per second.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages