Skip to content

Commit

Permalink
fix(*) make lint happy again
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion committed Jun 9, 2022
1 parent 27a3de1 commit 6a7c834
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 141 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ jobs:
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.29
args: --exclude composites

- name: Test
run: make test
run: make test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

.PHONY: lint
lint:
golint
golangci-lint run --exclude composites

.PHONY: dep
dep:
Expand Down
3 changes: 0 additions & 3 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) {
}

data = make([]byte, len)
if data == nil {
return nil, errors.New("no memory")
}

_, err = io.ReadFull(conn, data)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions bridge/bridgetest/bridgetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bridgetest
import (
"bytes"
"encoding/binary"
"errors"
"io"
"net"
"testing"
Expand All @@ -25,9 +24,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) {
}

data = make([]byte, len)
if data == nil {
return nil, errors.New("no memory")
}

_, err = io.ReadFull(conn, data)
if err != nil {
Expand Down Expand Up @@ -98,7 +94,11 @@ func Mock(t *testing.T, s []MockStep) net.Conn {
break
}
} else {
writePbFrame(conB, []byte{})
err = writePbFrame(conB, []byte{})
if err != nil {
t.Errorf("step %d, writePbFrame(ret): %s", i, err)
break
}
}
}
conB.Close()
Expand Down
2 changes: 0 additions & 2 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (c Ctx) SetShared(k string, value interface{}) error {
}

return c.Ask(`kong.ctx.shared.set`, &kong_plugin_protocol.KV{K: k, V: v}, nil)
return err
}

// kong.Ctx.GetSharedAny() returns a value from the `kong.ctx.shared` request context table.
Expand Down Expand Up @@ -86,4 +85,3 @@ func (c Ctx) GetSharedFloat(k string) (float64, error) {

return 0, bridge.ReturnTypeError("number")
}

