github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/events/plugin.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package events
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kaleido-io/firefly/internal/config"
    23  	"github.com/kaleido-io/firefly/pkg/fftypes"
    24  )
    25  
    26  // Plugin is the interface implemented by event interface
    27  // - Delivery to generic application code - WebSockets, Webhooks, AMQP etc.
    28  // - Integration of frameworks for coordination of multi-party compute - Hyperledger Avalon,  etc.
    29  type Plugin interface {
    30  	fftypes.Named
    31  
    32  	// InitPrefix initializes the set of configuration options that are valid, with defaults. Called on all plugins.
    33  	InitPrefix(prefix config.Prefix)
    34  
    35  	// Init initializes the plugin, with configuration
    36  	// Returns the supported featureset of the interface
    37  	Init(ctx context.Context, prefix config.Prefix, callbacks Callbacks) error
    38  
    39  	// Capabilities returns capabilities - not called until after Init
    40  	Capabilities() *Capabilities
    41  
    42  	// DeliveryRequest requests delivery of work on a connection, which must later be responded to
    43  	DeliveryRequest(connID string, event *fftypes.EventDelivery) error
    44  }
    45  
    46  type SubscriptionMatcher func(fftypes.SubscriptionRef) bool
    47  
    48  type Callbacks interface {
    49  
    50  	// RegisterConnection can be fired as often as requied.
    51  	// Dispatchers will be started against this connection for all persisted subscriptions that match via the supplied function.
    52  	// It can be fired multiple times for the same connection ID, to update the subscription list
    53  	// For a "connect-out" style plugin (MQTT/AMQP/JMS broker), you might fire it at startup (from Init) for each target queue, with a subscription match
    54  	// For a "connect-in" style plugin (inbound WebSocket connections), you fire it every time the client application connects attaches to a subscription
    55  	RegisterConnection(connID string, matcher SubscriptionMatcher) error
    56  
    57  	// EphemeralSubscription creates an ephemeral (non-durable) subscription, and associates it with a connection
    58  	EphemeralSubscription(connID, namespace string, filter fftypes.SubscriptionFilter, options fftypes.SubscriptionOptions) error
    59  
    60  	// ConnnectionClosed is a notification that a connection has closed, and all dispatchers should be re-allocated.
    61  	// Note the plugin must not crash if it receives PublishEvent calls on the connID after the ConnectionClosed event is fired
    62  	ConnnectionClosed(connID string)
    63  
    64  	// DeliveryResponse responds to a previous event delivery, to either:
    65  	// - Acknowledge it: the offset for the associated subscription can move forwards
    66  	//   * Note all gaps must fill before the offset can move forwards, so this message might still be redelivered if streaming ahead
    67  	// - Reject it: This resets the associated subscription back to the last committed offset
    68  	//   * Note all message since the last committed offet will be redelivered, so additional messages to be redelivered if streaming ahead
    69  	DeliveryResponse(connID string, inflight fftypes.EventDeliveryResponse) error
    70  }
    71  
    72  type Capabilities struct {
    73  }