Skip to content

Commit

Permalink
feat: update proto to v0.4.23
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Sep 25, 2024
1 parent bd788cf commit 0934318
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 32 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ func (c *LibrarianClient) RunBackgroundRefresh() {
time.Sleep(time.Hour)
}
}

func (c *LibrarianClient) WithToken(ctx context.Context) context.Context {
c.muToken.RLock()
defer c.muToken.RUnlock()
return WithToken(ctx, c.accessToken)
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/go-kratos/kratos/v2 v2.8.0
github.com/hashicorp/consul/api v1.29.1
github.com/invopop/jsonschema v0.12.0
github.com/tuihub/protos v0.4.20
google.golang.org/grpc v1.65.0
github.com/tuihub/protos v0.4.23
google.golang.org/grpc v1.66.0
google.golang.org/protobuf v1.34.2
)

Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.12.0 h1:4X+VP1GHd1Mhj6IB5mMeGbLCleqxjletLK6K0rbxyZI=
github.com/envoyproxy/go-control-plane v0.12.0/go.mod h1:ZBTaoJ23lqITozF0M6G4/IragXCQKCnYbmlmtHvwRG0=
github.com/envoyproxy/go-control-plane v0.12.1-0.20240621013728-1eb8caab5155 h1:IgJPqnrlY2Mr4pYB6oaMKvFvwJ9H+X6CCY5x1vCTcpc=
github.com/envoyproxy/go-control-plane v0.12.1-0.20240621013728-1eb8caab5155/go.mod h1:5Wkq+JduFtdAXihLmeTJf+tRYIT4KBc2vPXDhwVo1pA=
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand All @@ -60,8 +60,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
Expand Down Expand Up @@ -173,6 +171,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -206,8 +206,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tuihub/protos v0.4.20 h1:fozd8I6uRS1250YqmX7a26mFR/xUQPp4YAB0pqJoFZU=
github.com/tuihub/protos v0.4.20/go.mod h1:btbip/Tx2/xwrCuD/+pk7F/ElXhUYhjm8Dv2+uhd58o=
github.com/tuihub/protos v0.4.23 h1:qKRxguvvVbDNBPItSB3dGYxpz+lsex2sZDXt2NyT8rE=
github.com/tuihub/protos v0.4.23/go.mod h1:lmf29LH3wf7Fb0in47Q/ar2qf2V7ogckV6dnlBrsZ1I=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
Expand Down Expand Up @@ -270,8 +270,8 @@ google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/grpc v1.66.0 h1:DibZuoBznOxbDQxRINckZcUvnCEvrW9pcWIE2yF9r1c=
google.golang.org/grpc v1.66.0/go.mod h1:s3/l6xSSCURdVfAnL+TqCNMyTDAGN6+lZeVxnZR128Y=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
29 changes: 26 additions & 3 deletions porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
type Porter struct {
server *grpc.Server
requireAsUser bool
wrapper serviceWrapper
wrapper *serviceWrapper
logger log.Logger
app *kratos.App
consulConfig *capi.Config
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewPorter(
options ...PorterOption,
) (*Porter, error) {
if service == nil {
return nil, errors.New("service is nil")
return nil, errors.New("serviceServer is nil")
}
if info.GetBinarySummary() == nil {
return nil, errors.New("binary summary is nil")
Expand Down Expand Up @@ -112,14 +112,16 @@ func NewPorter(
if err != nil {
return nil, err
}
c := serviceWrapper{
c := &serviceWrapper{
LibrarianPorterServiceServer: service,
Info: info,
Logger: p.logger,
Client: client,
RequireToken: p.requireAsUser,
Token: nil,
tokenMu: sync.Mutex{},
lastHeartbeat: time.Time{},
lastRefreshToken: time.Time{},
}
p.wrapper = c
p.server = NewServer(
Expand Down Expand Up @@ -193,6 +195,27 @@ func WellKnownToString(e protoreflect.Enum) string {
))
}

func (p *Porter) ReverseCall(ctx context.Context) (*LibrarianClient, error) {
if !p.requireAsUser {
return nil, errors.New("init porter with `WithAsUser` option to use this method")
}
if p.wrapper.Token == nil {
return nil, errors.New("porter not enabled")
}
client, err := internal.NewSephirahClient(ctx, p.consulConfig, os.Getenv(sephirahServiceName))
if err != nil {
return nil, err
}
return &LibrarianClient{
LibrarianSephirahServiceClient: client,
accessToken: p.wrapper.Token.AccessToken,
refreshToken: "",
muToken: sync.RWMutex{},
backgroundRefresh: false,
consulConfig: p.consulConfig,
}, nil
}

func (p *Porter) AsUser(ctx context.Context, userID int64) (*LibrarianClient, error) {
if !p.requireAsUser {
return nil, errors.New("init porter with `WithAsUser` option to use this method")
Expand Down
65 changes: 46 additions & 19 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tuihub
import (
"context"
"fmt"
"sync"
"time"

pb "github.com/tuihub/protos/pkg/librarian/porter/v1"
Expand All @@ -14,13 +15,13 @@ import (
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/logging"
"github.com/go-kratos/kratos/v2/transport/grpc"
"google.golang.org/grpc/metadata"
)

const (
defaultHeartbeatInterval = time.Second * 10
defaultHeartbeatDowngrade = time.Second * 30
defaultHeartbeatTimeout = time.Second * 60
defaultRefreshToken = time.Hour / 2
)

type serviceWrapper struct {
Expand All @@ -30,8 +31,10 @@ type serviceWrapper struct {
Client sephirah.LibrarianSephirahServiceClient
RequireToken bool
Token *tokenInfo
tokenMu sync.Mutex

lastHeartbeat time.Time
lastHeartbeat time.Time
lastRefreshToken time.Time
}

type tokenInfo struct {
Expand All @@ -44,12 +47,29 @@ func (s *serviceWrapper) GetPorterInformation(ctx context.Context, req *pb.GetPo
*pb.GetPorterInformationResponse, error) {
return s.Info, nil
}
func (s *serviceWrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterRequest) (
func (s *serviceWrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterRequest) ( //nolint:gocognit //TODO
*pb.EnablePorterResponse, error) {
needRefreshToken := false
f := func() error {
if s.Token != nil {
s.tokenMu.Lock()
defer s.tokenMu.Unlock()
if s.Token != nil { //nolint:nestif //TODO
if s.Token.enabler == req.GetSephirahId() {
if req.GetRefreshToken() != "" {
resp, err := s.Client.RefreshToken(
WithToken(ctx, req.GetRefreshToken()),
new(sephirah.RefreshTokenRequest),
)
if err != nil {
return err
}
s.Token.AccessToken = resp.GetAccessToken()
s.Token.refreshToken = resp.GetRefreshToken()
s.lastRefreshToken = time.Now()
}
if s.RequireToken && (s.lastRefreshToken.Add(defaultRefreshToken).Before(time.Now()) || s.Token.refreshToken == "") {
needRefreshToken = true
}
return nil
} else if s.lastHeartbeat.Add(defaultHeartbeatTimeout).After(time.Now()) {
return fmt.Errorf("porter already enabled by %d", s.Token.enabler)
Expand All @@ -63,8 +83,10 @@ func (s *serviceWrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterR
needRefreshToken = true
return nil
}
ctx2 := metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+req.GetRefreshToken())
resp, err := s.Client.RefreshToken(ctx2, new(sephirah.RefreshTokenRequest))
resp, err := s.Client.RefreshToken(
WithToken(ctx, req.GetRefreshToken()),
new(sephirah.RefreshTokenRequest),
)
if err != nil {
return err
}
Expand All @@ -73,14 +95,19 @@ func (s *serviceWrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterR
AccessToken: resp.GetAccessToken(),
refreshToken: resp.GetRefreshToken(),
}
s.lastRefreshToken = time.Now()
}
return nil
}
if err := f(); err != nil {
return nil, err
}
if resp, err := s.LibrarianPorterServiceServer.EnablePorter(ctx, req); isUnimplementedError(err) {
return new(pb.EnablePorterResponse), nil
return &pb.EnablePorterResponse{
StatusMessage: "",
NeedRefreshToken: needRefreshToken || resp.GetNeedRefreshToken(),
EnablesSummary: nil,
}, nil
} else {
resp.NeedRefreshToken = needRefreshToken
return resp, err
Expand Down Expand Up @@ -113,25 +140,25 @@ func NewServer(c *ServerConfig, service pb.LibrarianPorterServiceServer, logger
return srv
}

type service struct {
serviceWrapper
type serviceServer struct {
*serviceWrapper
}

func NewService(p serviceWrapper) pb.LibrarianPorterServiceServer {
return &service{
func NewService(p *serviceWrapper) pb.LibrarianPorterServiceServer {
return &serviceServer{
p,
}
}

func (s *service) GetPorterInformation(ctx context.Context, req *pb.GetPorterInformationRequest) (
func (s *serviceServer) GetPorterInformation(ctx context.Context, req *pb.GetPorterInformationRequest) (
*pb.GetPorterInformationResponse, error) {
return s.serviceWrapper.GetPorterInformation(ctx, req)
}
func (s *service) EnablePorter(ctx context.Context, req *pb.EnablePorterRequest) (
func (s *serviceServer) EnablePorter(ctx context.Context, req *pb.EnablePorterRequest) (
*pb.EnablePorterResponse, error) {
return s.serviceWrapper.EnablePorter(ctx, req)
}
func (s *service) PullAccount(ctx context.Context, req *pb.PullAccountRequest) (
func (s *serviceServer) PullAccount(ctx context.Context, req *pb.PullAccountRequest) (
*pb.PullAccountResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
Expand All @@ -148,7 +175,7 @@ func (s *service) PullAccount(ctx context.Context, req *pb.PullAccountRequest) (
}
return nil, errors.BadRequest("Unsupported account platform", "")
}
func (s *service) PullAppInfo(ctx context.Context, req *pb.PullAppInfoRequest) (*pb.PullAppInfoResponse, error) {
func (s *serviceServer) PullAppInfo(ctx context.Context, req *pb.PullAppInfoRequest) (*pb.PullAppInfoResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
}
Expand All @@ -165,7 +192,7 @@ func (s *service) PullAppInfo(ctx context.Context, req *pb.PullAppInfoRequest) (
}
return nil, errors.BadRequest("Unsupported app source", "")
}
func (s *service) PullAccountAppInfoRelation(ctx context.Context, req *pb.PullAccountAppInfoRelationRequest) (
func (s *serviceServer) PullAccountAppInfoRelation(ctx context.Context, req *pb.PullAccountAppInfoRelationRequest) (
*pb.PullAccountAppInfoRelationResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
Expand All @@ -182,7 +209,7 @@ func (s *service) PullAccountAppInfoRelation(ctx context.Context, req *pb.PullAc
}
return nil, errors.BadRequest("Unsupported account", "")
}
func (s *service) SearchAppInfo(ctx context.Context, req *pb.SearchAppInfoRequest) (*pb.SearchAppInfoResponse, error) {
func (s *serviceServer) SearchAppInfo(ctx context.Context, req *pb.SearchAppInfoRequest) (*pb.SearchAppInfoResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
}
Expand All @@ -194,7 +221,7 @@ func (s *service) SearchAppInfo(ctx context.Context, req *pb.SearchAppInfoReques
}
return nil, errors.BadRequest("Unsupported app source", "")
}
func (s *service) PullFeed(ctx context.Context, req *pb.PullFeedRequest) (*pb.PullFeedResponse, error) {
func (s *serviceServer) PullFeed(ctx context.Context, req *pb.PullFeedRequest) (*pb.PullFeedResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
}
Expand All @@ -205,7 +232,7 @@ func (s *service) PullFeed(ctx context.Context, req *pb.PullFeedRequest) (*pb.Pu
}
return nil, errors.BadRequest("Unsupported feed source", "")
}
func (s *service) PushFeedItems(ctx context.Context, req *pb.PushFeedItemsRequest) (
func (s *serviceServer) PushFeedItems(ctx context.Context, req *pb.PushFeedItemsRequest) (
*pb.PushFeedItemsResponse, error) {
if !s.serviceWrapper.Enabled() {
return nil, errors.Forbidden("Unauthorized caller", "")
Expand Down

0 comments on commit 0934318

Please sign in to comment.