Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
If `killAfterIdleMs` is 0, time.After() function call makes memory leak.
Refer to https://golang.org/pkg/time/#After. According to the document,
the underlying Timer object is not recovered by the garbage collector if
the timer is not fired. And It seems that if the duration is 0, it will
not be released. Therefore, use one Timer object for idle time checking
explicitly instead of the time.After() function.
  • Loading branch information
Sunkwan-Kwon authored and wolfstudy committed Dec 2, 2019
1 parent 86a6be7 commit f310ab0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pulsar-function-go/pf/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ func (gi *goInstance) startFunction(function function) error {
return err
}

idleDuration := getIdleTimeout(time.Millisecond * gi.context.instanceConf.killAfterIdleMs)
idleTimer := time.NewTimer(idleDuration)
defer idleTimer.Stop()

CLOSE:
for {
idleTimer.Reset(idleDuration)
select {
case cm := <-channel:
msgInput := cm.Message
Expand All @@ -94,7 +99,7 @@ CLOSE:

gi.processResult(msgInput, output)

case <-time.After(getIdleTimeout(time.Millisecond * gi.context.instanceConf.killAfterIdleMs)):
case <-idleTimer.C:
close(channel)
break CLOSE
}
Expand All @@ -107,6 +112,7 @@ CLOSE:

func (gi *goInstance) setupClient() error {
client, err := pulsar.NewClient(pulsar.ClientOptions{

URL: gi.context.instanceConf.pulsarServiceURL,
})
if err != nil {
Expand Down Expand Up @@ -271,7 +277,7 @@ func getIdleTimeout(timeoutMilliSecond time.Duration) time.Duration {
func (gi *goInstance) setupLogHandler() error {
if gi.context.instanceConf.funcDetails.GetLogTopic() != "" {
gi.context.logAppender = NewLogAppender(
gi.client, //pulsar client
gi.client, //pulsar client
gi.context.instanceConf.funcDetails.GetLogTopic(), //log topic
getDefaultSubscriptionName(gi.context.instanceConf.funcDetails.Tenant, //fqn
gi.context.instanceConf.funcDetails.Namespace,
Expand Down

0 comments on commit f310ab0

Please sign in to comment.