You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly, I would like to thank @autom8ter for bringing out the Machine. This package is super helpful in building highly concurrent Go applications.
In the below code snippet, how can I close subscribers when all the messages are consumed? Currently, the subscriber goroutine is hanging indefinitely.
numRange:= []int{1,2,3,4,5,6,7,8,9}
m:=machine.New(context.Background(),
// functions are added to a FIFO channel that will block when active routines == max routines.machine.WithMaxRoutines(d.MaxConcurrentSqlExecutions),
// every function executed by machine.Go will recover from panicsmachine.WithMiddlewares(machine.PanicRecover()),
)
deferm.Close()
channelName:="dedupe"// start a goroutine that subscribes to all messages sent to the target channelm.Go(func(routine machine.Routine) {
for {
select {
case<-routine.Context().Done():
returndefault:
err:=routine.Subscribe(channelName, func(objinterface{}) {
log.Infof("%v | subscription msg received! channel = %v msg = %v stats = %s\n",
routine.PID(), channelName, obj, m.Stats().String())
})
iferr!=nil {
log.Error("failed to start goroutines to consume jobs ", err)
routine.Cancel()
return
}
}
}
},
machine.GoWithTags("subscribe"))
// start another goroutine that publishes to the target channel every secondm.Go(func(routine machine.Routine) {
deferroutine.Cancel()
log.Infof("%v | streaming msg to channel = %v stats = %s\n", routine.PID(), channelName, routine.Machine().Stats().String())
// publish message to channelfor_, interval:=rangenumRange {
err:=routine.Publish(channelName, interval)
iferr!=nil {
log.Error("failed to start goroutines to consume jobs ", err)
}
}
},
machine.GoWithTags("publish"),
)
m.Wait()
Thank you!
The text was updated successfully, but these errors were encountered:
Hello, I have added SubscribeUntil(), SubscribeWhile(), & SubscribeN() methods to the routine interface that should let you break the subscription!
// SubscribeN subscribes to the given channel until it receives N messages or its context is cancelled
SubscribeN(channel string, n int, handler func(msg interface{})) error
// SubscribeUntil subscribes to the given channel until the decider returns false for the first time. The subscription breaks when the routine's context is cancelled or the decider returns false.
SubscribeUntil(channel string, decider func() bool, handler func(msg interface{})) error
// SubscribeWhile subscribes to the given channel while the decider returns true. The subscription breaks when the routine's context is cancelled.
SubscribeWhile(channel string, decider func() bool, handler func(msg interface{})) error
Please see the updated README for an example and let me know if this will work for you.
Hello 👋,
Firstly, I would like to thank @autom8ter for bringing out the Machine. This package is super helpful in building highly concurrent Go applications.
In the below code snippet, how can I close subscribers when all the messages are consumed? Currently, the subscriber goroutine is hanging indefinitely.
Thank you!
The text was updated successfully, but these errors were encountered: