-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Acknowledger to its own package (#26)
- Loading branch information
Christian Häusler
committed
Feb 27, 2018
1 parent
e3978cb
commit 4f4a5f5
Showing
9 changed files
with
127 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ checks: | |
method-complexity: | ||
config: | ||
threshold: 10 | ||
method-length: | ||
config: | ||
threshold: 32 | ||
|
||
plugins: | ||
gofmt: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package acknowledger | ||
|
||
import ( | ||
"github.com/corvus-ch/rabbitmq-cli-consumer/config" | ||
"github.com/corvus-ch/rabbitmq-cli-consumer/delivery" | ||
) | ||
|
||
// Mapping of script exit codes and message acknowledgment. | ||
const ( | ||
exitAck = 0 | ||
exitReject = 3 | ||
exitRejectRequeue = 4 | ||
exitNack = 5 | ||
exitNackRequeue = 6 | ||
) | ||
|
||
// Acknowledger does message acknowledgment depending on the scripts exit code. | ||
type Acknowledger interface { | ||
Ack(d delivery.Delivery, code int) error | ||
} | ||
|
||
// New creates new Acknowledger using strict or default behaviour. | ||
func New(strict bool, onFailure int) Acknowledger { | ||
if strict { | ||
return &Strict{} | ||
} | ||
|
||
return &Default{ | ||
OnFailure: onFailure, | ||
} | ||
} | ||
|
||
// NewFromConfig creates a new Acknowledger from the given configuration. | ||
func NewFromConfig(cfg *config.Config) Acknowledger { | ||
if cfg.RabbitMq.Stricfailure { | ||
return &Strict{} | ||
} | ||
|
||
return &Default{cfg.RabbitMq.Onfailure} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package acknowledger | ||
|
||
import "github.com/corvus-ch/rabbitmq-cli-consumer/delivery" | ||
|
||
// Default is an Acknowledger implementation using a configurable default behaviour for script errors. | ||
type Default struct { | ||
OnFailure int | ||
} | ||
|
||
// Ack acknowledges the message on success or negatively acknowledges or rejects the message according to the configured | ||
// on error behaviour. | ||
func (a Default) Ack(d delivery.Delivery, code int) error { | ||
if code == exitAck { | ||
d.Ack() | ||
return nil | ||
} | ||
switch a.OnFailure { | ||
case exitReject: | ||
d.Reject(false) | ||
case exitRejectRequeue: | ||
d.Reject(true) | ||
case exitNack: | ||
d.Nack(false) | ||
case exitNackRequeue: | ||
d.Nack(true) | ||
default: | ||
d.Nack(true) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package acknowledger | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/corvus-ch/rabbitmq-cli-consumer/delivery" | ||
) | ||
|
||
// Strict is an Acknowledger implementation strictly using the scripts exit code. | ||
type Strict struct{} | ||
|
||
// Ack acknowledges the message on success or negatively acknowledges or rejects the message according to the scripts | ||
// exit code. It is an error if the script does not exit with one of the predefined exit codes. | ||
func (a Strict) Ack(d delivery.Delivery, code int) error { | ||
switch code { | ||
case exitAck: | ||
d.Ack() | ||
case exitReject: | ||
d.Reject(false) | ||
case exitRejectRequeue: | ||
d.Reject(true) | ||
case exitNack: | ||
d.Nack(false) | ||
case exitNackRequeue: | ||
d.Nack(true) | ||
default: | ||
d.Nack(true) | ||
return fmt.Errorf("unexpected exit code %v", code) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters