Skip to content

Commit

Permalink
chore: various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Oct 24, 2024
1 parent 7857d78 commit d57b771
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
25 changes: 18 additions & 7 deletions bolt_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func NewBoltTransport(
return nil, &TransportError{err: err}
}

lastEventID, err := getDBLastEventID(db, bucketName)
if err != nil {
return nil, &TransportError{err: err}
}

return &BoltTransport{
logger: logger,
db: db,
Expand All @@ -107,13 +112,13 @@ func NewBoltTransport(

subscribers: NewSubscriberList(1e5),
closed: make(chan struct{}),
lastEventID: getDBLastEventID(db, bucketName),
lastEventID: lastEventID,
}, nil
}

func getDBLastEventID(db *bolt.DB, bucketName string) string {
func getDBLastEventID(db *bolt.DB, bucketName string) (string, error) {
lastEventID := EarliestLastEventID
db.View(func(tx *bolt.Tx) error {
err := db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
if b == nil {
return nil // No data
Expand All @@ -126,7 +131,11 @@ func getDBLastEventID(db *bolt.DB, bucketName string) string {
return nil
})

return lastEventID
if err != nil {
return "", err

Check failure on line 135 in bolt_transport.go

View workflow job for this annotation

GitHub Actions / Lint

error returned from external package is unwrapped: sig: func (*go.etcd.io/bbolt.DB).View(fn func(*go.etcd.io/bbolt.Tx) error) error (wrapcheck)
}

return lastEventID, nil
}

// Dispatch dispatches an update to all subscribers and persists it in Bolt DB.
Expand Down Expand Up @@ -207,7 +216,9 @@ func (t *BoltTransport) AddSubscriber(s *Subscriber) error {
t.Unlock()

if s.RequestLastEventID != "" {
t.dispatchHistory(s, toSeq)
if err := t.dispatchHistory(s, toSeq); err != nil {
return err
}
}

s.Ready()
Expand Down Expand Up @@ -239,8 +250,8 @@ func (t *BoltTransport) GetSubscribers() (string, []*Subscriber, error) {
}

//nolint:gocognit
func (t *BoltTransport) dispatchHistory(s *Subscriber, toSeq uint64) {
t.db.View(func(tx *bolt.Tx) error {
func (t *BoltTransport) dispatchHistory(s *Subscriber, toSeq uint64) error {
return t.db.View(func(tx *bolt.Tx) error {

Check failure on line 254 in bolt_transport.go

View workflow job for this annotation

GitHub Actions / Lint

error returned from external package is unwrapped: sig: func (*go.etcd.io/bbolt.DB).View(fn func(*go.etcd.io/bbolt.Tx) error) error (wrapcheck)
b := tx.Bucket([]byte(t.bucketName))
if b == nil {
s.HistoryDispatched(EarliestLastEventID)
Expand Down
2 changes: 1 addition & 1 deletion demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var uiContent embed.FS
// The Content-Type header will automatically be set according to the URL's extension.
func (h *Hub) Demo(w http.ResponseWriter, r *http.Request) {
// JSON-LD is the preferred format
mime.AddExtensionType(".jsonld", "application/ld+json")
_ = mime.AddExtensionType(".jsonld", "application/ld+json")

url := r.URL.String()
mimeType := mime.TypeByExtension(filepath.Ext(r.URL.Path))
Expand Down
2 changes: 1 addition & 1 deletion jwt_keyfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestCreateJWTKeyfunc(t *testing.T) {
f, err := createJWTKeyfunc(([]byte{}), "invalid")
f, err := createJWTKeyfunc([]byte{}, "invalid")
require.Error(t, err)
require.Nil(t, f)
}
Expand Down
2 changes: 1 addition & 1 deletion subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func escapeTopics(topics []string) []string {
return escapedTopics
}

// MatchTopic checks if the current subscriber can access to the given topic.
// MatchTopics checks if the current subscriber can access to the given topic.
//
//nolint:gocognit
func (s *Subscriber) MatchTopics(topics []string, private bool) bool {
Expand Down
2 changes: 1 addition & 1 deletion subscriber_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func BenchmarkSubscriberList(b *testing.B) {
l := NewSubscriberList(100)
for i := 0; i < 100; i++ {
s := NewSubscriber("", logger, tss)
t := fmt.Sprintf("https://example.com/%d", (i % 10))
t := fmt.Sprintf("https://example.com/%d", i%10)
s.SetTopics([]string{"https://example.org/foo", t}, []string{"https://example.net/bar", t})

l.Add(s)
Expand Down
2 changes: 1 addition & 1 deletion topic_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
"strings"

uritemplate "github.com/yosida95/uritemplate/v3"
"github.com/yosida95/uritemplate/v3"
)

type TopicSelectorStoreCache interface {
Expand Down

0 comments on commit d57b771

Please sign in to comment.