forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 7
/
osrmhttpclient.go
118 lines (99 loc) · 3.05 KB
/
osrmhttpclient.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package osrmconnector
import (
"encoding/json"
"net/http"
"strings"
"github.com/Telenav/osrm-backend/integration/pkg/api"
"github.com/Telenav/osrm-backend/integration/pkg/api/osrm/route"
"github.com/Telenav/osrm-backend/integration/pkg/api/osrm/table"
"github.com/Telenav/osrm-backend/integration/pkg/backend"
"github.com/golang/glog"
)
type osrmHTTPClient struct {
osrmBackendEndpoint string
httpclient http.Client
requestC chan *request
}
func newOsrmHTTPClient(osrmEndpoint string) *osrmHTTPClient {
osrmEndpoint = strings.TrimSuffix(osrmEndpoint, api.Slash)
return &osrmHTTPClient{
osrmBackendEndpoint: osrmEndpoint,
httpclient: http.Client{Timeout: backend.Timeout()},
requestC: make(chan *request),
}
}
func (oc *osrmHTTPClient) submitRouteReq(r *route.Request) <-chan RouteResponse {
var url string
if !strings.HasPrefix(oc.osrmBackendEndpoint, "http://") {
url += "http://"
}
url = url + oc.osrmBackendEndpoint + r.RequestURI()
req := newOsrmRequest(url, OSRMRoute)
oc.requestC <- req
glog.V(3).Infof("[osrmHTTPClient]Submit route request " + url + "\n")
return req.routeRespC
}
func (oc *osrmHTTPClient) submitTableReq(r *table.Request) <-chan TableResponse {
var url string
if !strings.HasPrefix(oc.osrmBackendEndpoint, "http://") {
url += "http://"
}
url = url + oc.osrmBackendEndpoint + r.RequestURI()
req := newOsrmRequest(url, OSRMTable)
oc.requestC <- req
glog.V(3).Infof("[osrmHTTPClient]Submit table request " + url + "\n")
return req.tableRespC
}
func (oc *osrmHTTPClient) start() {
glog.V(0).Info("osrm connector started.\n")
c := make(chan message)
for {
select {
case req := <-oc.requestC:
go oc.send(req, c)
case m := <-c:
go oc.response(&m)
}
}
}
type message struct {
req *request
resp *http.Response
err error
}
func (oc *osrmHTTPClient) send(req *request, c chan<- message) {
resp, err := oc.httpclient.Get(req.url)
glog.V(3).Infof("[osrmHTTPClient] send function succeed with request %s.\n" + req.url)
m := message{req: req, resp: resp, err: err}
c <- m
}
func (oc *osrmHTTPClient) response(m *message) {
defer close(m.req.routeRespC)
defer close(m.req.tableRespC)
var routeResp RouteResponse
var tableResp TableResponse
if m.err != nil || m.resp == nil {
glog.Warningf("osrm request %s failed, err %v\n", m.req.url, m.err)
if m.req.t == OSRMRoute {
routeResp.Err = m.err
m.req.routeRespC <- routeResp
} else if m.req.t == OSRMTable {
tableResp.Err = m.err
m.req.tableRespC <- tableResp
} else {
glog.Fatal("Unsupported request type for osrmHTTPClient.\n")
}
return
}
defer m.resp.Body.Close()
if m.req.t == OSRMRoute {
routeResp.Err = json.NewDecoder(m.resp.Body).Decode(&routeResp.Resp)
m.req.routeRespC <- routeResp
} else if m.req.t == OSRMTable {
tableResp.Err = json.NewDecoder(m.resp.Body).Decode(&tableResp.Resp)
m.req.tableRespC <- tableResp
} else {
glog.Fatal("Unsupported request type for osrmHTTPClient.\n")
}
glog.V(3).Infof("[osrmHTTPClient] Response for request %s" + m.req.url + " is generated.\n")
}