From b73eba8feacaaa7504d9926f20a7893f98c7b78a Mon Sep 17 00:00:00 2001 From: smallnest Date: Tue, 31 Oct 2023 22:59:46 +0800 Subject: [PATCH] add CacheDiscovery --- client/discovery.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/client/discovery.go b/client/discovery.go index 800d4ac0..7e4680d9 100644 --- a/client/discovery.go +++ b/client/discovery.go @@ -3,6 +3,7 @@ package client import ( "encoding/json" "os" + "path/filepath" "sync" ) @@ -39,6 +40,15 @@ func CacheDiscovery(threshold int, cachedFile string, discovery ServiceDiscovery cachedFile = ".cache/discovery.json" } + cachedFileDir := filepath.Dir(cachedFile) + + if _, err := os.Stat(cachedFileDir); os.IsNotExist(err) { + // 目录不存在,创建目录 + if err := os.MkdirAll(cachedFileDir, os.ModePerm); err != nil { + panic(err) + } + } + return &cachedServiceDiscovery{ threshold: threshold, cachedFile: cachedFile,