Skip to content

Commit

Permalink
change code
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt committed Oct 9, 2024
1 parent ccc4dd9 commit 4b6e9c9
Show file tree
Hide file tree
Showing 6 changed files with 434 additions and 95 deletions.
32 changes: 20 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func main() {
// app implements the main component, the entry point to a Service Weaver app.
type app struct {
weaver.Implements[weaver.Main]
fileSub weaver.Ref[subFileSourceProvider]
urlSub weaver.Ref[subURLSourceProvider]
lis weaver.Listener `weaver:"lis"`
configure weaver.Ref[subConfigureProvider]
fileSub weaver.Ref[subFileSourceProvider]
urlSub weaver.Ref[subURLSourceProvider]
lis weaver.Listener `weaver:"lis"`
}

// serve serves HTTP traffic.
Expand All @@ -40,21 +41,28 @@ func serve(ctx context.Context, app *app) error {
}

func subShareHandlerApp(app *app) func(w http.ResponseWriter, _ *http.Request) {
fileSub, err := app.fileSub.Get().UpdateFileSub(context.Background())
if err != nil {
app.Logger(context.Background()).Error("failed to get file sub update", "error", err)
}
urlSub, err := app.urlSub.Get().UpdateUrlSub(context.Background())
if err != nil {
app.Logger(context.Background()).Error("failed to get url sub update", "error", err)
}
return func(w http.ResponseWriter, _ *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
encoder := base64.NewEncoder(base64.StdEncoding, w)
defer encoder.Close()

buf := bufPool.Get()
defer bufPool.Free(buf)

privateToken := r.URL.Query().Get("token")

subFilePaths, _ := app.configure.Get().GetSubFilePaths(context.Background(), privateToken)
urlSubPaths, timeout, _ := app.configure.Get().GetUrlSubs(context.Background(), privateToken)

fileSub, err := app.fileSub.Get().UpdateFileSub(context.Background(), subFilePaths)
if err != nil {
app.Logger(context.Background()).Error("failed to get file sub update", "error", err)
}

urlSub, err := app.urlSub.Get().UpdateUrlSub(context.Background(), urlSubPaths, timeout)
if err != nil {
app.Logger(context.Background()).Error("failed to get url sub update", "error", err)
}

if len(fileSub) > 0 {
if _, err = buf.Write(fileSub); err != nil {
app.Logger(context.Background()).Error("failed to write file sub to buffer", "error", err)
Expand Down
46 changes: 46 additions & 0 deletions sub_configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"context"
"github.com/ServiceWeaver/weaver"
)

var _ subConfigureProvider = (*subConfigure)(nil)

type subConfigureProvider interface {
GetSubFilePaths(ctx context.Context, privateSubToken string) ([]string, error)
GetUrlSubs(ctx context.Context, privateSubToken string) ([]string, int, error)
}

type subConfig struct {
weaver.AutoMarshal
PublicSubFilePaths []string `toml:"public_sub_file_paths"`
PrivateSubFilePaths []string `toml:"private_sub_file_paths"`
UrlSubFetchTimeoutSeconds int `toml:"url_sub_fetch_timeout_seconds"`
PublicUrlSubs []string `toml:"public_url_subs"`
PrivateUrlSubs []string `toml:"private_url_subs"`

PrivateSubToken string `toml:"private_sub_token"`
}

type subConfigure struct {
weaver.Implements[subConfigureProvider]
weaver.WithConfig[subConfig]
}

func (s *subConfigure) GetSubFilePaths(ctx context.Context, privateSubToken string) ([]string, error) {
config := s.Config()
if privateSubToken != config.PrivateSubToken {
return config.PublicSubFilePaths, nil
}
return append(config.PrivateSubFilePaths, config.PublicSubFilePaths...), nil
}

func (s *subConfigure) GetUrlSubs(ctx context.Context, privateSubToken string) ([]string, int, error) {
config := s.Config()
if privateSubToken != config.PrivateSubToken {
s.Logger(ctx).Info("token check pass")
return config.PublicUrlSubs, config.UrlSubFetchTimeoutSeconds, nil
}
return append(config.PrivateUrlSubs, config.PublicUrlSubs...), config.UrlSubFetchTimeoutSeconds, nil
}
14 changes: 4 additions & 10 deletions sub_file_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,22 @@ var (
fileBuf = tinypool.New(tinypool.BufReset)
)

type subFileSourceConfig struct {
weaver.AutoMarshal
FilePaths []string `toml:"file_paths"`
}

type subFileSourceProvider interface {
UpdateFileSub(ctx context.Context) ([]byte, error)
UpdateFileSub(ctx context.Context, subFilePaths []string) ([]byte, error)
}

type subFileSource struct {
weaver.Implements[subFileSourceProvider]
weaver.WithConfig[subFileSourceConfig]
}

func (s *subFileSource) UpdateFileSub(ctx context.Context) ([]byte, error) {
if len(s.Config().FilePaths) == 0 {
func (s *subFileSource) UpdateFileSub(ctx context.Context, subFilePaths []string) ([]byte, error) {
if len(subFilePaths) == 0 {
s.Logger(ctx).Warn("skip file source,for dir was not config")
return nil, subFileSourceDirNotConfiguredError
}
buf := fileBuf.Get()
defer fileBuf.Free(buf)
for _, path := range s.Config().FilePaths {
for _, path := range subFilePaths {
if content, found := fileSubLru.Get(path); found {
if _, err := buf.Write(content); err != nil {
s.Logger(context.Background()).Error("write buffered content to buf", "path", path, "err", err)
Expand Down
18 changes: 5 additions & 13 deletions sub_url_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,28 @@ var (
urlBuf = tinypool.New(tinypool.BufReset)
)

type subUrlSourceConfig struct {
weaver.AutoMarshal
TimeoutSeconds int `toml:"timeout_seconds"`
UrlSubs []string `toml:"url_subs"`
}

type subURLSourceProvider interface {
UpdateUrlSub(ctx context.Context) ([]byte, error)
UpdateUrlSub(ctx context.Context, urlSubs []string, fetchTimeoutSeconds int) ([]byte, error)
}

type subURLSource struct {
weaver.Implements[subURLSourceProvider]
weaver.WithConfig[subUrlSourceConfig]
}

func (s *subURLSource) UpdateUrlSub(ctx context.Context) ([]byte, error) {
config := s.Config()
func (s *subURLSource) UpdateUrlSub(ctx context.Context, urlSubs []string, fetchTimeoutSeconds int) ([]byte, error) {

buf := urlBuf.Get()
defer urlBuf.Free(buf)

st := stream.New().WithMaxGoroutines(len(config.UrlSubs))
for _, sub := range config.UrlSubs {
st := stream.New().WithMaxGoroutines(len(urlSubs))
for _, sub := range urlSubs {
sub := sub
if subContent, found := urlSubLru.Get(sub); found {
buf.Write(subContent)
continue
}
st.Go(func() stream.Callback {
client := fetcher.NewClient(config.TimeoutSeconds)
client := fetcher.NewClient(fetchTimeoutSeconds)
content, err := client.Fetch(sub)
if err != nil {
s.Logger(ctx).Error("failed to fetch url sub from source", "url", sub, "error", err)
Expand Down
27 changes: 18 additions & 9 deletions weaver.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ binary = "raycat"
# `weaver single deploy`. See serviceweaver.dev/docs.html#single-process for
# more information about the single process deployer.

["raycat/subFileSourceProvider"]
file_paths = [
["raycat/subConfigureProvider"]
public_sub_file_paths = [
"sss",
"yyyy",
"ddd"
]

["raycat/subURLSourceProvider"]
timeout_seconds = 2
url_subs = [
"ssss",
"zzzz"
private_sub_file_paths = [
"sss1",
"ddd1"
]
url_sub_fetch_timeout_seconds = 2
public_url_subs = [
"url1",
"url2"
]
private_url_subs = [
"url3",
"url4"
]
private_sub_token = "gopher"




[single]
Expand Down
Loading

0 comments on commit 4b6e9c9

Please sign in to comment.