diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-12-25 16:08:09 -0800 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-12-25 16:08:09 -0800 |
commit | 0adb1d0dcd57b17444a3364e576b3bcf94266ce4 (patch) | |
tree | 6d3fa06b869eaebf4ad795a7a4f278337bd2c41e | |
parent | dd0bd625475f07306b5176a9695d66a212249191 (diff) |
rest: use []byte instead of interface{} for REST response
Some Marshall method is not thread-safe. They can't be called by
a goroutine for a REST client.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | api/rest.go | 9 | ||||
-rw-r--r-- | server/peer.go | 3 | ||||
-rw-r--r-- | server/server.go | 7 |
3 files changed, 10 insertions, 9 deletions
diff --git a/api/rest.go b/api/rest.go index 91b98e57..a5fdfaa7 100644 --- a/api/rest.go +++ b/api/rest.go @@ -16,7 +16,6 @@ package api import ( - "encoding/json" log "github.com/Sirupsen/logrus" "github.com/gorilla/mux" "net/http" @@ -67,7 +66,7 @@ func NewRestRequest(reqType int, remoteAddr string) *RestRequest { type RestResponse struct { ResponseErr error - Data interface{} + Data []byte } func (r *RestResponse) Err() error { @@ -148,8 +147,7 @@ func (rs *RestServer) neighbor(w http.ResponseWriter, r *http.Request, reqType i } w.Header().Set("Content-Type", "application/json; charset=utf-8") - j, _ := json.Marshal(res.Data) - w.Write(j) + w.Write(res.Data) } func (rs *RestServer) Neighbor(w http.ResponseWriter, r *http.Request) { @@ -174,8 +172,7 @@ func (rs *RestServer) Neighbors(w http.ResponseWriter, r *http.Request) { } w.Header().Set("Content-Type", "application/json; charset=utf-8") - j, _ := json.Marshal(res.Data) - w.Write(j) + w.Write(res.Data) } func NotFoundHandler(w http.ResponseWriter, r *http.Request) { diff --git a/server/peer.go b/server/peer.go index 1d7ec4a8..b3261dbd 100644 --- a/server/peer.go +++ b/server/peer.go @@ -106,7 +106,8 @@ func (peer *Peer) path2update(pathList []table.Path) []*bgp.BGPMessage { func (peer *Peer) handleREST(restReq *api.RestRequest) { result := &api.RestResponse{} - result.Data = peer.rib.Tables[bgp.RF_IPv4_UC] + j, _ := json.Marshal(peer.rib.Tables[bgp.RF_IPv4_UC]) + result.Data = j restReq.ResponseCh <- result close(restReq.ResponseCh) } diff --git a/server/server.go b/server/server.go index ac1e4f81..e97d31c4 100644 --- a/server/server.go +++ b/server/server.go @@ -16,6 +16,7 @@ package server import ( + "encoding/json" "fmt" log "github.com/Sirupsen/logrus" "github.com/osrg/gobgp/api" @@ -157,7 +158,8 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) { for _, peer := range server.peerMap { peerList = append(peerList, peer) } - result.Data = peerList + j, _ := json.Marshal(peerList) + result.Data = j restReq.ResponseCh <- result close(restReq.ResponseCh) @@ -167,7 +169,8 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) { result := &api.RestResponse{} peer, found := server.peerMap[remoteAddr] if found { - result.Data = peer + j, _ := json.Marshal(peer) + result.Data = j } else { result.ResponseErr = fmt.Errorf("Neighbor that has %v does not exist.", remoteAddr) } |