Skip to content

Commit

Permalink
feat: impl heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Aug 7, 2024
1 parent cf7a0b3 commit f7d2be0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 7 additions & 6 deletions porter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ func NewPorter(
return nil, err
}
c := serviceWrapper{
Handler: handler,
Info: info,
Logger: p.logger,
Client: client,
RequireToken: p.requireAsUser,
Token: nil,
Handler: handler,
Info: info,
Logger: p.logger,
Client: client,
RequireToken: p.requireAsUser,
Token: nil,
lastHeartbeat: time.Time{},
}
p.wrapper = c
p.server = NewServer(
Expand Down
11 changes: 10 additions & 1 deletion wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ import (
"google.golang.org/grpc/metadata"
)

const (
defaultHeartbeatInterval = time.Second * 10
defaultHeartbeatDowngrade = time.Second * 30
defaultHeartbeatTimeout = time.Second * 60
)

type serviceWrapper struct {
Handler Handler
Info *pb.GetPorterInformationResponse
Logger log.Logger
Client sephirah.LibrarianSephirahServiceClient
RequireToken bool
Token *tokenInfo

lastHeartbeat time.Time
}

type tokenInfo struct {
Expand All @@ -45,10 +53,11 @@ func (s *serviceWrapper) EnablePorter(ctx context.Context, req *pb.EnablePorterR
NeedRefreshToken: false,
EnablesSummary: nil,
}, nil
} else {
} else if s.lastHeartbeat.Add(defaultHeartbeatTimeout).After(time.Now()) {
return nil, fmt.Errorf("porter already enabled by %d", s.Token.enabler)
}
}
s.lastHeartbeat = time.Now()
if s.RequireToken {
ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+req.GetRefreshToken())
resp, err := s.Client.RefreshToken(ctx, new(sephirah.RefreshTokenRequest))
Expand Down

0 comments on commit f7d2be0

Please sign in to comment.