Skip to content

Instantly share code, notes, and snippets.

@abhi-bit
Last active January 29, 2017 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhi-bit/8fcf336986ed689e4594cf04b8a0fece to your computer and use it in GitHub Desktop.
Save abhi-bit/8fcf336986ed689e4594cf04b8a0fece to your computer and use it in GitHub Desktop.
package a
import (
"log"
"time"
"github.com/abhi-bit/tools/eventing/circular_dependency/b"
"github.com/abhi-bit/tools/eventing/circular_dependency/c"
)
type Producer struct {
AppName string
consumerCount int
stopCh chan bool
consumers []c.EventingConsumer
}
func (p *Producer) Init() {
p.stopCh = make(chan bool, 1)
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-ticker.C:
c := b.New(p, p.AppName)
p.consumers = append(p.consumers, c)
p.consumerCount += 1
log.Println("Created another consumer, len:", p.consumerCount)
case <-p.stopCh:
return
}
}
}
func (p *Producer) Terminate() {
p.stopCh <- true
}
func (p *Producer) String() string {
return p.AppName
}
func (p *Producer) ConsumerCount() int {
return p.consumerCount
}
package b
import (
"fmt"
"log"
"math/rand"
"github.com/abhi-bit/tools/eventing/circular_dependency/c"
)
type Consumer struct {
appName string
osPid int
parentProducer c.EventingProducer
}
func New(p c.EventingProducer, appName string) *Consumer {
consumer := &Consumer{
appName: appName,
osPid: rand.Intn(10000),
parentProducer: p,
}
return consumer
}
func (c *Consumer) Init() {
log.Println("Dummy Init")
}
func (c *Consumer) Terminate() {
log.Println("Dummy Terminate")
}
func (c *Consumer) String() string {
return fmt.Sprintf("%s_%d", c.appName, c.osPid)
}
func (c *Consumer) OsPid() int {
return c.osPid
}
package c
type EventingProducer interface {
Init()
Terminate()
String() string
ConsumerCount() int
}
type EventingConsumer interface {
Init()
Terminate()
String() string
OsPid() int
}
package main
import (
"fmt"
"time"
"github.com/abhi-bit/tools/eventing/circular_dependency/a"
)
func main() {
producer := &a.Producer{
AppName: "credit_score",
}
producer.Init()
time.Sleep(100)
fmt.Println("Existing main")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment