Skip to content

Instantly share code, notes, and snippets.

@LittleKey
Created July 31, 2018 15:49
Show Gist options
  • Save LittleKey/01c51da2f00dcdb8a90c81639a1a6929 to your computer and use it in GitHub Desktop.
Save LittleKey/01c51da2f00dcdb8a90c81639a1a6929 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
// Message ...
type Message struct {
cmd string
timestamp time.Time
}
func randint(min int, max int) int {
return rand.Intn(max-min) + min
}
func ack(ch chan string, cmd string) {
num := randint(3, 10)
time.Sleep(time.Duration(num) * time.Second)
ch <- fmt.Sprintf("ACK: %s", cmd)
}
func send(ch chan Message) {
for {
num := randint(5, 10)
time.Sleep(time.Duration(num) * time.Second)
timestamp := time.Now()
msg := new(Message)
msg.cmd = fmt.Sprintf("cmd%d", num)
msg.timestamp = timestamp
ch <- *msg
}
}
func retry(sendChan chan Message, ackChan chan string, msg Message) {
now := time.Now()
if now.Sub(msg.timestamp) < time.Duration(2*time.Second) {
newMsg := new(Message)
newMsg.cmd = fmt.Sprintf("Retry: %s", msg.cmd)
newMsg.timestamp = msg.timestamp
sendChan <- *newMsg
ack(ackChan, msg.cmd)
} else {
fmt.Printf("Timeout: %s", msg.cmd)
}
}
func main() {
ackChan := make(chan string, 10)
sendChan := make(chan Message, 10)
var msg *Message
go send(sendChan)
for {
select {
case res := <-ackChan:
fmt.Println(res)
case res := <-sendChan:
msg = new(Message)
msg.cmd = res.cmd
msg.timestamp = res.timestamp
fmt.Println(res.cmd)
go ack(ackChan, res.cmd)
case <-time.After(1 * time.Second):
if msg != nil {
go retry(sendChan, ackChan, *msg)
msg = nil
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment