Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/flashmob/go-guerrilla
Browse files Browse the repository at this point in the history
  • Loading branch information
phires committed Jan 29, 2017
2 parents 2cf07ff + 0b01b75 commit 456713b
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 40 deletions.
38 changes: 31 additions & 7 deletions backends/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,16 @@ func New(backendName string, backendConfig BackendConfig) (Backend, error) {
return gateway, nil
}

// Distributes an envelope to one of the backend workers
// Process distributes an envelope to one of the backend workers
func (gw *BackendGateway) Process(e *envelope.Envelope) BackendResult {
if gw.State != BackendStateRunning {
return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Transaction failed - backend not running "+strconv.Itoa(gw.State)))
resp := &response.Response{
EnhancedCode: response.OtherOrUndefinedProtocolStatus,
BasicCode: 554,
Class: response.ClassPermanentFailure,
Comment: "Transaction failed - backend not running " + strconv.Itoa(gw.State),
}
return NewBackendResult(resp.String())
}

to := e.RcptTo
Expand All @@ -146,12 +152,31 @@ func (gw *BackendGateway) Process(e *envelope.Envelope) BackendResult {
select {
case status := <-savedNotify:
if status.err != nil {
return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Error: "+status.err.Error()))
resp := &response.Response{
EnhancedCode: response.OtherOrUndefinedProtocolStatus,
BasicCode: 554,
Class: response.ClassPermanentFailure,
Comment: "Error: " + status.err.Error(),
}
return NewBackendResult(resp.String())
}
resp := &response.Response{
EnhancedCode: response.OtherStatus,
BasicCode: 250,
Class: response.ClassSuccess,
Comment: fmt.Sprintf("OK : queued as %s", status.hash),
}
return NewBackendResult(response.CustomString(response.OtherStatus, 250, response.ClassSuccess, fmt.Sprintf("OK : queued as %s", status.hash)))
return NewBackendResult(resp.String())

case <-time.After(time.Second * 30):
log.Infof("Backend has timed out")
return NewBackendResult(response.CustomString(response.OtherOrUndefinedProtocolStatus, 554, response.ClassPermanentFailure, "Error: transaction timeout"))
resp := &response.Response{
EnhancedCode: response.OtherOrUndefinedProtocolStatus,
BasicCode: 554,
Class: response.ClassPermanentFailure,
Comment: "Error: transaction timeout",
}
return NewBackendResult(resp.String())
}
}
func (gw *BackendGateway) Shutdown() error {
Expand All @@ -177,9 +202,8 @@ func (gw *BackendGateway) Reinitialize() error {
err := gw.Initialize(gw.config)
if err != nil {
return fmt.Errorf("error while initializing the backend: %s", err)
} else {
gw.State = BackendStateRunning
}
gw.State = BackendStateRunning
return err
}

Expand Down
38 changes: 37 additions & 1 deletion response/enhanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
var codeMap = struct {
m map[string]int
}{m: map[string]int{
"2.1.0": 250,
"2.1.5": 250,
"2.3.0": 250,
"2.5.0": 250,
Expand Down Expand Up @@ -155,6 +156,41 @@ var defaultTexts = struct {
"5.5.1": "Invalid command",
}}

// Response type for Stringer interface
type Response struct {
EnhancedCode string
BasicCode int
Class int
// Comment is optional
Comment string
}

// Custom returns a custom Response Stringer
func (r *Response) String() string {
e := buildEnhancedResponseFromDefaultStatus(r.Class, r.EnhancedCode)
basicCode := r.BasicCode
comment := r.Comment
if len(comment) == 0 && r.BasicCode == 0 {
comment = defaultTexts.m[r.EnhancedCode]
if len(comment) == 0 {
switch r.Class {
case 2:
comment = "OK"
case 4:
comment = "Temporary failure."
case 5:
comment = "Permanent failure."
}
}
}
if r.BasicCode == 0 {
basicCode = getBasicStatusCode(e)
}

return fmt.Sprintf("%d %s %s", basicCode, e, comment)
}

/*
// CustomString builds an enhanced status code string using your custom string and basic code
func CustomString(enhancedCode string, basicCode, class int, comment string) string {
e := buildEnhancedResponseFromDefaultStatus(class, enhancedCode)
Expand All @@ -179,7 +215,7 @@ func String(enhancedCode string, class int) string {
}
return CustomString(enhancedCode, basicCode, class, comment)
}

*/
func getBasicStatusCode(enhancedStatusCode string) int {
if val, ok := codeMap.m[enhancedStatusCode]; ok {
return val
Expand Down
25 changes: 18 additions & 7 deletions response/enhanced_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package response

import "testing"
import (
"testing"
)

func TestClass(t *testing.T) {
if ClassPermanentFailure != 5 {
Expand Down Expand Up @@ -31,15 +33,24 @@ func TestGetBasicStatusCode(t *testing.T) {
// TestString for the String function
func TestCustomString(t *testing.T) {
// Basic testing
a := CustomString(OtherStatus, 200, ClassSuccess, "Test")
if a != "200 2.0.0 Test" {
t.Errorf("CustomString failed. String \"%s\" not expected.", a)
resp := &Response{
EnhancedCode: OtherStatus,
BasicCode: 200,
Class: ClassSuccess,
Comment: "Test",
}

if resp.String() != "200 2.0.0 Test" {
t.Errorf("CustomString failed. String \"%s\" not expected.", resp)
}

// Default String
b := String(OtherStatus, ClassSuccess)
if b != "200 2.0.0 OK" {
t.Errorf("String failed. String \"%s\" not expected.", b)
resp2 := &Response{
EnhancedCode: OtherStatus,
Class: ClassSuccess,
}
if resp2.String() != "200 2.0.0 OK" {
t.Errorf("String failed. String \"%s\" not expected.", resp2)
}
}

Expand Down
Loading

0 comments on commit 456713b

Please sign in to comment.