mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Added IronCache (not used yet).
Using copy of reverse proxy so can get error response.
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
// I wanted to do some stuff to this so had to make a copy. Namely change the Host handling for virtual hosts.
|
||||
/* I wanted to do some stuff to this so had to make a copy. Namely:
|
||||
- change the Host handling for virtual hosts.
|
||||
- get errors if the proxy request fails
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// Copyright 2011 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
@@ -83,7 +88,7 @@ func copyHeader(dst, src http.Header) {
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) error {
|
||||
transport := p.Transport
|
||||
if transport == nil {
|
||||
transport = http.DefaultTransport
|
||||
@@ -115,8 +120,8 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
res, err := transport.RoundTrip(outreq)
|
||||
if err != nil {
|
||||
log.Printf("http: proxy error: %v", err)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
// rw.WriteHeader(http.StatusInternalServerError)
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
@@ -124,6 +129,9 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
p.copyResponse(rw, res.Body)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
|
||||
|
||||
@@ -11,19 +11,28 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/iron-io/iron_go/cache"
|
||||
// "github.com/iron-io/iron_go/config"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var routingTable = map[string]Route{}
|
||||
var icache = cache.New("routertest")
|
||||
|
||||
func init() {
|
||||
icache.Settings.UseConfigMap(map[string]interface{}{"token": "MWx0VfngzsCu0W8NAYw7S2lNrgo", "project_id": "50e227be8e7d14359b001373"})
|
||||
}
|
||||
|
||||
type Route struct {
|
||||
// TODO: Change destinations to a simple cache so it can expire entries after 55 minutes (the one we use in common?)
|
||||
Destinations []string
|
||||
ProjectId string
|
||||
Token string // store this so we can queue up new workers on demand
|
||||
|
||||
}
|
||||
|
||||
// for adding new hosts
|
||||
@@ -52,6 +61,11 @@ func main() {
|
||||
func ProxyFunc(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Println("HOST:", req.Host)
|
||||
host := strings.Split(req.Host, ":")[0]
|
||||
|
||||
// We look up the destinations in the routing table and there can be 3 possible scenarios:
|
||||
// 1) This host was never registered so we return 404
|
||||
// 2) This host has active workers so we do the proxy
|
||||
// 3) This host has no active workers so we queue one (or more) up and return a 503 or something with message that says "try again in a minute"
|
||||
route := routingTable[host]
|
||||
// choose random dest
|
||||
if len(route.Destinations) == 0 {
|
||||
@@ -67,14 +81,28 @@ func ProxyFunc(w http.ResponseWriter, req *http.Request) {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("proxying to", destUrl)
|
||||
proxy := httputil.NewSingleHostReverseProxy(destUrl)
|
||||
proxy.ServeHTTP(w, req)
|
||||
proxy := NewSingleHostReverseProxy(destUrl)
|
||||
err = proxy.ServeHTTP(w, req)
|
||||
if err != nil {
|
||||
fmt.Println("Error proxying!", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
// start new worker if it's a connection error
|
||||
return
|
||||
}
|
||||
fmt.Println("Served!")
|
||||
// todo: how to handle destination failures. I got this in log output when testing a bad endpoint:
|
||||
// 2012/12/26 23:22:08 http: proxy error: dial tcp 127.0.0.1:8082: connection refused
|
||||
}
|
||||
|
||||
// When a worker starts up, it calls this
|
||||
func AddWorker(w http.ResponseWriter, req *http.Request) {
|
||||
log.Println("AddWorker called!")
|
||||
|
||||
// get project id and token
|
||||
projectId := req.FormValue("project_id")
|
||||
token := req.FormValue("token")
|
||||
fmt.Println("project_id:", projectId, "token:", token)
|
||||
|
||||
r2 := Route2{}
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.Decode(&r2)
|
||||
@@ -82,9 +110,12 @@ func AddWorker(w http.ResponseWriter, req *http.Request) {
|
||||
fmt.Println("DECODED:", r2)
|
||||
|
||||
// todo: routing table should be in mongo (or IronCache?) so all routers can update/read from it.
|
||||
// todo: one cache entry per host domain
|
||||
route := routingTable[r2.Host]
|
||||
fmt.Println("ROUTE:", route)
|
||||
route.Destinations = append(route.Destinations, r2.Dest)
|
||||
route.ProjectId = projectId
|
||||
route.Token = token
|
||||
fmt.Println("ROUTE:", route)
|
||||
routingTable[r2.Host] = route
|
||||
fmt.Println("New routing table:", routingTable)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
require 'rest'
|
||||
require 'sinatra'
|
||||
|
||||
# The backend would do this part before execute the worker
|
||||
########################################################
|
||||
rest = Rest::Client.new
|
||||
rest.logger.level = Logger::DEBUG
|
||||
|
||||
@@ -10,10 +12,9 @@ puts "public dns name: #{public_dns}"
|
||||
port = rand(50000..55000)
|
||||
puts "port: #{port}"
|
||||
|
||||
|
||||
response = rest.post(
|
||||
# "http://localhost:8080/",
|
||||
"http://router.irondns.info/",
|
||||
"http://router.irondns.info/?project_id=#{params[:project_id]}&token=#{params[:token]}",
|
||||
headers: {"Iron-Router"=>"YES!"},
|
||||
body: {"host"=>"routertest.irondns.info", "dest"=>"#{public_dns}:#{port}"})
|
||||
puts "body:"
|
||||
@@ -21,6 +22,9 @@ puts response.body
|
||||
|
||||
STDOUT.flush
|
||||
|
||||
# Now we start the actual worker
|
||||
##################################################################3
|
||||
|
||||
ENV['PORT'] = port.to_s # for sinatra
|
||||
my_app = Sinatra.new do
|
||||
set :port, port
|
||||
|
||||
Reference in New Issue
Block a user