goflow icon indicating copy to clipboard operation
goflow copied to clipboard

Manipulate a network in running

Open brucekim opened this issue 3 years ago • 0 comments

@trustmaster

Hello, I would like to know how to dynamically manipulate a network in running.

I tested with a simple code below. However inserting a new process into the network already initialized doesn't change after the network running. I am not sure whether this is a nature of design for flow based programming.

In addition, I would like to hear whether implementing such dynamic manipulating a network is meaningful or not.

type SrcTest struct {
	Out chan<- byte
}

type FilterTest1 struct {
	In <-chan byte
	Out chan<- byte
}

type FilterTest2 struct {
	In <-chan byte
	Out chan<- byte
}

type SinkTest struct {
	In <-chan byte
}

func (s *SrcTest) Process() {
	for {
		s.Out <- 'a'
		time.Sleep(100 * time.Millisecond)
	}
}

func (f *FilterTest1) Process() {
	for in := range f.In {
		_ = in
		f.Out <- 'b'
	}
}

func (f *FilterTest2) Process() {
	for in := range f.In {
		_ = in
		log.Infof("f2 f2")
		f.Out <- 'c'
	}
}

func (s *SinkTest) Process() {
	for in := range s.In {
		log.Infof("recv: %c", in)
	}
}

func TestStreamData(t *testing.T) {
	n := goflow.NewGraph()
	n.Add("src", new(SrcTest))
	n.Add("f1", new(FilterTest1))
	n.Add("sink", new(SinkTest))
	n.Connect("src", "Out", "f1", "In")
	n.Connect("f1", "Out", "sink", "In")

	wait := goflow.Run(n)
	go func() {
		time.Sleep(2 * time.Second)
		log.Infof("insert f2")


		n.Add("f2", new(FilterTest2))
		//n.Connect("f1", "Out", "f2", "In")
		n.Connect("src", "Out", "f2", "In")
		n.Connect("f2", "Out", "sink", "In")

	}()

	<-wait
}

brucekim avatar Jun 22 '21 08:06 brucekim