Skip to content

Commit

Permalink
refactor kserver
Browse files Browse the repository at this point in the history
  • Loading branch information
day253 committed Oct 19, 2024
1 parent 03b6842 commit 6391a3a
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 132 deletions.
5 changes: 2 additions & 3 deletions boilerplate/cmd/press/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"github.com/bytedance/gopkg/util/gopool"
json "github.com/bytedance/sonic"
"github.com/cloudwego/kitex/pkg/klog"
_ "github.com/ishumei/krpc/autolimit"
"github.com/ishumei/krpc/conf"
"github.com/ishumei/krpc/kclient"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/objects"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service"
registry_zookeeper "github.com/ishumei/krpc/registry-zookeeper"
Expand All @@ -34,7 +33,7 @@ func main() {
klog.Info(f.Name, ": ", f.Value)
})
client := kclient.MustNewArbiterClient(func() *kclient.SingleClientConf {
c := &sconfig.FrameConfig{}
c := &kserver.FrameConfig{}
err := conf.LoadDefaultConf(c, "frame", "overwrite.yaml")
klog.Info(objects.String(c))
return &kclient.SingleClientConf{
Expand Down
5 changes: 2 additions & 3 deletions boilerplate/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package main
import (
"flag"

_ "github.com/ishumei/krpc/autolimit"
"github.com/ishumei/krpc/boilerplate/pkg/handler"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/arbiter"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/samber/do"
)

func main() {
flag.Parse()
handler.BackgroundTask()
injector := sconfig.Injector
injector := kserver.Injector
arbiterService := do.MustInvoke[*arbiter.ArbiterService](injector)
defer func() { _ = arbiterService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
Expand Down
9 changes: 4 additions & 5 deletions boilerplate/pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
json "github.com/bytedance/sonic"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/ishumei/krpc/kclient"
_ "github.com/ishumei/krpc/kserver/governance"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/logging"
"github.com/ishumei/krpc/objects"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service"
Expand Down Expand Up @@ -108,8 +107,8 @@ func (s *mirrorClients) Predict(ctx context.Context, request *service.PredictReq
}

func MustNew() *mirrorClients {
zkConn := do.MustInvoke[*registry_zookeeper.ZookeeperRegistry](sconfig.Injector)
sConf := do.MustInvoke[*sconfig.FrameConfig](sconfig.Injector)
zkConn := do.MustInvoke[*registry_zookeeper.ZookeeperRegistry](kserver.Injector)
sConf := do.MustInvoke[*kserver.FrameConfig](kserver.Injector)
childNodes, _, err := zkConn.Children(sConf.ServiceName)
lo.Must0(err)
localIp, err := registry_zookeeper.GetLocalIp("")
Expand Down Expand Up @@ -182,7 +181,7 @@ func BackgroundTask() {
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (service.Predictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (service.Predictor, error) {
return new(ArbiterPredictorImpl), nil
})
clientsCache = doublebuf.New(MustNew(), MustNew())
Expand Down
4 changes: 2 additions & 2 deletions boilerplate/pkg/models/predictor_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/cloudwego/kitex/pkg/retry"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/ishumei/krpc/boilerplate/pkg/conf"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver"
prometheus "github.com/ishumei/krpc/monitor-prometheus"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service/predictor"
"github.com/kitex-contrib/obs-opentelemetry/tracing"
Expand Down Expand Up @@ -41,7 +41,7 @@ func (m *PredictorOption) apply(p *Predictor) {
c, _ := predictor.NewClient(
m.NodePath,
client.WithShortConnection(),
client.WithResolver(do.MustInvoke[discovery.Resolver](sconfig.Injector)),
client.WithResolver(do.MustInvoke[discovery.Resolver](kserver.Injector)),
client.WithTransportProtocol(DefaultTransportProtocol),
client.WithConnectTimeout(DefaultConnectTimeout),
client.WithRPCTimeout(time.Duration(m.TimeoutMs)*time.Millisecond),
Expand Down
4 changes: 2 additions & 2 deletions boilerplate/pkg/strategies/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/cloudwego/kitex/pkg/klog"
"github.com/ishumei/krpc/boilerplate/pkg/conf"
"github.com/ishumei/krpc/boilerplate/pkg/models"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/objects"
registry_zookeeper "github.com/ishumei/krpc/registry-zookeeper"
"github.com/samber/do"
Expand All @@ -17,7 +17,7 @@ func Init() error {
if err != nil {
return err
}
zookeeperConn, err := do.Invoke[*registry_zookeeper.ZookeeperResolver](sconfig.Injector)
zookeeperConn, err := do.Invoke[*registry_zookeeper.ZookeeperResolver](kserver.Injector)
if err != nil {
return err
}
Expand Down
14 changes: 6 additions & 8 deletions kserver/arbiter/arbiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package arbiter

import (
"github.com/cloudwego/kitex/server"
"github.com/ishumei/krpc/kserver/kservice"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver/ssuite"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service"
arbiterpredictor "github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service/predictor"
"github.com/samber/do"
)

type ArbiterService struct {
*kservice.Kservice
*kserver.Kservice
}

func NewArbiterService(i *do.Injector) (*ArbiterService, error) {
opts := do.MustInvoke[*ssuite.ServerOptions](sconfig.Injector)
opts := do.MustInvoke[*kserver.ServerOptions](kserver.Injector)

predictor := do.MustInvoke[service.Predictor](sconfig.Injector)
predictor := do.MustInvoke[service.Predictor](kserver.Injector)

return &ArbiterService{
Kservice: kservice.MustNewKservice(i, arbiterpredictor.NewServer(predictor, server.WithSuite(opts))),
Kservice: kserver.MustNewKservice(i, arbiterpredictor.NewServer(predictor, server.WithSuite(opts))),
}, nil
}

func init() {
do.Provide(sconfig.Injector, NewArbiterService)
do.Provide(kserver.Injector, NewArbiterService)
}
14 changes: 6 additions & 8 deletions kserver/audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package audio

import (
"github.com/cloudwego/kitex/server"
"github.com/ishumei/krpc/kserver/kservice"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver/ssuite"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/protocols/audio/kitex_gen/shumei/strategy/re"
"github.com/ishumei/krpc/protocols/audio/kitex_gen/shumei/strategy/re/audiopredictor"
"github.com/samber/do"
)

type AudioService struct {
*kservice.Kservice
*kserver.Kservice
}

func NewAudioService(i *do.Injector) (*AudioService, error) {
opts := do.MustInvoke[*ssuite.ServerOptions](sconfig.Injector)
opts := do.MustInvoke[*kserver.ServerOptions](kserver.Injector)

predictor := do.MustInvoke[re.AudioPredictor](sconfig.Injector)
predictor := do.MustInvoke[re.AudioPredictor](kserver.Injector)

return &AudioService{
Kservice: kservice.MustNewKservice(i, audiopredictor.NewServer(predictor, server.WithSuite(opts))),
Kservice: kserver.MustNewKservice(i, audiopredictor.NewServer(predictor, server.WithSuite(opts))),
}, nil
}

func init() {
do.Provide(sconfig.Injector, NewAudioService)
do.Provide(kserver.Injector, NewAudioService)
}
2 changes: 1 addition & 1 deletion kserver/sconfig/conf.go → kserver/conf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sconfig
package kserver

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion kserver/sconfig/conf_test.go → kserver/conf_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sconfig
package kserver

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion kserver/debug/debug.go → kserver/debug.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package debug
package kserver

import (
"net/http"
Expand Down
1 change: 0 additions & 1 deletion kserver/debug/debug_test.go

This file was deleted.

1 change: 1 addition & 0 deletions kserver/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package kserver
14 changes: 6 additions & 8 deletions kserver/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ package event

import (
"github.com/cloudwego/kitex/server"
"github.com/ishumei/krpc/kserver/kservice"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver/ssuite"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/protocols/event/kitex_gen/shumei/strategy/re"
"github.com/ishumei/krpc/protocols/event/kitex_gen/shumei/strategy/re/eventpredictor"
"github.com/samber/do"
)

type EventService struct {
*kservice.Kservice
*kserver.Kservice
}

func NewEventService(i *do.Injector) (*EventService, error) {
opts := do.MustInvoke[*ssuite.ServerOptions](sconfig.Injector)
opts := do.MustInvoke[*kserver.ServerOptions](kserver.Injector)

predictor := do.MustInvoke[re.EventPredictor](sconfig.Injector)
predictor := do.MustInvoke[re.EventPredictor](kserver.Injector)

return &EventService{
Kservice: kservice.MustNewKservice(i, eventpredictor.NewServer(predictor, server.WithSuite(opts))),
Kservice: kserver.MustNewKservice(i, eventpredictor.NewServer(predictor, server.WithSuite(opts))),
}, nil
}

func init() {
do.Provide(sconfig.Injector, NewEventService)
do.Provide(kserver.Injector, NewEventService)
}
6 changes: 3 additions & 3 deletions kserver/example/arbiter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/arbiter"
"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/protocols/arbiter/kitex_gen/com/shumei/service"
"github.com/samber/do"
)
Expand All @@ -22,15 +22,15 @@ func (s *predictorImpl) Health(ctx context.Context) (resp bool, err error) {

func main() {
os.Setenv("ENV_ROLE", "test")
injector := sconfig.Injector
injector := kserver.Injector
arbiterService := do.MustInvoke[*arbiter.ArbiterService](injector)
defer func() { _ = arbiterService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
arbiterService.Start()
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (service.Predictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (service.Predictor, error) {
return new(predictorImpl), nil
})
}
6 changes: 3 additions & 3 deletions kserver/example/audio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/audio"
"github.com/ishumei/krpc/kserver/sconfig"
re "github.com/ishumei/krpc/protocols/audio/kitex_gen/shumei/strategy/re"
"github.com/samber/do"
)
Expand All @@ -22,15 +22,15 @@ func (s *predictorImpl) Health(ctx context.Context) (resp bool, err error) {

func main() {
os.Setenv("ENV_ROLE", "test")
injector := sconfig.Injector
injector := kserver.Injector
audioService := do.MustInvoke[*audio.AudioService](injector)
defer func() { _ = audioService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
audioService.Start()
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (re.AudioPredictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (re.AudioPredictor, error) {
return new(predictorImpl), nil
})
}
6 changes: 3 additions & 3 deletions kserver/example/event/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/event"
"github.com/ishumei/krpc/kserver/sconfig"
re "github.com/ishumei/krpc/protocols/event/kitex_gen/shumei/strategy/re"
"github.com/samber/do"
)
Expand All @@ -22,15 +22,15 @@ func (s *predictorImpl) Health(ctx context.Context) (resp bool, err error) {

func main() {
os.Setenv("ENV_ROLE", "test")
injector := sconfig.Injector
injector := kserver.Injector
eventService := do.MustInvoke[*event.EventService](injector)
defer func() { _ = eventService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
eventService.Start()
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (re.EventPredictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (re.EventPredictor, error) {
return new(predictorImpl), nil
})
}
6 changes: 3 additions & 3 deletions kserver/example/image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"os"

"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/image"
"github.com/ishumei/krpc/kserver/sconfig"
re "github.com/ishumei/krpc/protocols/image/kitex_gen/shumei/strategy/re"
"github.com/samber/do"
)
Expand All @@ -22,15 +22,15 @@ func (s *predictorImpl) Health(ctx context.Context) (resp bool, err error) {

func main() {
os.Setenv("ENV_ROLE", "test")
injector := sconfig.Injector
injector := kserver.Injector
imageService := do.MustInvoke[*image.ImageService](injector)
defer func() { _ = imageService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
imageService.Start()
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (re.ImagePredictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (re.ImagePredictor, error) {
return new(predictorImpl), nil
})
}
6 changes: 3 additions & 3 deletions kserver/example/text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"os"

"github.com/ishumei/krpc/kserver/sconfig"
"github.com/ishumei/krpc/kserver"
"github.com/ishumei/krpc/kserver/text"
re "github.com/ishumei/krpc/protocols/text/kitex_gen/shumei/strategy/re"
"github.com/samber/do"
Expand All @@ -22,15 +22,15 @@ func (s *predictorImpl) Health(ctx context.Context) (resp bool, err error) {

func main() {
os.Setenv("ENV_ROLE", "test")
injector := sconfig.Injector
injector := kserver.Injector
textService := do.MustInvoke[*text.TextService](injector)
defer func() { _ = textService.Shutdown() }()
defer func() { _ = injector.Shutdown() }()
textService.Start()
}

func init() {
do.Provide(sconfig.Injector, func(i *do.Injector) (re.TextPredictor, error) {
do.Provide(kserver.Injector, func(i *do.Injector) (re.TextPredictor, error) {
return new(predictorImpl), nil
})
}
23 changes: 0 additions & 23 deletions kserver/governance/resolver.go

This file was deleted.

Loading

0 comments on commit 6391a3a

Please sign in to comment.