Skip to content

Commit

Permalink
added more error recovery into broker, versioning, starting up dev us…
Browse files Browse the repository at this point in the history
…ing foreman
  • Loading branch information
sausheong committed Sep 21, 2014
1 parent 429eeeb commit b189eb5
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.2
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
acceptor: ./polyglot
broker: broker/broker
4 changes: 1 addition & 3 deletions acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
)

func main() {
fmt.Println("Polyglot Acceptor v0.1")
fmt.Println("")
router := httprouter.New()

router.GET("/_/*p", process)
Expand All @@ -24,7 +22,7 @@ func main() {
WriteTimeout: time.Duration(config.WriteTimeout * int64(time.Second)),
MaxHeaderBytes: 1 << 20,
}
fmt.Println("Polyglot Acceptor", version(), "started at", config.Acceptor)
server.ListenAndServe()

}

32 changes: 27 additions & 5 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"
"log"
"os"
"strings"
)

var logger *log.Logger
Expand All @@ -22,8 +23,7 @@ func init() {
}

func main() {
fmt.Println("Polyglot Broker v0.1")
fmt.Println("")
fmt.Println("Polyglot Broker", version(), "started")
frontend, _ := zmq.NewSocket(zmq.ROUTER)
backend, _ := zmq.NewSocket(zmq.ROUTER)
admin, _ := zmq.NewSocket(zmq.ROUTER)
Expand Down Expand Up @@ -81,12 +81,20 @@ LOOP:
msg, err := s.RecvMessage(0)
if err != nil {
danger("Backend", "Cannot receive message", err)
break LOOP // Interrupted
break
}

identity, routeid, msg := unwrap_responder_message(msg)
if !has_valid_http_method(routeid) {
danger("Backend", "Invalid route ID sent by responder", identity, ":", routeid)
break
}

if msg != nil {
if len(msg) < 3 {
danger("Backend", "Message length sent by responder is less than 3", routeid)
break
}
_, err := frontend.SendMessage(routeid, "", msg[0], msg[1], msg[2])
if err != nil {
danger("Backend", "Cannot send message", err)
Expand Down Expand Up @@ -132,7 +140,7 @@ LOOP:
}
} else {
info("Route not found:", routeid)
_, err := frontend.SendMessage(routeid, "", "404", "{\"Content-Type\" : \"text/plain; charset=UTF-8\"}", "Route not found")
_, err := frontend.SendMessage(routeid, "", "404", "{\"Content-Type\" : \"text/html; charset=UTF-8\"}", "<html><body>Polyglot - route not found</body></html>")
if err != nil {
danger("Frontend", "Cannot send message for route not found", err)
}
Expand All @@ -145,7 +153,6 @@ LOOP:


// for unwrapping the client and responder messages

func unwrap_responder_message(msg []string) (identity string, routeid string, data []string) {
identity = msg[0]
if msg[1] == "" {
Expand Down Expand Up @@ -211,4 +218,19 @@ func show_routes() []string {
}

return r
}

// for testing responder route ID to make sure it starts with
// GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH

func has_valid_http_method(routeid string) bool {
is_valid := strings.HasPrefix(routeid, "GET") ||
strings.HasPrefix(routeid, "HEAD") ||
strings.HasPrefix(routeid, "POST") ||
strings.HasPrefix(routeid, "DELETE") ||
strings.HasPrefix(routeid, "TRACE") ||
strings.HasPrefix(routeid, "OPTIONS") ||
strings.HasPrefix(routeid, "CONNECT") ||
strings.HasPrefix(routeid, "PATCH")
return is_valid
}
5 changes: 5 additions & 0 deletions broker/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func version() string {
return "v0.1"
}
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"Acceptor" : ":8080",
"Acceptor" : "0.0.0.0:8080",
"ReadTimeout" : 10,
"WriteTimeout" : 600,
"RequestTimeout" : 2500,
Expand Down
5 changes: 5 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func version() string {
return "v0.1"
}

0 comments on commit b189eb5

Please sign in to comment.