Skip to content

Instantly share code, notes, and snippets.

@pellaeon
Created July 29, 2015 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pellaeon/ed316171d44605f4ae04 to your computer and use it in GitHub Desktop.
Save pellaeon/ed316171d44605f4ae04 to your computer and use it in GitHub Desktop.
Simultaenous Reader reading and read from slice PoC
package main
import (
"fmt"
"io"
"net/http"
"sync/atomic"
"time"
)
var p []byte
var c int64
type ProgressReader struct {
io.Reader
count int64
}
func (pr *ProgressReader) Read(p []byte) (n int, err error) {
n, err = pr.Reader.Read(p)
aa := pr.count
atomic.AddInt64(&pr.count, int64(n))
if aa > pr.count {
fmt.Println("!!! how is this possible!!")
}
fmt.Printf("%p Progress%d: %d -> %d\n", &pr.count, c, aa, pr.count)
atomic.AddInt64(&c, int64(1))
return
}
func main() {
/*
fd, _ := os.Open("123.dat")
defer fd.Close()
info, _ := fd.Stat()
p = make([]byte, info.Size())
*/
resp, err := http.Get("http://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-arm64-disk1.img")
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
pr := ProgressReader{
Reader: resp.Body,
count: 0,
}
p = make([]byte, 325386752)
ticker := time.NewTicker(time.Millisecond * 800)
//ch1 := make(chan bool)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
readp()
//fmt.Println("blocked until", time.Now())
}
}()
io.ReadFull(&pr, p)
//<-ch1
}
func readp() {
var i int
for i = len(p) - 1; i > 0; i-- {
if p[i] != 0 {
break
}
}
fmt.Println("readp: ", i)
//ch1 <- true
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment