Skip to content

Commit 8dadb79

Browse files
committed
to separate the http and grpc driven services
1 parent f2a6dde commit 8dadb79

File tree

2 files changed

+93
-108
lines changed

2 files changed

+93
-108
lines changed

cmd/revad/runtime/drivenserver.go

Lines changed: 89 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net"
66
"net/http"
77
"os"
8-
"sync"
98
"time"
109

1110
"github.com/owncloud/reva/v2/pkg/registry"
@@ -14,11 +13,15 @@ import (
1413
"github.com/rs/zerolog"
1514
)
1615

17-
type drivenServer struct {
16+
type drivenHTTPServer struct {
1817
rhttpServer *rhttp.Server
18+
gracefulShutdownTimeout int
19+
log *zerolog.Logger
20+
}
21+
22+
type drivenGRPCServer struct {
1923
rgrpcServer *rgrpc.Server
2024
gracefulShutdownTimeout int
21-
pidFile string
2225
log *zerolog.Logger
2326
}
2427

@@ -30,7 +33,7 @@ type RevaDrivenServer interface {
3033

3134
// RunDrivenServerWithOptions runs a revad server w/o watcher with the given config file, pid file and options.
3235
// Use it in cases where you want to run a revad server without the need for a watcher and the os signal handling as a part of another runtime.
33-
func RunDrivenServerWithOptions(mainConf map[string]interface{}, pidFile string, opts ...Option) RevaDrivenServer {
36+
func RunDrivenServerWithOptions(mainConf map[string]interface{}, opts ...Option) (RevaDrivenServer, RevaDrivenServer) {
3437
options := newOptions(opts...)
3538
parseSharedConfOrDie(mainConf["shared"])
3639
coreConf := parseCoreConfOrDie(mainConf["core"])
@@ -50,86 +53,22 @@ func RunDrivenServerWithOptions(mainConf map[string]interface{}, pidFile string,
5053
}
5154
initCPUCount(coreConf, log)
5255

53-
server := &drivenServer{
54-
rhttpServer: initHTTPServer(mainConf, &options),
55-
rgrpcServer: initGRPCServer(mainConf, &options),
56-
log: log,
57-
pidFile: pidFile,
58-
}
59-
server.gracefulShutdownTimeout = 30
56+
gracefulShutdownTimeout := 30
6057
if coreConf.GracefulShutdownTimeout > 0 {
61-
server.gracefulShutdownTimeout = coreConf.GracefulShutdownTimeout
58+
gracefulShutdownTimeout = coreConf.GracefulShutdownTimeout
6259
}
6360

64-
if server.rhttpServer == nil && server.rgrpcServer == nil {
65-
log.Fatal().Msg("nothing to do, no grpc/http enabled_services declared in config")
66-
}
67-
return server
68-
}
69-
70-
// Start runs the revad drivenServer with the given config file and pid file.
71-
func (s *drivenServer) Start() error {
72-
errCh := make(chan error, 2)
73-
done := make(chan struct{}, 1)
74-
wg := &sync.WaitGroup{}
75-
76-
if s.rhttpServer != nil {
77-
wg.Add(1)
78-
go func() {
79-
errCh <- s.startHTTPServer()
80-
wg.Done()
81-
}()
82-
}
83-
if s.rgrpcServer != nil {
84-
wg.Add(1)
85-
go func() {
86-
errCh <- s.startGRPCServer()
87-
wg.Done()
88-
}()
89-
}
90-
91-
go func() {
92-
wg.Wait()
93-
close(done)
94-
}()
95-
for {
96-
select {
97-
case err := <-errCh:
98-
if err != nil && !errors.Is(err, http.ErrServerClosed) {
99-
return err
100-
}
101-
case <-done:
102-
return nil
103-
}
104-
}
105-
}
61+
rhttpServer := initHTTPServer(mainConf, &options, gracefulShutdownTimeout)
62+
rgrpcServer := initGRPCServer(mainConf, &options, gracefulShutdownTimeout)
10663

107-
// Stop gracefully stops the revad drivenServer.
108-
func (s *drivenServer) Stop() error {
109-
wg := &sync.WaitGroup{}
110-
111-
s.gracefulStopHTTPServer(wg)
112-
s.gracefulStopGRPCServer(wg)
113-
114-
done := make(chan struct{})
115-
go func() {
116-
wg.Wait()
117-
close(done)
118-
}()
119-
120-
select {
121-
case <-time.After(time.Duration(s.gracefulShutdownTimeout) * time.Second):
122-
s.log.Info().Msg("graceful shutdown timeout reached. running hard shutdown")
123-
s.stopHTTPServer()
124-
s.stopGRPCServer()
125-
return nil
126-
case <-done:
127-
s.log.Info().Msg("all revad services gracefully stopped")
128-
return nil
64+
if rhttpServer == nil && rgrpcServer == nil {
65+
log.Fatal().Msg("nothing to do, no grpc/http enabled_services declared in config")
12966
}
67+
return rhttpServer, rgrpcServer
13068
}
13169

132-
func (s *drivenServer) startHTTPServer() error {
70+
// Start runs the revad HTTP drivenServer with the given config file.
71+
func (s *drivenHTTPServer) Start() error {
13372
if s.rhttpServer == nil {
13473
s.log.Fatal().Msg("http server not initialized")
13574
}
@@ -146,7 +85,8 @@ func (s *drivenServer) startHTTPServer() error {
14685
return nil
14786
}
14887

149-
func (s *drivenServer) startGRPCServer() error {
88+
// Start runs the revad GRPC drivenServer with the given config file.
89+
func (s *drivenGRPCServer) Start() error {
15090
if s.rgrpcServer == nil {
15191
s.log.Fatal().Msg("grcp server not initialized")
15292
}
@@ -163,35 +103,72 @@ func (s *drivenServer) startGRPCServer() error {
163103
return nil
164104
}
165105

166-
func (s *drivenServer) gracefulStopHTTPServer(wg *sync.WaitGroup) {
106+
// Stop gracefully stops the revad drivenServer.
107+
func (s *drivenHTTPServer) Stop() error {
108+
if s == nil || s.rhttpServer == nil {
109+
return nil
110+
}
111+
done := make(chan struct{})
112+
go func() {
113+
s.gracefulStopHTTPServer()
114+
close(done)
115+
}()
116+
117+
select {
118+
case <-time.After(time.Duration(s.gracefulShutdownTimeout) * time.Second):
119+
s.log.Info().Msg("graceful shutdown timeout reached. running hard shutdown")
120+
s.stopHTTPServer()
121+
return nil
122+
case <-done:
123+
s.log.Info().Msg("all revad services gracefully stopped")
124+
return nil
125+
}
126+
}
127+
128+
// Stop gracefully stops the revad drivenServer.
129+
func (s *drivenGRPCServer) Stop() error {
130+
if s == nil || s.rgrpcServer == nil {
131+
return nil
132+
}
133+
134+
done := make(chan struct{})
135+
go func() {
136+
s.gracefulStopGRPCServer()
137+
close(done)
138+
}()
139+
140+
select {
141+
case <-time.After(time.Duration(s.gracefulShutdownTimeout) * time.Second):
142+
s.log.Info().Msg("graceful shutdown timeout reached. running hard shutdown")
143+
s.stopGRPCServer()
144+
return nil
145+
case <-done:
146+
s.log.Info().Msg("all revad services gracefully stopped")
147+
return nil
148+
}
149+
}
150+
151+
func (s *drivenHTTPServer) gracefulStopHTTPServer() {
167152
if s.rhttpServer != nil {
168-
wg.Add(1)
169-
go func() {
170-
defer wg.Done()
171-
s.log.Info().Msgf("fd to %s:%s gracefully closing", s.rhttpServer.Network(), s.rhttpServer.Address())
172-
if err := s.rhttpServer.GracefulStop(); err != nil {
173-
s.log.Error().Err(err).Msg("error gracefully stopping server")
174-
s.rhttpServer.Stop()
175-
}
176-
}()
153+
s.log.Info().Msgf("fd to %s:%s gracefully closing", s.rhttpServer.Network(), s.rhttpServer.Address())
154+
if err := s.rhttpServer.GracefulStop(); err != nil {
155+
s.log.Error().Err(err).Msg("error gracefully stopping server")
156+
s.rhttpServer.Stop()
157+
}
177158
}
178159
}
179160

180-
func (s *drivenServer) gracefulStopGRPCServer(wg *sync.WaitGroup) {
161+
func (s *drivenGRPCServer) gracefulStopGRPCServer() {
181162
if s.rgrpcServer != nil {
182-
wg.Add(1)
183-
go func() {
184-
defer wg.Done()
185-
s.log.Info().Msgf("fd to %s:%s gracefully closing", s.rgrpcServer.Network(), s.rgrpcServer.Address())
186-
if err := s.rgrpcServer.GracefulStop(); err != nil {
187-
s.log.Error().Err(err).Msg("error gracefully stopping server")
188-
s.rgrpcServer.Stop()
189-
}
190-
}()
163+
s.log.Info().Msgf("fd to %s:%s gracefully closing", s.rgrpcServer.Network(), s.rgrpcServer.Address())
164+
if err := s.rgrpcServer.GracefulStop(); err != nil {
165+
s.log.Error().Err(err).Msg("error gracefully stopping server")
166+
s.rgrpcServer.Stop()
167+
}
191168
}
192169
}
193170

194-
func (s *drivenServer) stopHTTPServer() {
171+
func (s *drivenHTTPServer) stopHTTPServer() {
195172
if s.rhttpServer == nil {
196173
return
197174
}
@@ -202,7 +179,7 @@ func (s *drivenServer) stopHTTPServer() {
202179
}
203180
}
204181

205-
func (s *drivenServer) stopGRPCServer() {
182+
func (s *drivenGRPCServer) stopGRPCServer() {
206183
if s.rgrpcServer == nil {
207184
return
208185
}
@@ -213,24 +190,32 @@ func (s *drivenServer) stopGRPCServer() {
213190
}
214191
}
215192

216-
func initHTTPServer(mainConf map[string]interface{}, options *Options) *rhttp.Server {
193+
func initHTTPServer(mainConf map[string]interface{}, options *Options, timeout int) RevaDrivenServer {
217194
if isEnabledHTTP(mainConf) {
218195
s, err := getHTTPServer(mainConf["http"], options.Logger, options.TraceProvider)
219196
if err != nil {
220197
options.Logger.Fatal().Err(err).Msg("error creating http server")
221198
}
222-
return s
199+
return &drivenHTTPServer{
200+
rhttpServer: s,
201+
gracefulShutdownTimeout: timeout,
202+
log: options.Logger,
203+
}
223204
}
224205
return nil
225206
}
226207

227-
func initGRPCServer(mainConf map[string]interface{}, options *Options) *rgrpc.Server {
208+
func initGRPCServer(mainConf map[string]interface{}, options *Options, timeout int) RevaDrivenServer {
228209
if isEnabledGRPC(mainConf) {
229210
s, err := getGRPCServer(mainConf["grpc"], options.Logger, options.TraceProvider)
230211
if err != nil {
231212
options.Logger.Fatal().Err(err).Msg("error creating grpc server")
232213
}
233-
return s
214+
return &drivenGRPCServer{
215+
rgrpcServer: s,
216+
gracefulShutdownTimeout: timeout,
217+
log: options.Logger,
218+
}
234219
}
235220
return nil
236221
}

pkg/rgrpc/rgrpc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"net"
2626
"sort"
2727

28+
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
29+
"github.com/mitchellh/mapstructure"
2830
"github.com/owncloud/reva/v2/internal/grpc/interceptors/appctx"
2931
"github.com/owncloud/reva/v2/internal/grpc/interceptors/auth"
3032
"github.com/owncloud/reva/v2/internal/grpc/interceptors/log"
@@ -33,8 +35,6 @@ import (
3335
"github.com/owncloud/reva/v2/internal/grpc/interceptors/useragent"
3436
"github.com/owncloud/reva/v2/pkg/sharedconf"
3537
rtrace "github.com/owncloud/reva/v2/pkg/trace"
36-
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
37-
"github.com/mitchellh/mapstructure"
3838
"github.com/pkg/errors"
3939
"github.com/rs/zerolog"
4040
mtls "go-micro.dev/v4/util/tls"
@@ -279,15 +279,15 @@ func (s *Server) cleanupServices() {
279279

280280
// Stop stops the server.
281281
func (s *Server) Stop() error {
282-
s.cleanupServices()
283282
s.s.Stop()
283+
s.cleanupServices()
284284
return nil
285285
}
286286

287287
// GracefulStop gracefully stops the server.
288288
func (s *Server) GracefulStop() error {
289-
s.cleanupServices()
290289
s.s.GracefulStop()
290+
s.cleanupServices()
291291
return nil
292292
}
293293

0 commit comments

Comments
 (0)