• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/commandhandler/bus/commandhandler.go
1
// Copyright (c) 2014 - The Event Horizon authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package bus
16

17
import (
18
        "context"
19
        "errors"
20
        "sync"
21

22
        eh "github.com/looplab/eventhorizon"
23
)
24

25
var (
26
        // ErrHandlerAlreadySet is when a handler is already registered for a command.
27
        ErrHandlerAlreadySet = errors.New("handler is already set")
28
        // ErrHandlerNotFound is when no handler can be found.
29
        ErrHandlerNotFound = errors.New("no handlers for command")
30
)
31

32
// CommandHandler is a command handler that handles commands by routing to the
33
// registered CommandHandlers.
34
type CommandHandler struct {
35
        handlers   map[eh.CommandType]eh.CommandHandler
36
        handlersMu sync.RWMutex
37
}
38

39
// NewCommandHandler creates a CommandHandler.
40
func NewCommandHandler() *CommandHandler {
×
41
        return &CommandHandler{
×
42
                handlers: make(map[eh.CommandType]eh.CommandHandler),
×
43
        }
×
44
}
×
45

46
// HandleCommand handles a command with a handler capable of handling it.
47
func (h *CommandHandler) HandleCommand(ctx context.Context, cmd eh.Command) error {
×
48
        select {
×
49
        case <-ctx.Done():
×
50
                return ctx.Err()
×
51
        default:
×
52
        }
53

54
        if err := eh.CheckCommand(cmd); err != nil {
×
55
                return err
×
56
        }
×
57

58
        h.handlersMu.RLock()
×
59
        defer h.handlersMu.RUnlock()
×
60

×
61
        if handler, ok := h.handlers[cmd.CommandType()]; ok {
×
62
                return handler.HandleCommand(ctx, cmd)
×
63
        }
×
64

65
        return ErrHandlerNotFound
×
66
}
67

68
// SetHandler adds a handler for a specific command.
69
func (h *CommandHandler) SetHandler(handler eh.CommandHandler, cmdType eh.CommandType) error {
×
70
        h.handlersMu.Lock()
×
71
        defer h.handlersMu.Unlock()
×
72

×
73
        if _, ok := h.handlers[cmdType]; ok {
×
74
                return ErrHandlerAlreadySet
×
75
        }
×
76

77
        h.handlers[cmdType] = handler
×
78

×
79
        return nil
×
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc