Skip to content

Send messages to channel with specified time to receive

License

Notifications You must be signed in to change notification settings

mehanizm/bivrost

Repository files navigation

BIVROST

Send messages to channel with specified time to receive

GoDoc Go codecov Go Report

See an example in cmd/main.go

func main() {

	log.Println("start")

	s, in, out := bivrost.Init()
	go s.Serve()

	var events = []event{
		{1, "one second"},
		{2, "two seconds"},
		{3, "three seconds"},
	}

	go func() {
		for _, event := range events {
			in <- &bivrost.Event{
				When:   time.Now().Add(time.Duration(event.after) * time.Second),
				Entity: interface{}(event.message),
			}
		}
	}()

	go func() {
		for event := range out {
			log.Println(event)
		}
	}()

	time.Sleep(4 * time.Second)
	s.Cancel()
	time.Sleep(1 * time.Millisecond)
}

OUTPUT:

>> 2020/10/12 13:08:31 start
>> 2020/10/12 13:08:32 one second
>> 2020/10/12 13:08:33 two seconds
>> 2020/10/12 13:08:34 three seconds
>> 2020/10/12 13:08:35 bivrost [bivrost] INFO: cancel serving

About

Send messages to channel with specified time to receive

Resources

License

Stars

Watchers

Forks

Packages

No packages published