mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 05:23:04 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package submatview
|
|
|
|
import (
|
|
"github.com/hashicorp/consul/proto/private/pbsubscribe"
|
|
)
|
|
|
|
// eventHandler is a function which performs some operation on the received
|
|
// events, then returns the eventHandler that should be used for the next set
|
|
// of events.
|
|
// If eventHandler fails to handle the events it may return an error. If an
|
|
// error is returned the next eventHandler will be ignored.
|
|
// eventHandler is used to implement a very simple finite-state machine.
|
|
type eventHandler func(state viewState, events *pbsubscribe.Event) (next eventHandler, err error)
|
|
|
|
type viewState interface {
|
|
updateView(events []*pbsubscribe.Event, index uint64) error
|
|
reset()
|
|
}
|
|
|
|
func initialHandler(index uint64) eventHandler {
|
|
if index == 0 {
|
|
return newSnapshotHandler()
|
|
}
|
|
return resumeStreamHandler
|
|
}
|
|
|
|
// snapshotHandler accumulates events. When it receives an EndOfSnapshot event
|
|
// it updates the view, and then returns eventStreamHandler to handle new events.
|
|
type snapshotHandler struct {
|
|
events []*pbsubscribe.Event
|
|
}
|
|
|
|
func newSnapshotHandler() eventHandler {
|
|
return (&snapshotHandler{}).handle
|
|
}
|
|
|
|
func (h *snapshotHandler) handle(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
|
if event.GetEndOfSnapshot() {
|
|
err := state.updateView(h.events, event.Index)
|
|
return eventStreamHandler, err
|
|
}
|
|
|
|
h.events = append(h.events, eventsFromEvent(event)...)
|
|
return h.handle, nil
|
|
}
|
|
|
|
// eventStreamHandler handles events by updating the view. It always returns
|
|
// itself as the next handler.
|
|
func eventStreamHandler(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
|
err := state.updateView(eventsFromEvent(event), event.Index)
|
|
return eventStreamHandler, err
|
|
}
|
|
|
|
func eventsFromEvent(event *pbsubscribe.Event) []*pbsubscribe.Event {
|
|
if batch := event.GetEventBatch(); batch != nil {
|
|
return batch.Events
|
|
}
|
|
return []*pbsubscribe.Event{event}
|
|
}
|
|
|
|
// resumeStreamHandler checks if the event is a NewSnapshotToFollow event. If it
|
|
// is it resets the view and returns a snapshotHandler to handle the next event.
|
|
// Otherwise it uses eventStreamHandler to handle events.
|
|
func resumeStreamHandler(state viewState, event *pbsubscribe.Event) (eventHandler, error) {
|
|
if event.GetNewSnapshotToFollow() {
|
|
state.reset()
|
|
return newSnapshotHandler(), nil
|
|
}
|
|
return eventStreamHandler(state, event)
|
|
}
|