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 8dd97b28c8c906f5f294a6d326441daa80813365
Author: spacewander <[email protected]>
AuthorDate: Wed May 19 17:06:45 2021 +0800

    feat: init cache according to the env
---
 internal/server/server.go      | 25 ++++++++++++++++++++++++-
 internal/server/server_test.go |  1 +
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/internal/server/server.go b/internal/server/server.go
index 8f333fb..26ab5cf 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -21,7 +21,9 @@ import (
        "net"
        "os"
        "os/signal"
+       "strconv"
        "syscall"
+       "time"
 
        "github.com/apache/apisix-go-plugin-runner/internal/log"
        "github.com/apache/apisix-go-plugin-runner/internal/plugin"
@@ -30,7 +32,9 @@ import (
 const (
        HeaderLen   = 4
        MaxDataSize = 2<<24 - 1
-       SockAddrEnv = "APISIX_LISTEN_ADDRESS"
+
+       SockAddrEnv     = "APISIX_LISTEN_ADDRESS"
+       ConfCacheTTLEnv = "APISIX_CONF_EXPIRE_TIME"
 )
 
 const (
@@ -121,6 +125,16 @@ func handleConn(c net.Conn) {
        }
 }
 
+func getConfCacheTTL() time.Duration {
+       ttl := os.Getenv(ConfCacheTTLEnv)
+       n, err := strconv.Atoi(ttl)
+       if err != nil || n <= 0 {
+               log.Errorf("invalid cache ttl: %s", ttl)
+               return 0
+       }
+       return time.Duration(n) * time.Second
+}
+
 func getSockAddr() string {
        path := os.Getenv(SockAddrEnv)
        if path == "" {
@@ -131,6 +145,15 @@ func getSockAddr() string {
 }
 
 func Run() {
+       ttl := getConfCacheTTL()
+       if ttl == 0 {
+               log.Fatalf("A valid conf cache ttl should be set via 
environment variable %s",
+                       ConfCacheTTLEnv)
+       }
+       log.Infof("conf cache ttl is %v", ttl)
+
+       plugin.InitConfCache(ttl)
+
        sockAddr := getSockAddr()
        if sockAddr == "" {
                log.Fatalf("A valid socket address should be set via 
environment variable %s", SockAddrEnv)
diff --git a/internal/server/server_test.go b/internal/server/server_test.go
index c62634b..bd483eb 100644
--- a/internal/server/server_test.go
+++ b/internal/server/server_test.go
@@ -36,6 +36,7 @@ func TestGetSockAddr(t *testing.T) {
 func TestRun(t *testing.T) {
        addr := "/tmp/x.sock"
        os.Setenv(SockAddrEnv, addr)
+       os.Setenv(ConfCacheTTLEnv, "60")
 
        go func() {
                Run()

Reply via email to