Why doesn't the sdk-go have EnableAutoHeartbeat

I see EnableAutoHeartbeat option in cadence, so why doesn’t the sdk-go have EnableAutoHeartbeat?

Different people may want different things out of their heartbeater. Some may want details. Others may want to send something on last failure. Others may want to stop the heartbeater for some other reason besides activity complete. Some may use async activity completion and need to heartbeat via the client.

We provide the primitives to build any heartbeater you want. For example, here’s an untested one I just typed in a minute or two:

type myAutoHeartbeater struct {
	ticker *time.Ticker
	stopCh chan struct{}
}

func startAutoHeartbeat(ctx context.Context) *myAutoHeartbeater {
	heartbeatTimeout := activity.GetInfo(ctx).HeartbeatTimeout
	// If there is no heartbeat timeout, we don't auto heartbeat
	if heartbeatTimeout == 0 {
		return nil
	}
	// We'll heartbeat twice as often as timeout (this is throttled anyways)
	ticker := time.NewTicker(heartbeatTimeout / 2)
	stopCh := make(chan struct{})
	go func() {
		defer ticker.Stop()
		for {
			select {
			case <-ticker.C:
				activity.RecordHeartbeat(ctx)
			case <-stopCh:
				return
			}
		}
	}()
	return &myAutoHeartbeater{ticker, stopCh}
}

// Receiver can be nil
func (m *myAutoHeartbeater) stop() {
	if m != nil {
		close(m.stopCh)
	}
}

And you can use it like:

func MyActivity(ctx context.Context) error {
	h := startAutoHeartbeat(ctx)
	defer h.stop()

	// ... rest of your code
}

Auto heartbeating can be implemented yourself, and this way you have the freedom to customize it to suit your needs.

2 Likes

Update for those reaching this thread, we may make this a bit more official in the future. See [Feature Request] Auto heartbeating · Issue #229 · temporalio/features · GitHub.