Skip to content

Commit

Permalink
fix(instance-id): use sequence number as instance id
Browse files Browse the repository at this point in the history
This patch changes how the instance ID is calculated; rather than
generating an instance ID solely on the plugin server side, the plugin
sequence number passed in by Kong is used. If one is not found in the
request, a random ID is generated.
  • Loading branch information
gszr committed Aug 29, 2023
1 parent 4deec45 commit 31875d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
30 changes: 26 additions & 4 deletions server/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import (
"github.com/Kong/go-pdk"
"log"
"time"
"math/rand"
)

type configMetadata struct {
Seq int `json:"__seq__"`
}

type instanceData struct {
id int
startTime time.Time
config interface{}
configMeta configMetadata
handlers map[string]func(*pdk.PDK)
lastEventTime time.Time
}
Expand Down Expand Up @@ -48,8 +54,19 @@ func (rh *rpcHandler) addInstance(instance *instanceData) {
rh.lock.Lock()
defer rh.lock.Unlock()

instance.id = rh.nextInstanceId
rh.nextInstanceId++
seq := instance.configMeta.Seq

var id int
if seq != 0 {
id = seq // if kong signaled a plugin seq number, use it
} else {
id = int(rand.Int31()) // otherwise assign a random id
for _, exists := rh.instances[id]; exists; { // handle possible collision
id = int(rand.Int31())
}
}
instance.id = id

rh.instances[instance.id] = instance
}

Expand All @@ -66,18 +83,23 @@ type InstanceStatus struct {
// a new instance should be started and the old one closed.
//
// RPC exported method
func (rh *rpcHandler) StartInstance(config PluginConfig, status *InstanceStatus) error {
func (rh *rpcHandler) StartInstance(seq int, config PluginConfig, status *InstanceStatus) error {
// TODO: check if config.Name is the one we care

instanceConfig := rh.constructor()
instanceMeta := configMetadata{}
if err := json.Unmarshal(config.Config, &instanceMeta); err != nil {
return fmt.Errorf("decoding config metadata: %w", err)
}

instanceConfig := rh.constructor()
if err := json.Unmarshal(config.Config, instanceConfig); err != nil {
return fmt.Errorf("decoding config: %w", err)
}

instance := instanceData{
startTime: time.Now(),
config: instanceConfig,
configMeta: instanceMeta,
handlers: getHandlers(instanceConfig),
}

Expand Down
3 changes: 1 addition & 2 deletions server/pbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func handlePbCmd(rh *rpcHandler, conn net.Conn, m *kong_plugin_protocol.RpcCall)
Config: c.CmdStartInstance.Config,
}
var status InstanceStatus
err = rh.StartInstance(config, &status)
err = rh.StartInstance(int(c.CmdStartInstance.Seq), config, &status)
if err != nil {
return
}
Expand Down Expand Up @@ -138,7 +138,6 @@ func handlePbCmd(rh *rpcHandler, conn net.Conn, m *kong_plugin_protocol.RpcCall)
if err != nil {
return
}

rm = &kong_plugin_protocol.RpcReturn{
Sequence: m.Sequence,
Return: pbInstanceStatus(status),
Expand Down
1 change: 0 additions & 1 deletion server/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type rpcHandler struct {
priority int // priority info
lock sync.RWMutex
instances map[int]*instanceData
nextInstanceId int
events map[int]*eventData
lastCloseInstance time.Time
}
Expand Down

0 comments on commit 31875d6

Please sign in to comment.