-
-
Save abhi-bit/8fcf336986ed689e4594cf04b8a0fece to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package c | |
type EventingProducer interface { | |
Init() | |
Terminate() | |
String() string | |
ConsumerCount() int | |
} | |
type EventingConsumer interface { | |
Init() | |
Terminate() | |
String() string | |
OsPid() int | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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