Skip to content

Add a "cache size limit" option #5

@neilotoole

Description

@neilotoole

Per this reddit comment:

I would suggest having an optional variant that takes some sort of cache size limit as well, in the general spirit of "I should always be able to set limits on the resources an unbounded stream will consume". It is effectively impossible to implement that from the outside (you could but you'd basically be reimplementing the entire package for that). Exceeding the cache size can then return a special error for that case.

A potential solution (using func opts) might look like:

stream := streamcache.New(os.Stdin, streamcache.MaxCacheSize(1000000))

r := stream.NewReader(ctx)

if _, err := io.Copy(dest, r); err != nil {
  if errors.Is(err, streamcache.ErrCacheLimit) {
    // ... do something
  }
}

Maybe func opts are overkill when there's only one option. It could also be something like:

stream := streamcache.NewWithLimit(os.Stdin, 100000)

Or maybe just a method on Stream:

stream := streamcache.New(os.Stdin)
stream.SetMaxCacheSize(1000000)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions