shezmu/example/daemons/price_consumer.go

27 lines
599 B
Go
Raw Normal View History

2015-10-16 02:35:03 +03:00
package daemons
import (
2015-10-24 19:28:17 +03:00
"time"
2015-10-16 02:35:03 +03:00
"github.com/localhots/satan"
)
// PriceConsumer consumes price update messages and prints them to the console.
type PriceConsumer struct {
2015-10-24 19:28:17 +03:00
satan.BaseDaemon
2015-10-16 02:35:03 +03:00
}
// PriceUpdate describes a price update message.
type PriceUpdate struct {
Product string `json:"product"`
Amount float64 `json:"amount"`
}
// Startup creates a new subscription for ProductPriceUpdates topic.
func (p *PriceConsumer) Startup() {
2015-10-24 19:28:17 +03:00
p.Subscribe("ProductPriceUpdates", func(u PriceUpdate) {
p.Logf("Price for %q is now $%.2f", u.Product, u.Amount)
2015-10-16 02:35:03 +03:00
})
2015-10-24 03:07:47 +03:00
p.LimitRate(5, time.Second)
2015-10-16 02:35:03 +03:00
}