Skip to content

Event stream client#1

Merged
btoews merged 7 commits intomainfrom
event-client
Sep 14, 2023
Merged

Event stream client#1
btoews merged 7 commits intomainfrom
event-client

Conversation

@btoews
Copy link
Contributor

@btoews btoews commented Sep 12, 2023

This PR adds client APIs for working with the event streams added in superfly/litefs#401. A lower level EventSubscription type is added for subscribing to raw events. More generally useful is the PrimaryMonitor type that continuously monitors the event stream for changes in the cluster primary, allowing current information about the primary to be fetched immediately.

Here's a preview of the godocs.

@btoews btoews requested a review from benbjohnson September 12, 2023 19:29
- allow URL to be specified via Client type
- iterator-style API for EventSubscription
@btoews btoews requested a review from benbjohnson September 13, 2023 19:33
@btoews
Copy link
Contributor Author

btoews commented Sep 13, 2023

I added a Lag(databasePath) helper too for reading the .lag file.

client.go Outdated
Comment on lines 8 to 18
// SubscribeEvents subscribes to events from the default (localhost:20202)
// LiteFS node.
func SubscribeEvents() EventSubscription {
return DefaultClient.SubscribeEvents()
}

// MonitorPrimary monitors the primary status of the LiteFS cluster via the
// default (localhost:20202) LiteFS node's event stream.
func MonitorPrimary() PrimaryMonitor {
return DefaultClient.MonitorPrimary()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think these add much value since they're one-liners. I think you can just remove them.

Comment on lines +29 to +34
// DefaultClient is a client for communicating with the default
// (localhost:20202) LiteFS node.
var DefaultClient = &Client{
URL: "http://localhost:20202",
HTTP: http.DefaultClient,
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This works. You could also have NewClient() return a client with defaults set.

Comment on lines +17 to +27
// EventSubscription monitors a LiteFS node for published events.
type EventSubscription interface {
// Next attempts to read the next event from the LiteFS node. An error is
// returned if the request fails. Calling `Next()` again after an error will
// initiate a new HTTP request. ErrClosed is returned if the EventSubscription
// is closed while this method is blocking.
Next() (*Event, error)

// Close aborts any in-progress requests to the LiteFS node.
Close()
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it was better just having the regular struct. I'm not a huge fan of interfaces unless you really need them. I don't see users mocking this out and if they do, it's pretty easy to mock out the actual HTTP endpoint.

@benbjohnson
Copy link
Collaborator

@btoews Sorry for the slow responses. I've been slammed lately. Just some minor feedback and then we can merge it in. 👍

@btoews btoews merged commit bcdba8b into main Sep 14, 2023
@btoews btoews deleted the event-client branch September 14, 2023 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants