Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure straw store is closed when initialising freezer via url. #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 73 additions & 35 deletions freezer/freezer_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package freezer

import (
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/uw-labs/freezer"
"github.com/uw-labs/straw"
"github.com/uw-labs/substrate"
Expand Down Expand Up @@ -44,14 +46,13 @@ func newFreezerSink(u *url.URL) (substrate.AsyncMessageSink, error) {
}
}

var streamstore straw.StreamStore
var err error
var (
store straw.StreamStore
err error
)
switch u.Scheme {
case "freezer+dir":
streamstore, err = strawOpen("file:///")
if err != nil {
return nil, err
}
store, err = strawOpen("file:///")
case "freezer+s3":
u1 := url.URL{
Scheme: "s3",
Expand All @@ -73,27 +74,43 @@ func newFreezerSink(u *url.URL) (substrate.AsyncMessageSink, error) {
}

u1.RawQuery = newVals.Encode()
store, err = strawOpen(u1.String())

streamstore, err = strawOpen(u1.String())
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported scheme : %s", u.Scheme)
}
if err != nil {
return nil, err
}
conf := AsyncMessageSinkConfig{
StreamStore: streamstore,
StreamStore: store,
MaxUnflushedMessages: maxUnflushed,
FreezerConfig: freezer.MessageSinkConfig{
Path: u.Path,
CompressionType: ct,
},
}
return sinker(conf)
sink, err := sinker(conf)
if err != nil {
return nil, err
}
return closeStoreSink{
AsyncMessageSink: sink,
store: store,
}, nil
}

var sinker = NewAsyncMessageSink

type closeStoreSink struct {
substrate.AsyncMessageSink
store straw.StreamStore
}

func (sink closeStoreSink) Close() (err error) {
return closeAll([]io.Closer{sink.AsyncMessageSink, sink.store})
}

func newFreezerSource(u *url.URL) (substrate.AsyncMessageSource, error) {
q := u.Query()

Expand All @@ -107,21 +124,17 @@ func newFreezerSource(u *url.URL) (substrate.AsyncMessageSource, error) {
return nil, fmt.Errorf("unknown compression type : %s", cts)
}

var (
store straw.StreamStore
err error
)

switch u.Scheme {
case "freezer+dir":
ss, err := strawOpen("file:///")
store, err = strawOpen("file:///")
if err != nil {
return nil, err
}
conf := AsyncMessageSourceConfig{
StreamStore: ss,
FreezerConfig: freezer.MessageSourceConfig{
Path: u.Path,
PollPeriod: 10 * time.Second,
CompressionType: ct,
},
}
return sourcer(conf)
case "freezer+s3":
u1 := url.URL{Scheme: "s3", Host: u.Hostname()}

Expand All @@ -139,26 +152,51 @@ func newFreezerSource(u *url.URL) (substrate.AsyncMessageSource, error) {
}
}
u1.RawQuery = newVals.Encode()
store, err = strawOpen(u1.String())

ss, err := strawOpen(u1.String())
if err != nil {
return nil, err
}
conf := AsyncMessageSourceConfig{
StreamStore: ss,
FreezerConfig: freezer.MessageSourceConfig{
Path: u.Path,
PollPeriod: 10 * time.Second,
CompressionType: ct,
},
}
return sourcer(conf)
default:
return nil, fmt.Errorf("unsupported scheme : %s", u.Scheme)
}
if err != nil {
return nil, err
}

conf := AsyncMessageSourceConfig{
StreamStore: store,
FreezerConfig: freezer.MessageSourceConfig{
Path: u.Path,
PollPeriod: 10 * time.Second,
CompressionType: ct,
},
}
source, err := sourcer(conf)
if err != nil {
return nil, err
}
return &closeStoreSource{
AsyncMessageSource: source,
store: store,
}, nil
}

var sourcer = NewAsyncMessageSource

type closeStoreSource struct {
substrate.AsyncMessageSource
store straw.StreamStore
}

func (sink closeStoreSource) Close() (err error) {
return closeAll([]io.Closer{sink.AsyncMessageSource, sink.store})
}

var strawOpen = straw.Open

func closeAll(closers []io.Closer) (outErr error) {
for _, c := range closers {
if err := c.Close(); err != nil {
outErr = multierror.Append(outErr, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closeAll would be about 1 line longer if it didn't use multierror. Personally, I don't think it's worth the extra dependency.

Also, the call sites would be cleaner if this was variadic.

}
}
return outErr
}