github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/events/events.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/events/events.go
    14  
    15  // Package events is for event streaming and storage
    16  package events
    17  
    18  import (
    19  	"encoding/json"
    20  	"errors"
    21  	"time"
    22  )
    23  
    24  var (
    25  	// DefaultStream is the default events stream implementation
    26  	DefaultStream Stream
    27  	// DefaultStore is the default events store implementation
    28  	DefaultStore Store
    29  )
    30  
    31  var (
    32  	// ErrMissingTopic is returned if a blank topic was provided to publish
    33  	ErrMissingTopic = errors.New("Missing topic")
    34  	// ErrEncodingMessage is returned from publish if there was an error encoding the message option
    35  	ErrEncodingMessage = errors.New("Error encoding message")
    36  )
    37  
    38  // Stream is an event streaming interface
    39  type Stream interface {
    40  	Publish(topic string, msg interface{}, opts ...PublishOption) error
    41  	Consume(topic string, opts ...ConsumeOption) (<-chan Event, error)
    42  }
    43  
    44  // Store is an event store interface
    45  type Store interface {
    46  	Read(topic string, opts ...ReadOption) ([]*Event, error)
    47  	Write(event *Event, opts ...WriteOption) error
    48  }
    49  
    50  type AckFunc func() error
    51  type NackFunc func() error
    52  
    53  // Event is the object returned by the broker when you subscribe to a topic
    54  type Event struct {
    55  	// ID to uniquely identify the event
    56  	ID string
    57  	// Topic of event, e.g. "registry.service.created"
    58  	Topic string
    59  	// Timestamp of the event
    60  	Timestamp time.Time
    61  	// Metadata contains the values the event was indexed by
    62  	Metadata map[string]string
    63  	// Payload contains the encoded message
    64  	Payload []byte
    65  
    66  	ackFunc  AckFunc
    67  	nackFunc NackFunc
    68  }
    69  
    70  // Unmarshal the events message into an object
    71  func (e *Event) Unmarshal(v interface{}) error {
    72  	return json.Unmarshal(e.Payload, v)
    73  }
    74  
    75  // Ack acknowledges successful processing of the event in ManualAck mode
    76  func (e *Event) Ack() error {
    77  	return e.ackFunc()
    78  }
    79  
    80  func (e *Event) SetAckFunc(f AckFunc) {
    81  	e.ackFunc = f
    82  }
    83  
    84  // Nack negatively acknowledges processing of the event (i.e. failure) in ManualAck mode
    85  func (e *Event) Nack() error {
    86  	return e.nackFunc()
    87  }
    88  
    89  func (e *Event) SetNackFunc(f NackFunc) {
    90  	e.nackFunc = f
    91  }
    92  
    93  // Publish an event to a topic
    94  func Publish(topic string, msg interface{}, opts ...PublishOption) error {
    95  	return DefaultStream.Publish(topic, msg, opts...)
    96  }
    97  
    98  // Consume to events
    99  func Consume(topic string, opts ...ConsumeOption) (<-chan Event, error) {
   100  	return DefaultStream.Consume(topic, opts...)
   101  }
   102  
   103  // Read events for a topic
   104  func Read(topic string, opts ...ReadOption) ([]*Event, error) {
   105  	return DefaultStore.Read(topic, opts...)
   106  }