Skip to content

Instantly share code, notes, and snippets.

@moredure
Last active November 21, 2019 12:50
Show Gist options
  • Select an option

  • Save moredure/567bfa5db9f0f3cd4b94be8ebcec81a0 to your computer and use it in GitHub Desktop.

Select an option

Save moredure/567bfa5db9f0f3cd4b94be8ebcec81a0 to your computer and use it in GitHub Desktop.
package main
type Sequencer struct {
sync.Mutex
tasks map[string]chan string
}
func (t *Sequencer) run(msg string) {
for {
select {
case <-t.tasks[msg]:
time.Sleep(5 * time.Second)
println(msg)
default:
t.Lock()
select {
case <-t.tasks[msg]:
t.Unlock()
time.Sleep(2 * time.Second)
println(msg)
default:
close(t.tasks[msg])
delete(t.tasks, msg)
t.Unlock()
return
}
}
}
}
func (t *Sequencer) Execute(msg string) {
t.Lock()
tasks, ok := t.tasks[msg]
if !ok {
ch := make(chan string, 5)
ch <- msg
t.tasks[msg] = ch
t.Unlock()
go t.run(msg)
return
}
tasks <- msg
t.Unlock()
}
func NewSequencer() *Sequencer {
return &Sequencer{
tasks: make(map[string]chan string),
}
}
func main() {
s := NewSequencer()
go s.Execute("fuck")
go s.Execute("fuck")
go s.Execute("shit")
go s.Execute("shit")
go s.Execute("shit")
go s.Execute("bowl")
time.Sleep(100 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment