This is an automated email from the ASF dual-hosted git repository.

spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-go-plugin-runner.git

commit d0e3a4e1c0db373cc0488b6e926aead23484d445
Author: spacewander <[email protected]>
AuthorDate: Mon May 17 15:09:58 2021 +0800

    Init commit
---
 cmd/go-runner/main.go          |  21 ++++++
 go.mod                         |   9 +++
 go.sum                         |  54 ++++++++++++++
 internal/log/log.go            |  58 +++++++++++++++
 internal/plugin/conf.go        |  19 +++++
 internal/server/server.go      | 164 +++++++++++++++++++++++++++++++++++++++++
 internal/server/server_test.go |  63 ++++++++++++++++
 7 files changed, 388 insertions(+)

diff --git a/cmd/go-runner/main.go b/cmd/go-runner/main.go
new file mode 100644
index 0000000..e01254e
--- /dev/null
+++ b/cmd/go-runner/main.go
@@ -0,0 +1,21 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package main
+
+import "github.com/apache/apisix-go-plugin-runner/internal/server"
+
+func main() {
+       server.Run()
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..210b8f7
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,9 @@
+module github.com/apache/apisix-go-plugin-runner
+
+go 1.15
+
+require (
+       github.com/stretchr/testify v1.7.0
+       go.uber.org/multierr v1.7.0 // indirect
+       go.uber.org/zap v1.16.0
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..25be7fe
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,54 @@
+github.com/BurntSushi/toml v0.3.1/go.mod 
h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
+github.com/davecgh/go-spew v1.1.0/go.mod 
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 
h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod 
h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/google/renameio v0.1.0/go.mod 
h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
+github.com/kisielk/gotool v1.0.0/go.mod 
h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
+github.com/kr/pretty v0.1.0/go.mod 
h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod 
h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/pkg/errors v0.8.1/go.mod 
h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+github.com/pmezard/go-difflib v1.0.0 
h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod 
h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/rogpeppe/go-internal v1.3.0/go.mod 
h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
+github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
+github.com/stretchr/objx v0.1.0/go.mod 
h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.3.0/go.mod 
h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
+github.com/stretchr/testify v1.4.0/go.mod 
h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+github.com/stretchr/testify v1.7.0 
h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
+github.com/stretchr/testify v1.7.0/go.mod 
h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
+go.uber.org/atomic v1.6.0/go.mod 
h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
+go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
+go.uber.org/atomic v1.7.0/go.mod 
h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
+go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
+go.uber.org/multierr v1.5.0/go.mod 
h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
+go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
+go.uber.org/multierr v1.7.0/go.mod 
h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
+go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod 
h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
+go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
+go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod 
h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod 
h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod 
h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod 
h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod 
h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod 
h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod 
h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod 
h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod 
h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod 
h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod 
h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod 
h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
+golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod 
h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod 
h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod 
h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod 
h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod 
h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b 
h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
+gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod 
h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+honnef.co/go/tools v0.0.1-2019.2.3/go.mod 
h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
diff --git a/internal/log/log.go b/internal/log/log.go
new file mode 100644
index 0000000..4b4c28d
--- /dev/null
+++ b/internal/log/log.go
@@ -0,0 +1,58 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package log
+
+import (
+       "os"
+
+       "go.uber.org/zap"
+       "go.uber.org/zap/zapcore"
+)
+
+var (
+       logger *zap.SugaredLogger
+)
+
+func newLogger() *zap.SugaredLogger {
+       var level = zap.NewAtomicLevel()
+       level.SetLevel(zapcore.InfoLevel)
+
+       core := zapcore.NewCore(
+               zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
+               os.Stdout,
+               level)
+       lg := zap.New(core, zap.AddStacktrace(zap.ErrorLevel), zap.AddCaller())
+       return lg.Sugar()
+}
+
+func init() {
+       logger = newLogger()
+}
+
+func Infof(template string, args ...interface{}) {
+       logger.Infof(template, args...)
+}
+
+func Warnf(template string, args ...interface{}) {
+       logger.Warnf(template, args...)
+}
+
+func Errorf(template string, args ...interface{}) {
+       logger.Errorf(template, args...)
+}
+
+func Fatalf(template string, args ...interface{}) {
+       logger.Fatalf(template, args...)
+}
diff --git a/internal/plugin/conf.go b/internal/plugin/conf.go
new file mode 100644
index 0000000..843ac0b
--- /dev/null
+++ b/internal/plugin/conf.go
@@ -0,0 +1,19 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package plugin
+
+func PrepareConf(buf []byte) []byte {
+       return buf
+}
diff --git a/internal/server/server.go b/internal/server/server.go
new file mode 100644
index 0000000..162a704
--- /dev/null
+++ b/internal/server/server.go
@@ -0,0 +1,164 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package server
+
+import (
+       "encoding/binary"
+       "errors"
+       "io"
+       "net"
+       "os"
+       "os/signal"
+       "syscall"
+
+       "github.com/apache/apisix-go-plugin-runner/internal/log"
+       "github.com/apache/apisix-go-plugin-runner/internal/plugin"
+)
+
+const (
+       HeaderLen   = 4
+       MaxDataSize = 2<<24 - 1
+       SockAddrEnv = "APISIX_LISTEN_ADDRESS"
+)
+
+const (
+       RPCError = iota
+       RPCPrepareConf
+       RPCHTTPReqCall
+)
+
+func readErr(n int, err error) {
+       if err == io.EOF && n != 0 {
+               err = errors.New("truncated")
+       }
+       if err != io.EOF {
+               log.Errorf("read: %s", err)
+       }
+}
+
+func writeErr(n int, err error) {
+       if err != nil {
+               log.Errorf("write: %s", err)
+       }
+}
+
+func handleConn(c net.Conn) {
+       log.Infof("Client connected (%s)", c.RemoteAddr().Network())
+       defer c.Close()
+
+       header := make([]byte, HeaderLen)
+       for {
+               n, err := c.Read(header)
+               if err != nil {
+                       readErr(n, err)
+                       break
+               }
+
+               ty := header[0]
+               // we only use last 3 bytes to store the length, so the first 
byte is
+               // consider zero
+               header[0] = 0
+               length := binary.BigEndian.Uint32(header)
+
+               log.Infof("receive rpc type: %d data length: %d", ty, length)
+
+               buf := make([]byte, length)
+               n, err = c.Read(buf)
+               if err != nil {
+                       readErr(n, err)
+                       break
+               }
+
+               var out []byte
+               switch ty {
+               case RPCPrepareConf:
+                       out = plugin.PrepareConf(buf)
+               }
+
+               size := len(out)
+               if size > MaxDataSize {
+                       log.Errorf("the max length of data is %d but got %d", 
MaxDataSize, size)
+                       continue
+               }
+
+               binary.BigEndian.PutUint32(header, uint32(size))
+               header[0] = ty
+
+               n, err = c.Write(header)
+               if err != nil {
+                       writeErr(n, err)
+                       break
+               }
+
+               n, err = c.Write(out)
+               if err != nil {
+                       writeErr(n, err)
+                       break
+               }
+       }
+}
+
+func getSockAddr() string {
+       path := os.Getenv(SockAddrEnv)
+       if path == "" {
+               log.Errorf("invalid socket address: %s", path)
+               return ""
+       }
+       return path
+}
+
+func Run() {
+       sockAddr := getSockAddr()
+       if sockAddr == "" {
+               log.Fatalf("A valid socket address should be set via 
environment variable %s", SockAddrEnv)
+       }
+       log.Infof("listening to %s", sockAddr)
+
+       if err := os.RemoveAll(sockAddr); err != nil {
+               log.Fatalf("remove file %s: %s", sockAddr, err)
+       }
+
+       l, err := net.Listen("unix", sockAddr)
+       if err != nil {
+               log.Fatalf("listen %s: %s", sockAddr, err)
+       }
+       defer l.Close()
+
+       done := make(chan struct{})
+       quit := make(chan os.Signal, 1)
+       signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
+
+       go func() {
+               for {
+                       select {
+                       case <-done:
+                               return
+                       default:
+                       }
+
+                       conn, err := l.Accept()
+                       if err != nil {
+                               log.Errorf("accept: %s", err)
+                               continue
+                       }
+
+                       go handleConn(conn)
+               }
+       }()
+
+       sig := <-quit
+       log.Infof("server receive %s and exit", sig.String())
+       close(done)
+}
diff --git a/internal/server/server_test.go b/internal/server/server_test.go
new file mode 100644
index 0000000..81529bf
--- /dev/null
+++ b/internal/server/server_test.go
@@ -0,0 +1,63 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package server
+
+import (
+       "encoding/binary"
+       "net"
+       "os"
+       "testing"
+       "time"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestGetSockAddr(t *testing.T) {
+       os.Unsetenv(SockAddrEnv)
+       assert.Equal(t, "", getSockAddr())
+
+       os.Setenv(SockAddrEnv, "/tmp/x.sock")
+       assert.Equal(t, "/tmp/x.sock", getSockAddr())
+}
+
+func TestRun(t *testing.T) {
+       addr := "/tmp/x.sock"
+       os.Setenv(SockAddrEnv, addr)
+
+       go func() {
+               Run()
+       }()
+
+       time.Sleep(100 * time.Millisecond)
+       header := make([]byte, 4)
+       binary.BigEndian.PutUint32(header, uint32(32))
+       header[0] = 1
+       cases := []struct {
+               header []byte
+       }{
+               // dad header
+               {[]byte("a")},
+               // header without body
+               {header},
+               // header without body truncated
+               {append(header, 32)},
+       }
+
+       for _, c := range cases {
+               conn, err := net.DialTimeout("unix", addr, 1*time.Second)
+               assert.NotNil(t, conn, err)
+               conn.Write(c.header)
+       }
+}

Reply via email to