41 lines
829 B
Go
41 lines
829 B
Go
|
|
package client
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"os"
|
||
|
|
|
||
|
|
"golang.org/x/sys/unix"
|
||
|
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (r *pipeListener) listen(ctx context.Context) {
|
||
|
|
mode := 0666 // 읽기/쓰기 권한
|
||
|
|
pipeName := "/tmp/" + r.config.MetricPipeName
|
||
|
|
|
||
|
|
os.Remove(pipeName)
|
||
|
|
if err := unix.Mkfifo(pipeName, uint32(mode)); err != nil {
|
||
|
|
logger.Println("mkfifo failed :", pipeName, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer os.Remove(pipeName)
|
||
|
|
|
||
|
|
go func() {
|
||
|
|
// file에 쓰기 핸들을 하나 열고 ctx가 Done일 때 닫음. 이래야 reader가 계속 열려있게 됨
|
||
|
|
f, err := os.OpenFile(pipeName, os.O_WRONLY, 0)
|
||
|
|
if err != nil {
|
||
|
|
logger.Println(err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
<-ctx.Done()
|
||
|
|
f.Close()
|
||
|
|
}()
|
||
|
|
|
||
|
|
file, err := os.Open(pipeName)
|
||
|
|
if err != nil {
|
||
|
|
logger.Println("FIFO open error:", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r.startReader(file)
|
||
|
|
}
|