Skip to content

Commit

Permalink
move syncing between handlign events into event manager & cleanup htt…
Browse files Browse the repository at this point in the history
…p server
  • Loading branch information
topi314 committed Apr 16, 2022
1 parent ff86d8d commit 1563cca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
6 changes: 6 additions & 0 deletions bot/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type eventManagerImpl struct {
client Client
eventListenerMu sync.Mutex
config EventManagerConfig

mu sync.Mutex
}

func (e *eventManagerImpl) RawEventsEnabled() bool {
Expand All @@ -71,6 +73,8 @@ func (e *eventManagerImpl) RawEventsEnabled() bool {

// HandleGatewayEvent calls the correct core.EventHandler
func (e *eventManagerImpl) HandleGatewayEvent(gatewayEventType discord.GatewayEventType, sequenceNumber int, reader io.Reader) {
e.mu.Lock()
defer e.mu.Unlock()
if handler, ok := e.config.GatewayHandlers[gatewayEventType]; ok {
v := handler.New()
if v != nil {
Expand All @@ -87,6 +91,8 @@ func (e *eventManagerImpl) HandleGatewayEvent(gatewayEventType discord.GatewayEv

// HandleHTTPEvent calls the correct core.EventHandler
func (e *eventManagerImpl) HandleHTTPEvent(respondFunc httpserver.RespondFunc, reader io.Reader) {
e.mu.Lock()
defer e.mu.Unlock()
v := e.config.HTTPServerHandler.New()
if err := json.NewDecoder(reader).Decode(&v); err != nil {
e.client.Logger().Error("error while unmarshalling httpserver event. error: ", err)
Expand Down
27 changes: 4 additions & 23 deletions httpserver/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,16 @@ func New(eventHandlerFunc EventHandlerFunc, opts ...ConfigOpt) Server {

return &serverImpl{
config: *config,
eventHandlerFunc: eventHandlerFunc,
publicKey: hexDecodedKey,
eventHandlerFunc: eventHandlerFunc,
}
}

// serverImpl is used in Client's webhook server for interactions
type serverImpl struct {
config Config
eventHandlerFunc EventHandlerFunc
publicKey PublicKey
interactionCh chan interaction
}

type interaction struct {
respondFunc RespondFunc
reader io.Reader
eventHandlerFunc EventHandlerFunc
}

func (s *serverImpl) Logger() log.Logger {
Expand All @@ -57,17 +51,7 @@ func (s *serverImpl) PublicKey() PublicKey {
}

func (s *serverImpl) Handle(respondFunc RespondFunc, payload io.Reader) {
s.interactionCh <- interaction{
respondFunc: respondFunc,
reader: payload,
}
}

func (s *serverImpl) listen() {
s.interactionCh = make(chan interaction)
for i := range s.interactionCh {
s.eventHandlerFunc(i.respondFunc, i.reader)
}
s.eventHandlerFunc(respondFunc, payload)
}

// Start makes the serverImpl listen on the specified port and handle requests
Expand All @@ -76,8 +60,6 @@ func (s *serverImpl) Start() {
s.config.HTTPServer.Addr = s.config.Address
s.config.HTTPServer.Handler = s.config.ServeMux

go s.listen()

go func() {
var err error
if s.config.CertFile != "" && s.config.KeyFile != "" {
Expand All @@ -86,15 +68,14 @@ func (s *serverImpl) Start() {
err = s.config.HTTPServer.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
s.config.Logger.Error("error while running http server: ", err)
s.Logger().Error("error while running http server: ", err)
}
}()
}

// Close shuts down the serverImpl
func (s *serverImpl) Close(ctx context.Context) {
_ = s.config.HTTPServer.Shutdown(ctx)
close(s.interactionCh)
}

type WebhookInteractionHandler struct {
Expand Down

0 comments on commit 1563cca

Please sign in to comment.