Gob encoding not working to NFS

69 views
Skip to first unread message

al...@cpu.host

unread,
Jun 14, 2019, 10:22:05 AM6/14/19
to golang-nuts
I have been writing metrics to a NFS, then reading them from another server.

In my unit tests everything works fine on a local disk, but not when I deploy.

I was previously using JSON streams on NFS, (one object per line), but gob seems like the idiomatic solution.

Since no errors were being thrown, and I just had empty files (my files should never be empty), I decided to try using os.NewFile with some other file descriptors rather than os.Create.

Here is my code which catches the metrics and writes a new file every 5 seconds if there is content.

Basically I would like to know if there is a specific file descriptor to use for this scenario of streaming the data to the NFS.

Thanks,


func (client *Client) batchLogs() {

var received int

var file *os.File
var enc *gob.Encoder

for {
t := time.After(5 * time.Second)

for {
select {

case metric := <- client.batchQueue:

if enc == nil {
file = os.NewFile(
syscall.O_ASYNC,
fmt.Sprintf("%s/%s_%v.gobs", client.volumePath, client.clientID, time.Now().UTC().Unix()),
)
if file == nil {
err := fmt.Errorf("FAILED TO MAKE NEW LOG FILE")
fmt.Println(err)
panic(err)
}
enc = gob.NewEncoder(file)
}

received++
if err := metric.Serialise(enc); err != nil {
panic(err)
}
continue

case <- t:

if file != nil {
if err := file.Close(); err != nil {
fmt.Println(err)
}
}

enc = nil
file = nil

}

break
}
}

}


Rob Pike

unread,
Jun 14, 2019, 5:31:01 PM6/14/19
to al...@cpu.host, golang-nuts
The problem is that os.NewFile makes a new File struct; it doesn't create a file. Also, the first argument should be an open file descriptor, not a flag. I think you want to use os.Create. You certainly don't want NewFile.

-rob


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/cb0bd3e7-3b1f-41f0-bd06-f02f15d44cb6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages