Some fixes suggested by golangci-lint
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
// ListRoutes queries the kapow! instance for the routes that are registered
|
// ListRoutes queries the kapow! instance for the routes that are registered
|
||||||
func ListRoutes(host string, w io.Writer) error {
|
func ListRoutes(host string, w io.Writer) error {
|
||||||
|
|
||||||
url := host + "/routes/"
|
url := host + "/routes/"
|
||||||
return http.Get(url, "", nil, w)
|
return http.Get(url, "", nil, w)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
// RemoveRoute removes a registered route in Kapow! server
|
// RemoveRoute removes a registered route in Kapow! server
|
||||||
func RemoveRoute(host, id string) error {
|
func RemoveRoute(host, id string) error {
|
||||||
|
|
||||||
url := host + "/routes/" + id
|
url := host + "/routes/" + id
|
||||||
return http.Delete(url, "", nil, nil)
|
return http.Delete(url, "", nil, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func SetData(host, handlerID, path string, r io.Reader) error {
|
func SetData(host, handlerID, path string, r io.Reader) error {
|
||||||
|
|
||||||
url := host + "/handlers/" + handlerID + path
|
url := host + "/handlers/" + handlerID + path
|
||||||
return http.Put(url, "", r, nil)
|
return http.Put(url, "", r, nil)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -12,8 +12,8 @@ import (
|
|||||||
// GetCmd is the command line interface for get kapow data operation
|
// GetCmd is the command line interface for get kapow data operation
|
||||||
var GetCmd = &cobra.Command{
|
var GetCmd = &cobra.Command{
|
||||||
Use: "get [flags] resource",
|
Use: "get [flags] resource",
|
||||||
Short: "Retrive a Kapow! resource",
|
Short: "Retrieve a Kapow! resource",
|
||||||
Long: "Retrive a Kapow! resource for the current request",
|
Long: "Retrieve a Kapow! resource for the current request",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
PreRunE: handlerIDRequired,
|
PreRunE: handlerIDRequired,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|||||||
@@ -21,5 +21,4 @@ func handlerIDRequired(cmd *cobra.Command, args []string) error {
|
|||||||
return errors.New("--handler or KAPOW_HANDLER_ID is mandatory")
|
return errors.New("--handler or KAPOW_HANDLER_ID is mandatory")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type safeHandlerMap struct {
|
type safeHandlerMap struct {
|
||||||
hs map[string]*model.Handler
|
hs map[string]*model.Handler
|
||||||
m sync.RWMutex
|
m *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var Handlers = New()
|
var Handlers = New()
|
||||||
@@ -16,13 +16,13 @@ var Handlers = New()
|
|||||||
func New() safeHandlerMap {
|
func New() safeHandlerMap {
|
||||||
return safeHandlerMap{
|
return safeHandlerMap{
|
||||||
hs: make(map[string]*model.Handler),
|
hs: make(map[string]*model.Handler),
|
||||||
m: sync.RWMutex{},
|
m: &sync.RWMutex{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (shm *safeHandlerMap) Add(h *model.Handler) {
|
func (shm *safeHandlerMap) Add(h *model.Handler) {
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
shm.hs[h.Id] = h
|
shm.hs[h.ID] = h
|
||||||
shm.m.Unlock()
|
shm.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ func TestPackageHaveASingletonEmptyHandlersList(t *testing.T) {
|
|||||||
func TestAddAddsANewHandlerToTheMap(t *testing.T) {
|
func TestAddAddsANewHandlerToTheMap(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
|
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
if _, ok := shm.hs["FOO"]; !ok {
|
if _, ok := shm.hs["FOO"]; !ok {
|
||||||
t.Error("Handler not added to the map")
|
t.Error("Handler not added to the map")
|
||||||
@@ -39,12 +39,12 @@ func TestAddAdquiresMutexBeforeAdding(t *testing.T) {
|
|||||||
|
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
defer shm.m.Unlock()
|
defer shm.m.Unlock()
|
||||||
go shm.Add(&model.Handler{Id: "FOO"})
|
go shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
|
||||||
if _, ok := shm.hs["FOO"]; ok {
|
if _, ok := shm.hs["FOO"]; ok {
|
||||||
t.Error("Handler added while mutex was adquired")
|
t.Error("Handler added while mutex was acquired")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ func TestAddAddsHandlerAfterMutexIsReleased(t *testing.T) {
|
|||||||
shm := New()
|
shm := New()
|
||||||
|
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
go shm.Add(&model.Handler{Id: "FOO"})
|
go shm.Add(&model.Handler{ID: "FOO"})
|
||||||
shm.m.Unlock()
|
shm.m.Unlock()
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
@@ -64,7 +64,7 @@ func TestAddAddsHandlerAfterMutexIsReleased(t *testing.T) {
|
|||||||
|
|
||||||
func TestRemoveRemovesAHandlerFromTheMap(t *testing.T) {
|
func TestRemoveRemovesAHandlerFromTheMap(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
shm.Remove("FOO")
|
shm.Remove("FOO")
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ func TestRemoveRemovesAHandlerFromTheMap(t *testing.T) {
|
|||||||
|
|
||||||
func TestRemoveAdquiresMutexBeforeRemoving(t *testing.T) {
|
func TestRemoveAdquiresMutexBeforeRemoving(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
defer shm.m.Unlock()
|
defer shm.m.Unlock()
|
||||||
@@ -85,13 +85,13 @@ func TestRemoveAdquiresMutexBeforeRemoving(t *testing.T) {
|
|||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
|
||||||
if _, ok := shm.hs["FOO"]; !ok {
|
if _, ok := shm.hs["FOO"]; !ok {
|
||||||
t.Error("Handler was remove while mutex was adquired")
|
t.Error("Handler was remove while mutex was acquired")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveRemovesHandlerAfterMutexIsReleased(t *testing.T) {
|
func TestRemoveRemovesHandlerAfterMutexIsReleased(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
go shm.Remove("FOO")
|
go shm.Remove("FOO")
|
||||||
@@ -110,12 +110,11 @@ func TestGetReturnFalseWhenHandlerDoesNotExist(t *testing.T) {
|
|||||||
if _, exists := shm.Get("FOO"); exists {
|
if _, exists := shm.Get("FOO"); exists {
|
||||||
t.Error("Get should return false when handler does not exist")
|
t.Error("Get should return false when handler does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReturnTrueWhenHandlerExists(t *testing.T) {
|
func TestGetReturnTrueWhenHandlerExists(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
if _, exists := shm.Get("FOO"); !exists {
|
if _, exists := shm.Get("FOO"); !exists {
|
||||||
t.Error("Get should return true when handler do exist")
|
t.Error("Get should return true when handler do exist")
|
||||||
@@ -124,7 +123,7 @@ func TestGetReturnTrueWhenHandlerExists(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetReturnExistingHandler(t *testing.T) {
|
func TestGetReturnExistingHandler(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
expected := &model.Handler{Id: "FOO"}
|
expected := &model.Handler{ID: "FOO"}
|
||||||
shm.Add(expected)
|
shm.Add(expected)
|
||||||
|
|
||||||
if current, _ := shm.Get("FOO"); current != expected {
|
if current, _ := shm.Get("FOO"); current != expected {
|
||||||
@@ -134,7 +133,7 @@ func TestGetReturnExistingHandler(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetWaitsForTheWriterToFinish(t *testing.T) {
|
func TestGetWaitsForTheWriterToFinish(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
shm.m.Lock()
|
shm.m.Lock()
|
||||||
defer shm.m.Unlock()
|
defer shm.m.Unlock()
|
||||||
@@ -146,14 +145,14 @@ func TestGetWaitsForTheWriterToFinish(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-c:
|
case <-c:
|
||||||
t.Error("Handler readed while mutex was adquired")
|
t.Error("Handler readed while mutex was acquired")
|
||||||
default: // This default prevents the select from being blocking
|
default: // This default prevents the select from being blocking
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNonBlockingReadWithOtherReaders(t *testing.T) {
|
func TestGetNonBlockingReadWithOtherReaders(t *testing.T) {
|
||||||
shm := New()
|
shm := New()
|
||||||
shm.Add(&model.Handler{Id: "FOO"})
|
shm.Add(&model.Handler{ID: "FOO"})
|
||||||
|
|
||||||
shm.m.RLock()
|
shm.m.RLock()
|
||||||
defer shm.m.RUnlock()
|
defer shm.m.RUnlock()
|
||||||
@@ -166,6 +165,6 @@ func TestGetNonBlockingReadWithOtherReaders(t *testing.T) {
|
|||||||
select {
|
select {
|
||||||
case <-c:
|
case <-c:
|
||||||
default: // This default prevents the select from being blocking
|
default: // This default prevents the select from being blocking
|
||||||
t.Error("Handler couldn't read while mutex was adquired for read")
|
t.Error("Handler couldn't read while mutex was acquired for read")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,25 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Handler represents an open HTTP connection in the User Server.
|
||||||
|
//
|
||||||
|
// This struct contains the connection Writer and Request to be managed
|
||||||
|
// by endpoints of the Data Server.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
Id string
|
// ID is unique identifier of the request.
|
||||||
Route *Route
|
ID string
|
||||||
|
|
||||||
|
// Route is a reference to the original route that matched this
|
||||||
|
// request.
|
||||||
|
Route *Route
|
||||||
|
|
||||||
|
// Writing is a mutex that prevents two goroutines from writing at
|
||||||
|
// the same time in the response.
|
||||||
Writing sync.Mutex
|
Writing sync.Mutex
|
||||||
Writer http.ResponseWriter
|
|
||||||
|
// Request is a pointer to the in-progress request.
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
|
|
||||||
|
// Writer is the original http.ResponseWriter of the request.
|
||||||
|
Writer http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
// Route contains the data needed to represent a Kapow! user route.
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Id string `json:"id"`
|
// ID is the unique identifier of the Route.
|
||||||
Method string `json:"method"`
|
ID string `json:"id"`
|
||||||
Pattern string `json:"url_pattern"`
|
|
||||||
|
// Method is the HTTP method that will match this Route.
|
||||||
|
Method string `json:"method"`
|
||||||
|
|
||||||
|
// Pattern is the gorilla/mux path pattern that will match this
|
||||||
|
// Route.
|
||||||
|
Pattern string `json:"url_pattern"`
|
||||||
|
|
||||||
|
// Entrypoint is the string that will be executed when the Route
|
||||||
|
// match.
|
||||||
|
//
|
||||||
|
// This string will be split according to the shell parsing rules to
|
||||||
|
// be passed as a list to exec.Command.
|
||||||
Entrypoint string `json:"entrypoint"`
|
Entrypoint string `json:"entrypoint"`
|
||||||
Command string `json:"command"`
|
|
||||||
|
// Command is the last argument to be passed to exec.Command when
|
||||||
|
// executing the Entrypoint
|
||||||
|
Command string `json:"command"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,16 @@ import (
|
|||||||
|
|
||||||
type safeRouteList struct {
|
type safeRouteList struct {
|
||||||
rs []model.Route
|
rs []model.Route
|
||||||
m sync.RWMutex
|
m *sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
var Routes = New()
|
var Routes = New()
|
||||||
|
|
||||||
func New() safeRouteList {
|
func New() safeRouteList {
|
||||||
return safeRouteList{}
|
return safeRouteList{
|
||||||
|
rs: []model.Route{},
|
||||||
|
m: &sync.RWMutex{},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srl *safeRouteList) Append(r model.Route) {
|
func (srl *safeRouteList) Append(r model.Route) {
|
||||||
@@ -27,11 +30,7 @@ func (srl *safeRouteList) Snapshot() []model.Route {
|
|||||||
srl.m.RLock()
|
srl.m.RLock()
|
||||||
defer srl.m.RUnlock()
|
defer srl.m.RUnlock()
|
||||||
|
|
||||||
if srl.rs == nil {
|
rs := make([]model.Route, len(srl.rs))
|
||||||
return nil
|
copy(rs, srl.rs)
|
||||||
} else {
|
return rs
|
||||||
rs := make([]model.Route, len(srl.rs))
|
|
||||||
copy(rs, srl.rs)
|
|
||||||
return rs
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,11 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/BBVA/kapow/internal/server/model"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/BBVA/kapow/internal/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewReturnAnEmptyStruct(t *testing.T) {
|
func TestNewReturnAnEmptyStruct(t *testing.T) {
|
||||||
@@ -43,7 +44,7 @@ func TestAppendAdquiresMutexBeforeAdding(t *testing.T) {
|
|||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
|
||||||
if len(srl.rs) != 0 {
|
if len(srl.rs) != 0 {
|
||||||
t.Error("Route added while mutex was adquired")
|
t.Error("Route added while mutex was acquired")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ func TestAppendAddsRouteAfterMutexIsReleased(t *testing.T) {
|
|||||||
|
|
||||||
func TestSnapshotReturnTheCurrentListOfRoutes(t *testing.T) {
|
func TestSnapshotReturnTheCurrentListOfRoutes(t *testing.T) {
|
||||||
srl := New()
|
srl := New()
|
||||||
srl.Append(model.Route{Id: "FOO"})
|
srl.Append(model.Route{ID: "FOO"})
|
||||||
|
|
||||||
rs := srl.Snapshot()
|
rs := srl.Snapshot()
|
||||||
|
|
||||||
@@ -72,19 +73,30 @@ func TestSnapshotReturnTheCurrentListOfRoutes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSnapshotReturnADeepCopyOfTheListWhenIsNil(t *testing.T) {
|
||||||
|
srl := New()
|
||||||
|
srl.rs = nil
|
||||||
|
|
||||||
|
rs := srl.Snapshot()
|
||||||
|
|
||||||
|
if len(rs) != 0 {
|
||||||
|
t.Fatal("Route list copy is not empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSnapshotReturnADeepCopyOfTheListWhenEmpty(t *testing.T) {
|
func TestSnapshotReturnADeepCopyOfTheListWhenEmpty(t *testing.T) {
|
||||||
srl := New()
|
srl := New()
|
||||||
|
|
||||||
rs := srl.Snapshot()
|
rs := srl.Snapshot()
|
||||||
|
|
||||||
if rs != nil {
|
if len(rs) != 0 {
|
||||||
t.Fatal("Route list copy is not empty")
|
t.Fatal("Route list copy is not empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotReturnADeepCopyOfTheListWhenNonEmpty(t *testing.T) {
|
func TestSnapshotReturnADeepCopyOfTheListWhenNonEmpty(t *testing.T) {
|
||||||
srl := New()
|
srl := New()
|
||||||
srl.Append(model.Route{Id: "FOO"})
|
srl.Append(model.Route{ID: "FOO"})
|
||||||
|
|
||||||
rs := srl.Snapshot()
|
rs := srl.Snapshot()
|
||||||
|
|
||||||
@@ -101,7 +113,7 @@ func TestSnapshotReturnADeepCopyOfTheListWhenNonEmpty(t *testing.T) {
|
|||||||
|
|
||||||
func TestSnapshotWaitsForTheWriterToFinish(t *testing.T) {
|
func TestSnapshotWaitsForTheWriterToFinish(t *testing.T) {
|
||||||
srl := New()
|
srl := New()
|
||||||
srl.Append(model.Route{Id: "FOO"})
|
srl.Append(model.Route{ID: "FOO"})
|
||||||
|
|
||||||
srl.m.Lock()
|
srl.m.Lock()
|
||||||
defer srl.m.Unlock()
|
defer srl.m.Unlock()
|
||||||
@@ -113,14 +125,14 @@ func TestSnapshotWaitsForTheWriterToFinish(t *testing.T) {
|
|||||||
|
|
||||||
select {
|
select {
|
||||||
case <-c:
|
case <-c:
|
||||||
t.Error("Route list readed while mutex was adquired")
|
t.Error("Route list readed while mutex was acquired")
|
||||||
default: // This default prevents the select from being blocking
|
default: // This default prevents the select from being blocking
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotNonBlockingReadWithOtherReaders(t *testing.T) {
|
func TestSnapshotNonBlockingReadWithOtherReaders(t *testing.T) {
|
||||||
srl := New()
|
srl := New()
|
||||||
srl.Append(model.Route{Id: "FOO"})
|
srl.Append(model.Route{ID: "FOO"})
|
||||||
|
|
||||||
srl.m.RLock()
|
srl.m.RLock()
|
||||||
defer srl.m.RUnlock()
|
defer srl.m.RUnlock()
|
||||||
@@ -133,6 +145,6 @@ func TestSnapshotNonBlockingReadWithOtherReaders(t *testing.T) {
|
|||||||
select {
|
select {
|
||||||
case <-c:
|
case <-c:
|
||||||
default: // This default prevents the select from being blocking
|
default: // This default prevents the select from being blocking
|
||||||
t.Error("Route list couldn't be readed while mutex was adquired for read")
|
t.Error("Route list couldn't be readed while mutex was acquired for read")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user