PouchContainer RingBuffer Log Practices

PouchContainer Log Data Streams

Problem with the Log Data Forwarding Link

package mainimport (
"io"
"log"
"os"
"strconv"
"syscall"
"time"
)
var (
namedPipePath = "/tmp/namedpipe"
dataSize = 1024
)
func main() {
mkfifo()
// set the data block size for writer, unit KB
kb, _ := strconv.ParseInt(os.Args[1], 10, 64)
dataSize *= int(kb)
// create goroutine for writer side
waitCh := make(chan struct{}, 0)
go func() {
close(waitCh)
in, err := os.OpenFile(namedPipePath, syscall.O_WRONLY, 0600)
if err != nil {
log.Fatal("failed to open named pipe for writing:", err)
}
defer in.Close()
if _, err := in.Write(make([]byte, dataSize)); err != nil {
log.Fatal("failed to write it into named pipe:", err)
}
log.Printf("finished to write %d KB data into named pipe", kb)
}()
<-waitCh out, err := os.OpenFile(namedPipePath, syscall.O_RDONLY, 0600)
if err != nil {
log.Fatal("failed to open named pipe for reading:", err)
}
defer out.Close()
// don't copy the data right now to make the buffer full
time.Sleep(2 * time.Second)
log.Println("Start to read data from Named Pipe")
if _, err := io.Copy(os.Stdout, out); err != nil {
log.Fatal("failed to read the data:", err)
}
}
func mkfifo() {
os.Remove(namedPipePath)
if err := syscall.Mkfifo(namedPipePath, 0666); err != nil {
log.Fatal("failed to create named pipe:", err)
}
}
/tmp go run main.go 4 # 4KB
2018/07/02 19:37:55 finished to write 4 KB data into named pipe
2018/07/02 19:37:57 Start to read data from Named Pipe
/tmp go run main.go 64 # 64KB
2018/07/02 19:38:03 finished to write 64 KB data into named pipe
2018/07/02 19:38:05 Start to read data from Named Pipe
/tmp go run main.go 128 # 128KB
2018/07/02 19:38:12 Start to read data from Named Pipe
2018/07/02 19:38:12 finished to write 128 KB data into named pipe

RingBuffer Practices in Golang

type RingBuffer interface {
// Push pushes value into buffer and return whether it covers the oldest data or not.
Push(val interface{}) (bool, error)

// Pop pops the value in the buffer.
//
// NOTE: it returns ErrClosed if the buffer has been closed.
Pop() (interface{}, error)
// Drain returns all the data in the buffer.
//
// NOTE: it can be used after closed to make sure the data have been consumed.
Drain() []interface{}
// Close closes the ringbuffer.
Close() error
}
package mainimport "fmt"func main() {
size := 5
bufCh := make(chan int, size)
for i := 0; i < size*2; i++ {
select {
case bufCh <- i:
default:
// remove the first one
<-bufCh
// add the new one
bufCh <- i
}
}
close(bufCh)
// val == size and ok == true
val, ok := <-bufCh
fmt.Println(val, ok)
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store