Skip to content

Commit

Permalink
Close the API HTTP Server When Ctx Canceled and Fix Retry Utils (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
rauljordan authored Dec 6, 2024
1 parent 3f20931 commit d0a87de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

"github.com/gorilla/mux"

"github.com/ethereum/go-ethereum/log"

"github.com/offchainlabs/bold/api/backend"
"github.com/offchainlabs/bold/util/stopwaiter"
)
Expand Down Expand Up @@ -53,13 +55,15 @@ func New(addr string, backend backend.BusinessLogicProvider) (*Server, error) {

func (s *Server) Start(ctx context.Context) error {
s.StopWaiter.Start(ctx, s)
go func() {
<-ctx.Done()
if err := s.srv.Shutdown(ctx); err != nil {
log.Error("Could not shutdown API server", "err", err)
}
}()
return s.srv.ListenAndServe()
}

func (s *Server) Stop(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}

func (s *Server) Addr() string {
return s.srv.Addr
}
Expand Down
6 changes: 5 additions & 1 deletion runtime/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func UntilSucceedsMultipleReturnValue[T, U any](ctx context.Context, fn func() (
"err", err,
)
retryCounter.Inc(1)
time.Sleep(cfg.sleepTime)
select {
case <-ctx.Done():
return zeroVal[T](), zeroVal[U](), ctx.Err()
case <-time.After(cfg.sleepTime):
}
continue
}
return got, got2, nil
Expand Down

0 comments on commit d0a87de

Please sign in to comment.