Skip to content

Commit

Permalink
Add BindHandlers method to allow chaining bindhandlers
Browse files Browse the repository at this point in the history
  • Loading branch information
plorenz committed Mar 3, 2023
1 parent fccaf15 commit a28dff8
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
// Binding is used to add handlers to Channel.
//
// NOTE: It is intended that the Add* methods are used at initial channel setup, and not invoked on an in-service
// Channel. This API may change in the future to enforce those semantics programmatically.
//
// Channel. The Binding should not be retained once the channel setup is complete
type Binding interface {
Bind(h BindHandler) error
AddPeekHandler(h PeekHandler)
Expand All @@ -49,6 +48,26 @@ func (f BindHandlerF) BindChannel(binding Binding) error {
return f(binding)
}

// BindHandlers takes the given handlers and returns a BindHandler which
// runs the handlers one at a time, returning an error as soon as
// an error is encountered, or nil, if no errors are encountered.
func BindHandlers(handlers ...BindHandler) BindHandler {
if len(handlers) == 1 {
return handlers[0]
}

return BindHandlerF(func(binding Binding) error {
for _, handler := range handlers {
if handler != nil {
if err := handler.BindChannel(binding); err != nil {
return err
}
}
}
return nil
})
}

type ConnectionHandler interface {
HandleConnection(hello *Hello, certificates []*x509.Certificate) error
}
Expand Down Expand Up @@ -111,4 +130,4 @@ type CloseHandlerF func(ch Channel)

func (self CloseHandlerF) HandleClose(ch Channel) {
self(ch)
}
}

0 comments on commit a28dff8

Please sign in to comment.