-
Notifications
You must be signed in to change notification settings - Fork 69
/
async.go
59 lines (50 loc) · 1.6 KB
/
async.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package async provides async consumer abstractions and component.
package async
import (
"context"
"fmt"
"github.com/beatlabs/patron/encoding"
"github.com/beatlabs/patron/encoding/json"
"github.com/beatlabs/patron/encoding/protobuf"
)
// FailStrategy type definition.
type FailStrategy int
const (
// NackExitStrategy does not acknowledge the message and exits the application on error.
NackExitStrategy FailStrategy = iota
// NackStrategy does not acknowledge the message, leaving it for reprocessing, and continues.
NackStrategy
// AckStrategy acknowledges message and continues.
AckStrategy
)
// ProcessorFunc definition of a async processor.
type ProcessorFunc func(Message) error
// Message interface for defining messages that are handled by the async component.
type Message interface {
Context() context.Context
Decode(v interface{}) error
Ack() error
Nack() error
Source() string
Payload() []byte
}
// ConsumerFactory interface for creating consumers.
type ConsumerFactory interface {
Create() (Consumer, error)
}
// Consumer interface which every specific consumer has to implement.
type Consumer interface {
Consume(context.Context) (<-chan Message, <-chan error, error)
OutOfOrder() bool
Close() error
}
// DetermineDecoder determines the decoder based on the content type.
func DetermineDecoder(contentType string) (encoding.DecodeRawFunc, error) {
switch contentType {
case json.Type, json.TypeCharset:
return json.DecodeRaw, nil
case protobuf.Type, protobuf.TypeGoogle:
return protobuf.DecodeRaw, nil
}
return nil, fmt.Errorf("content header %s is unsupported", contentType)
}