Skip to content

Commit 0c51a77

Browse files
authored
fix high cpu usage with new ticker in each for loop (#64)
1 parent 487e2b3 commit 0c51a77

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

config/pulsar_beam.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
PORT: 8085
33
CLUSTER: localhost
4-
PbDbType: mongo
4+
PbDbType: inmemory
55
PulsarPublicKey: ./unit-test/example_public_key.pub
66
PulsarPrivateKey: ./unit-test/example_private_key
77
PbDbInterval: 10s

src/broker/webhook.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ func Init() {
7171

7272
go func() {
7373
run()
74+
ticker := time.NewTicker(duration)
7475
for {
7576
select {
76-
case <-time.Tick(duration):
77+
case <-ticker.C:
7778
run()
7879
}
7980
}
@@ -195,11 +196,13 @@ func ConsumeLoop(url, token, topic, subscriptionKey string, whCfg model.WebhookC
195196
if err != nil {
196197
log.Infof("error from consumer loop receive: %v\n", err)
197198
retry++
199+
ticker := time.NewTicker(time.Duration(2*retry) * time.Second)
200+
defer ticker.Stop()
198201
select {
199202
case <-terminate:
200203
log.Infof("subscription %s received signal to exit consumer loop", subscriptionKey)
201204
return nil
202-
case <-time.Tick(time.Duration(2*retry) * time.Second):
205+
case <-ticker.C:
203206
//reconnect after error
204207
c, err = pulsardriver.GetPulsarConsumer(url, token, topic, whCfg.Subscription, whCfg.InitialPosition, whCfg.SubscriptionType, subscriptionKey)
205208
if err != nil {

src/util/cert-loader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ func watchFile(filePath string, updated chan *updatedChann) error {
4242
return err
4343
}
4444

45+
ticker := time.NewTicker(1 * time.Second)
4546
for {
4647
select {
47-
case <-time.Tick(1 * time.Second):
48+
case <-ticker.C:
4849
stat, err := os.Stat(filePath)
4950
if err == nil {
5051
if stat.Size() != initialStat.Size() || stat.ModTime() != initialStat.ModTime() {

src/util/ttlcache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ func (c *Cache) Get(key string) (interface{}, bool) {
5757

5858
// eventLoop name is a disguise. I should convert the lock/unlock to an event loop
5959
func (c *Cache) eventLoop() {
60+
ticker := time.NewTicker(c.opt.CleanInterval)
6061
for {
6162
select {
62-
case <-time.Tick(c.opt.CleanInterval):
63+
case <-ticker.C:
6364
// RLock is faster than Lock, performant improve to get a slice of keys first
6465
c.mutex.RLock()
6566
keys := make([]string, 0, len(c.items))

0 commit comments

Comments
 (0)