Skip to content

Commit

Permalink
fix: dead lock
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Dec 12, 2024
1 parent e6bf5f1 commit a3a5958
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func AddCommands(rootCmd *cobra.Command) {
Run: func(cmd *cobra.Command, args []string) {
resp, err := client.Blog.Ping(client.Context(), &proto.PingRequest{})
if err != nil {
panic(err)
log.Fatalln(err)
}
fmt.Println(resp.Pong)
},
Expand Down
2 changes: 1 addition & 1 deletion gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *Gateway) register(ctx context.Context, mux *http.ServeMux) error {
// 可跨进程使用。
{
// 扩展功能动态生成的样式、脚本、文件。
mc.Handle(`GET /v3/dynamic/`, http.StripPrefix(`/v3/dynamic`, &dynamic.Handler{}))
mc.Handle(`GET /v3/dynamic/`, http.StripPrefix(`/v3/dynamic`, dynamic.New()))

// 博客功能集
mc.Handle(`GET /v3/features/{theme}`, features.New())
Expand Down
13 changes: 8 additions & 5 deletions modules/utils/dir/dir.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dir

import (
"log"
"os"
"path/filepath"
"runtime"
Expand All @@ -17,7 +16,7 @@ func init() {
panic(err)
}
Root = rootDir
log.Println(`WorkingDir:`, rootDir)
// log.Println(`WorkingDir:`, rootDir)
}

type Dir string
Expand All @@ -27,7 +26,7 @@ func (d Dir) Join(components ...string) string {
}

func SourceRelativeDir() Dir {
s := strings.TrimPrefix(string(SourceAbsoluteDir()), Root)
s := strings.TrimPrefix(abs(), Root)
s = strings.TrimPrefix(s, "/")
if s == "" {
s = "."
Expand All @@ -36,12 +35,16 @@ func SourceRelativeDir() Dir {
}

func SourceAbsoluteDir() Dir {
_, file, _, ok := runtime.Caller(1)
return Dir(abs())
}

func abs() string {
_, file, _, ok := runtime.Caller(2)
if !ok {
panic(`无法获取路径。`)
}
if Root == "" {
panic(`没有设置根目录。`)
}
return Dir(filepath.Dir(file))
return filepath.Dir(file)
}
3 changes: 1 addition & 2 deletions protocols/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"log"
"net"
"net/url"
"time"
Expand Down Expand Up @@ -83,7 +82,7 @@ func _NewConn(home, orGrpcAddress string) *grpc.ClientConn {
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
conn, err := http2tcp.Dial(u.JoinPath(`/v3/grpc`).String(), grpc_proxy.PreSharedKey, ``)
if err != nil {
log.Println(home, err)
// log.Println(home, err)
return nil, err
}
return &_NetConn{conn}, nil
Expand Down
2 changes: 1 addition & 1 deletion service/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ func (s *Service) SetCommentPostID(ctx context.Context, in *proto.SetCommentPost
panic(`不能转移子评论`)
}
// 只是为了判断存在性。
_, err := s.GetPost(ctx, &proto.GetPostRequest{Id: int32(in.PostId)})
_, err := txs.GetPost(ctx, &proto.GetPostRequest{Id: int32(in.PostId)})
if err != nil {
return err
}
Expand Down
21 changes: 20 additions & 1 deletion service/modules/renderers/_dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ var style string
var script string
var files []fs.FS
var mod = time.Now()
var once2 sync.Once

type Handler struct {
func New() http.Handler {
once2.Do(callInits)
return &Handler{}
}

type Handler struct{}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
once.Do(func() {
for name, d := range Dynamic {
Expand Down Expand Up @@ -74,3 +79,17 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

http.NotFound(w, r)
}

var (
inits []func()
)

func RegisterInit(init func()) {
inits = append(inits, init)
}

func callInits() {
for _, init := range inits {
init()
}
}
14 changes: 8 additions & 6 deletions service/modules/renderers/friends/friends.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
var _root embed.FS

func init() {
dynamic.Dynamic[`friends`] = dynamic.Content{
Styles: []string{
string(utils.Must1(_root.ReadFile(`style.css`))),
},
}
sass.WatchDefaultAsync(string(dir.SourceAbsoluteDir()))
dynamic.RegisterInit(func() {
dynamic.Dynamic[`friends`] = dynamic.Content{
Styles: []string{
string(utils.Must1(_root.ReadFile(`style.css`))),
},
}
sass.WatchDefaultAsync(string(dir.SourceAbsoluteDir()))
})
}

type Friends struct {
Expand Down

0 comments on commit a3a5958

Please sign in to comment.