News for hackers. Stuff that matters.
Re:Buffer: Composable Buffers for Go by stinos on Sunday April 05, 2015 @03:56PM (#9325134)

   // Buffer 32KB to Memory, after that buffer to 100MB chunked files
  buf := buffer.NewUnboundedBuffer(32*1024, 100*1024*1024)

  pool := NewFilePool(100*1024*1024, "") // "" -- use temp dir
I seem to have a twofold feeling about this code: on one hand I really like the brevity of these statements, but on the other hand it does come at the cost of being rather cryptic: without the comments one can only guess what exactly is going on. To the point that should I want to use this in my own code and make it understandable I'd almost would be forced to either copy the comments as well or else wrap it in a method with another name or. In such cases it probably would have been better if the original API already had done this.
Re:Buffer: Composable Buffers for Go by gouggoug on Sunday April 05, 2015 @04:15PM (#9325181)
I feel like we could say that about any code. It doesn't sound cryptic to me. It does require to read the doc (http://godoc.org/github.com/djherbis/buffer#NewUnboundedBuff...) but I'd argue that's the case for everything.
Re:Buffer: Composable Buffers for Go by djherbis on Sunday April 05, 2015 @07:13PM (#9325653)
Just to add to the other comments made here, the NewUnboundedBuffer is meant to be a convenience method. It's actually just a (hopefully useful) example of how to compose the other buffer types to create a more complicated buffer. Thanks for the comment, hope it helps clear that up.
Re:Buffer: Composable Buffers for Go by nkatsaros on Sunday April 05, 2015 @06:35PM (#9325567)
If anything this is just a nomenclature problem. It's common for Go packages to have New* functions for common use cases and this is one example. After looking at the source it is very clear what it does.
Re:Buffer: Composable Buffers for Go by TillE on Sunday April 05, 2015 @07:26PM (#9325686)
That's one problem that named arguments solve pretty nicely. Unfortunately, I don't think Go supports them.
Re:Buffer: Composable Buffers for Go by MetaCosm on Sunday April 05, 2015 @08:03PM (#9325775)
There are a lot of ways to make arguments nicer in Go. The simple on is to just use a configuration struct (or anon struct) at that has nice names. But, for complex stuff, functional options as laid out by Dave Cheney: http://dave.cheney.net/2014/10/17/functional-options-for-fri... (video: https://www.youtube.com/watch?v=24lFtGHWxAQ)
Re:Buffer: Composable Buffers for Go by lobster_johnson on Sunday April 05, 2015 @07:54PM (#9325750)
You have anonymous strict literals, though, which are nearly as succinct and can serve the same role.
Re:Buffer: Composable Buffers for Go by elithrar on Sunday April 05, 2015 @10:23PM (#9326136)
Nice work. buffer.NewPool is a great convenience over writing your own sync.Pool.

I was previously using https://github.com/oxtoacart/bpool as a 64K buffer pool for rendering (concurrently) template/html contents—so I can check for the errors from template.Render—before then using io.Copy to copy the "known good" contents to the http.ResponseWriter. I may have to look into using this.

Re:Buffer: Composable Buffers for Go by djherbis on Sunday April 05, 2015 @06:52PM (#9325601)
Hey author here, happy to answer your questions.
Re:Buffer: Composable Buffers for Go by heavenlyhash on Sunday April 05, 2015 @07:54PM (#9325751)
Have you / would you consider extending this concept to buffers that are still single writer but support many readers -- something like a `FreshReader() io.Reader` function that returns a reader that starts from the beginning again? (I have an application that needs this semantic and ended up just rolling it quick-and-dirty; this sounds like very similar concepts done up with better gift wrapping.)
Re:Buffer: Composable Buffers for Go by djherbis on Sunday April 05, 2015 @08:09PM (#9325789)
I'm currently working on something that does that sort of thing. https://github.com/djherbis/fscache

It doesn't have the composability features yet, but it already handles 1 writer with many concurrent readers. Let me know if that helps!

Re:Buffer: Composable Buffers for Go by kenferry on Sunday April 05, 2015 @05:25PM (#9325384)
As a relative go newb, I do have a question about this: I thought channels/go routines were already essentially composable buffers. No?
Re:Buffer: Composable Buffers for Go by fishnchips on Sunday April 05, 2015 @06:42PM (#9325585)
Yes and no. Goroutines are lightweight threads - what other languages might call fibers. Channels are synchronisation primitives designed to work with goroutines but they do indeed provide some buffering. What OP is doing though is creating customisable buffers that allow you to control for memory usage and that are likely designed to replace things like bytes.Buffer rather then buffered channels. Of course I may be totally wrong ;)
Re:Buffer: Composable Buffers for Go by djherbis on Sunday April 05, 2015 @07:07PM (#9325643)
You've got the right idea. bytes.Buffer is great (I even use it under the hood) but this repo is intended to give you more control over how your buffer behaves as the amount of data stored in it changes.