chore: bump http kit (#817)

* use new httpkit runner

* refactor out last httpkit changes

* fix timeout defaults

* fix wrong time input - closes #819
This commit is contained in:
Hayden 2024-03-01 18:07:03 -06:00 committed by GitHub
parent 77b4d594af
commit 2867a05c92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 121 additions and 67 deletions

View file

@ -2,6 +2,7 @@
package eventbus
import (
"context"
"sync"
"github.com/google/uuid"
@ -43,24 +44,29 @@ func New() *EventBus {
}
}
func (e *EventBus) Run() {
func (e *EventBus) Run(ctx context.Context) error {
if e.started {
panic("event bus already started")
}
e.started = true
for event := range e.ch {
e.mu.RLock()
arr, ok := e.subscribers[event.event]
e.mu.RUnlock()
for {
select {
case <-ctx.Done():
return nil
case event := <-e.ch:
e.mu.RLock()
arr, ok := e.subscribers[event.event]
e.mu.RUnlock()
if !ok {
continue
}
if !ok {
continue
}
for _, fn := range arr {
fn(event.data)
for _, fn := range arr {
fn(event.data)
}
}
}
}

View file

@ -45,7 +45,9 @@ func TestMain(m *testing.M) {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
go tbus.Run()
go func() {
_ = tbus.Run(context.Background())
}()
err = client.Schema.Create(context.Background())
if err != nil {

View file

@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"time"
"github.com/ardanlabs/conf/v3"
)
@ -39,12 +40,12 @@ type DebugConf struct {
}
type WebConfig struct {
Port string `yaml:"port" conf:"default:7745"`
Host string `yaml:"host"`
MaxUploadSize int64 `yaml:"max_file_upload" conf:"default:10"`
ReadTimeout int `yaml:"read_timeout" conf:"default:10"`
WriteTimeout int `yaml:"write_timeout" conf:"default:10"`
IdleTimeout int `yaml:"idle_timeout" conf:"default:30"`
Port string `yaml:"port" conf:"default:7745"`
Host string `yaml:"host"`
MaxUploadSize int64 `yaml:"max_file_upload" conf:"default:10"`
ReadTimeout time.Duration `yaml:"read_timeout" conf:"default:10s"`
WriteTimeout time.Duration `yaml:"write_timeout" conf:"default:10s"`
IdleTimeout time.Duration `yaml:"idle_timeout" conf:"default:30s"`
}
// New parses the CLI/Config file and returns a Config struct. If the file argument is an empty string, the

View file

@ -16,7 +16,7 @@ type ErrorResponse struct {
Fields map[string]string `json:"fields,omitempty"`
}
func Errors(svr *server.Server, log zerolog.Logger) errchain.ErrorHandler {
func Errors(log zerolog.Logger) errchain.ErrorHandler {
return func(h errchain.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := h.ServeHTTP(w, r)
@ -71,14 +71,6 @@ func Errors(svr *server.Server, log zerolog.Logger) errchain.ErrorHandler {
if err := server.JSON(w, code, resp); err != nil {
log.Err(err).Msg("failed to write response")
}
// If Showdown error, return error
if server.IsShutdownError(err) {
err := svr.Shutdown(err.Error())
if err != nil {
log.Err(err).Msg("failed to shutdown server")
}
}
}
})
}