-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor grpc server creation in own package #1487
Refactor grpc server creation in own package #1487
Conversation
62316bb
to
7a06089
Compare
Codecov Report
@@ Coverage Diff @@
## master #1487 +/- ##
==========================================
- Coverage 99.81% 99.79% -0.03%
==========================================
Files 181 182 +1
Lines 8652 8711 +59
==========================================
+ Hits 8636 8693 +57
- Misses 8 9 +1
- Partials 8 9 +1
Continue to review full report at Codecov.
|
9678eca
to
65d0b2a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good start!
cmd/query/app/grpcserver/server.go
Outdated
// Prepare cmux conn. | ||
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", g.QueryOptions.Port)) | ||
if err != nil { | ||
g.Logger.Fatal("Could not start listener", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as a reusable package, it's better to return errors than to log them (main will log)
cmd/query/app/grpcserver/server.go
Outdated
QuerySvc querysvc.QueryService | ||
Logger *zap.Logger | ||
Tracer opentracing.Tracer | ||
QueryOptions *app.QueryOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like only Port is used, I would pass the Port explicitly and narrow the API surface
cmd/query/app/grpcserver/server.go
Outdated
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package grpcserver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not, strictly speaking, a grpc server, I would call it just "server". It can go under app/server.go
cmd/query/app/grpcserver/server.go
Outdated
} | ||
|
||
// Build a GrpcServer | ||
func (g *Builder) Build() *GrpcServer { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the value in separating Build() and Start()? Maybe just have a single Server struct and Start() function.
cmd/query/app/grpcserver/server.go
Outdated
// Start http, GRPC and cmux servers concurrently | ||
func (s *GrpcServer) Start() { | ||
|
||
logger := s.logger // shortcut |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not necessary in new code
cmd/query/main.go
Outdated
@@ -122,58 +117,17 @@ func main() { | |||
compressHandler := handlers.CompressHandler(r) | |||
recoveryHandler := recoveryhandler.NewRecoveryHandler(logger, true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like L102-118 could also be refactored into httpserver
pkg, and reused in all-in-one
cmd/query/app/grpcserver/server.go
Outdated
|
||
func (g *Builder) createHandler() *app.GRPCHandler { | ||
return app.NewGRPCHandler(g.QuerySvc, g.Logger, g.Tracer) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be nice to have similar function for HTTP Handler.
cmd/query/app/grpcserver/server.go
Outdated
Svc *flags.Service | ||
RecoveryHandler http.Handler | ||
QuerySvc querysvc.QueryService | ||
Logger *zap.Logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Svc includes logger
cmd/query/app/grpcserver/server.go
Outdated
// Builder provides the configuration used to build a GrpcServer | ||
type Builder struct { | ||
Svc *flags.Service | ||
RecoveryHandler http.Handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"recovery" is not relevant here, it's just HTTP handler.
I would prefer symmetry between GRPC and HTTP, i.e. either we accept fully instantiated handlers, OR we create both of them in this class (the latter is better, can share code w/ all-in-one)
cmd/query/app/grpcserver/server.go
Outdated
if err := s.httpServer.Serve(s.listeners[httpListener]); err != nil { | ||
logger.Fatal("Could not start HTTP server", zap.Error(err)) | ||
} | ||
s.svc.HC().Set(healthcheck.Unavailable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a goal of this PR, but we should really revisit this, because it's feasible that one of the servers will fail to start and set HC=not-healthy, while the others would start up fine and main will set HC to OK.
The goroutines should just be signaling to main() that they failed, and main can set the healthcheck.
Maybe add a TODO to fix this separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the docs for logger.Fatal
it says that
// ....
// The logger then calls os.Exit(1), even if logging at FatalLevel is
wouldn't that mean that anything below the log is dead code and won't be executed? If so should we log in the goroutine with fatal
or just have a mechanism for propagation of the errs to main()
, where they'll be logged.
a982dcd
to
0a8c857
Compare
0a8c857
to
8eda072
Compare
Signed-off-by: stefan vassilev <[email protected]>
Signed-off-by: stefan vassilev <[email protected]>
Signed-off-by: stefan vassilev <[email protected]>
Signed-off-by: stefan vassilev <[email protected]>
Resolves racing condition when healthcheck's status is set to available after it is set to unavailable by server initialization process Signed-off-by: stefan vassilev <[email protected]>
a316701
to
bd28b50
Compare
cmd/flags/service.go
Outdated
@@ -49,18 +49,23 @@ type Service struct { | |||
MetricsFactory metrics.Factory | |||
|
|||
signalsChannel chan os.Signal | |||
|
|||
// HcStatusChannel is used for error propagation | |||
HcStatusChannel chan healthcheck.Status |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't need to be exposed publicly, instead you can have a method SetHealthCheckStatus() - assuming this is how we want goroutines to interact with Service
cmd/query/app/server.go
Outdated
} | ||
|
||
// NewServer creates and initializes Server | ||
func NewServer(svc *flags.Service, router *mux.Router, querySvc querysvc.QueryService, tracker opentracing.Tracer, port int) (*Server, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tracker -> tracer
cmd/query/app/server.go
Outdated
httpListener: cmuxServer.Match(cmux.Any()), | ||
httpServer: createHTTPServer(router, svc.Logger), | ||
muxServer: cmuxServer, | ||
querySvc: querysvc.QueryService{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
querysvc.QueryService{}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my IDE does this sometimes, my bad
cmd/query/app/server_test.go
Outdated
err := flagsSvc.Admin.Serve() | ||
assert.NoError(t, err) | ||
flagsSvc.Logger = zap.NewNop() | ||
go flagsSvc.RunAndThen(func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens here?
Signed-off-by: stefan vassilev <[email protected]>
a0ddb2c
to
00a9e74
Compare
Signed-off-by: Yuri Shkuro <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
build is failing on gosec #1501 |
Thanks for PR! |
Follow-up: #1503 |
Signed-off-by: stefan vassilev [email protected]
Which problem is this PR solving?
Short description of the changes
grpc
server togrpcserver
package