13 changes: 10 additions & 3 deletions ip/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import (

func TestIsTrusted(t *testing.T) {
ip := Ip{bridge.New(bridgetest.Mock(t, []bridgetest.MockStep{
{"kong.ip.is_trusted", bridge.WrapString("1.1.1.1"), &kong_plugin_protocol.Bool{V: true}},
{"kong.ip.is_trusted", bridge.WrapString("1.0.0.1"), &kong_plugin_protocol.Bool{V: false}},
{
Method: "kong.ip.is_trusted",
Args: bridge.WrapString("1.1.1.1"),
Ret: &kong_plugin_protocol.Bool{V: true},
},
{
Method: "kong.ip.is_trusted",
Args: bridge.WrapString("1.0.0.1"),
Ret: &kong_plugin_protocol.Bool{V: false},
},
}))}

ret, err := ip.IsTrusted("1.1.1.1")
Expand All @@ -23,4 +31,3 @@ func TestIsTrusted(t *testing.T) {
assert.NoError(t, err)
assert.False(t, ret)
}

11 changes: 6 additions & 5 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func mockNode(t *testing.T, s []bridgetest.MockStep) Node {

func TestGetId(t *testing.T) {
node := mockNode(t, []bridgetest.MockStep{
{"kong.node.get_id", nil, bridge.WrapString("001:002:0003")},
{Method: "kong.node.get_id", Ret: bridge.WrapString("001:002:0003")},
})

ret, err := node.GetId()
Expand All @@ -25,8 +25,9 @@ func TestGetId(t *testing.T) {

func TestGetMemoryStats(t *testing.T) {
node := mockNode(t, []bridgetest.MockStep{
{"kong.node.get_memory_stats", nil,
&kong_plugin_protocol.MemoryStats{
{
Method: "kong.node.get_memory_stats",
Ret: &kong_plugin_protocol.MemoryStats{
LuaSharedDicts: &kong_plugin_protocol.MemoryStats_LuaSharedDicts{
Kong: &kong_plugin_protocol.MemoryStats_LuaSharedDicts_DictStats{
AllocatedSlabs: 1027,
Expand Down Expand Up @@ -68,8 +69,8 @@ func TestGetMemoryStats(t *testing.T) {
}{AllocatedSlabs: 4093, Capacity: 3424875},
},
WorkersLuaVms: []workerLuaVmStats{
workerLuaVmStats{HttpAllocatedGc: 123456, Pid: 543},
workerLuaVmStats{HttpAllocatedGc: 345678, Pid: 876},
{HttpAllocatedGc: 123456, Pid: 543},
{HttpAllocatedGc: 345678, Pid: 876},
},
}, ret)
}
8 changes: 4 additions & 4 deletions request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func mockRequest(t *testing.T, s []bridgetest.MockStep) Request {
func TestGetInfos(t *testing.T) {

q, err := bridge.WrapHeaders(map[string][]string{
"ref": []string{"wayback"},
"trail": []string{"faint"},
"ref": {"wayback"},
"trail": {"faint"},
})
assert.NoError(t, err)

h, err := bridge.WrapHeaders(map[string][]string{
"Host": []string{"example.com"},
"X-Two-Things": []string{"first", "second"},
"Host": {"example.com"},
"X-Two-Things": {"first", "second"},
})
assert.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (r Response) Exit(status int, body string, headers map[string][]string) {
Body: body,
Headers: h,
}
r.Ask(`kong.response.exit`, &arg, nil)
_ = r.Ask(`kong.response.exit`, &arg, nil)
r.Close()
}

Expand All @@ -237,6 +237,6 @@ func (r Response) ExitStatus(status int) {
arg := kong_plugin_protocol.ExitArgs{
Status: int32(status),
}
r.Ask(`kong.response.exit`, &arg, nil)
_ = r.Ask(`kong.response.exit`, &arg, nil)
r.Close()
}
31 changes: 15 additions & 16 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,44 @@ package router
import (
"testing"

"github.com/Kong/go-pdk/entities"
"github.com/Kong/go-pdk/bridge"
"github.com/Kong/go-pdk/bridge/bridgetest"
"github.com/Kong/go-pdk/entities"
"github.com/Kong/go-pdk/server/kong_plugin_protocol"
"github.com/stretchr/testify/assert"
)


func TestRouter(t *testing.T) {
router := Router{bridge.New(bridgetest.Mock(t, []bridgetest.MockStep{
{"kong.router.get_route", nil, &kong_plugin_protocol.Route{
Id: "001:002",
Name: "route_66",
{Method: "kong.router.get_route", Ret: &kong_plugin_protocol.Route{
Id: "001:002",
Name: "route_66",
Protocols: []string{"http", "tcp"},
Paths: []string{"/v0/left", "/v1/this"},
Paths: []string{"/v0/left", "/v1/this"},
}},
{"kong.router.get_service", nil, &kong_plugin_protocol.Service{
Id: "003:004",
Name: "self_service",
{Method: "kong.router.get_service", Ret: &kong_plugin_protocol.Service{
Id: "003:004",
Name: "self_service",
Protocol: "http",
Path: "/v0/left",
Path: "/v0/left",
}},
}))}

ret_r, err := router.GetRoute()
assert.NoError(t, err)
assert.Equal(t, entities.Route{
Id: "001:002",
Name: "route_66",
Id: "001:002",
Name: "route_66",
Protocols: []string{"http", "tcp"},
Paths: []string{"/v0/left", "/v1/this"},
Paths: []string{"/v0/left", "/v1/this"},
}, ret_r)

ret_s, err := router.GetService()
assert.NoError(t, err)
assert.Equal(t, entities.Service{
Id: "003:004",
Name: "self_service",
Id: "003:004",
Name: "self_service",
Protocol: "http",
Path: "/v0/left",
Path: "/v0/left",
}, ret_s)
}
66 changes: 1 addition & 65 deletions server/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"fmt"
"github.com/Kong/go-pdk"
)

// Incoming data for a new event.
Expand All @@ -14,72 +13,9 @@ type StartEventData struct {
}

type eventData struct {
id int // event id
instance *instanceData // plugin instance
ipc chan interface{} // communication channel (TODO: use decoded structs)
pdk *pdk.PDK // go-pdk instance
ipc chan interface{} // communication channel (TODO: use decoded structs)
}

func (rh *rpcHandler) addEvent(event *eventData) {
rh.lock.Lock()
defer rh.lock.Unlock()

event.id = rh.nextEventId
rh.nextEventId++
rh.events[event.id] = event
}

// HandleEvent starts the call/{callback/response}*/finish cycle.
// More than one event can be run concurrenty for a single plugin instance,
// they all receive the same object instance, so should be careful if it's
// mutated or holds references to mutable data.
//
// RPC exported method
// func (rh *rpcHandler) HandleEvent(in StartEventData, out *StepData) error {
// rh.lock.RLock()
// instance, ok := rh.instances[in.InstanceId]
// rh.lock.RUnlock()
// if !ok {
// return fmt.Errorf("no plugin instance %d", in.InstanceId)
// }
//
// h, ok := instance.handlers[in.EventName]
// if !ok {
// return fmt.Errorf("undefined method %s", in.EventName)
// }
//
// ipc := make(chan interface{})
//
// event := eventData{
// instance: instance,
// ipc: ipc,
// pdk: pdk.Init(ipc),
// }
//
// rh.addEvent(&event)
//
// //log.Printf("Will launch goroutine for key %d / operation %s\n", key, op)
// go func() {
// _ = <-ipc
// h(event.pdk)
//
// func() {
// defer func() { recover() }()
// ipc <- "ret"
// }()
//
// rh.lock.Lock()
// defer rh.lock.Unlock()
// event.instance.lastEventTime = time.Now()
// delete(rh.events, event.id)
// }()
//
// ipc <- "run" // kickstart the handler
//
// *out = StepData{EventId: event.id, Data: <-ipc}
// return nil
// }

// A callback's response/request.
type StepData struct {
EventId int // event cycle to which this belongs
Expand Down
18 changes: 6 additions & 12 deletions server/pbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"encoding/binary"
"errors"
"fmt"
"io"
"log"
Expand All @@ -13,14 +12,16 @@ import (
"google.golang.org/protobuf/proto"
)

func servePb(conn net.Conn, rh *rpcHandler) (err error) {
func servePb(conn net.Conn, rh *rpcHandler) {
var err error
var d, rd []byte
for {
d, err := readPbFrame(conn)
d, err = readPbFrame(conn)
if err != nil {
break
}

rd, err := codecPb(rh, conn, d)
rd, err = codecPb(rh, conn, d)
if err != nil {
break
}
Expand All @@ -35,8 +36,6 @@ func servePb(conn net.Conn, rh *rpcHandler) (err error) {
if err != nil {
log.Print(err)
}

return
}

func readPbFrame(conn net.Conn) (data []byte, err error) {
Expand All @@ -47,9 +46,6 @@ func readPbFrame(conn net.Conn) (data []byte, err error) {
}

data = make([]byte, len)
if data == nil {
return nil, errors.New("no memory")
}

_, err = io.ReadFull(conn, data)
if err != nil {
Expand Down Expand Up @@ -177,9 +173,7 @@ func handlePbEvent(rh *rpcHandler, conn net.Conn, e *kong_plugin_protocol.CmdHan
pdk := pdk.Init(conn)

h(pdk)
writePbFrame(conn, []byte{})

return nil
return writePbFrame(conn, []byte{})
}

// Start the embedded plugin server, ProtoBuf version.
Expand Down
2 changes: 0 additions & 2 deletions server/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type rpcHandler struct {
instances map[int]*instanceData
nextInstanceId int
events map[int]*eventData
nextEventId int
lastStartInstance time.Time
lastCloseInstance time.Time
}

Expand Down
Loading

0 comments on commit 6a7c834

Please sign in to comment.