Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
  • Loading branch information
sejongk committed Sep 13, 2023
1 parent dcdd8f9 commit 1985575
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
6 changes: 4 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ func (c *Client) Watch(
}

rch := make(chan WatchResponse)

stream, err := c.client.WatchDocument(
withShardKey(ctx, c.options.APIKey, doc.Key().String()),
&api.WatchDocumentRequest{
Expand Down Expand Up @@ -554,7 +553,10 @@ func handleResponse(
if handler, ok := doc.BroadcastEventHandlers()[eventBody.Topic]; ok && handler != nil {
err := handler(eventBody.Topic, resp.Event.Publisher, eventBody.Payload)
if err != nil {
return nil, err
return &WatchResponse{
Type: DocumentBroadcast,
Err: err,
}, nil
}
}
return nil, nil
Expand Down
14 changes: 7 additions & 7 deletions pkg/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func (d *Document) RemoveOnlineClient(clientID string) {
d.doc.RemoveOnlineClient(clientID)
}

// Events returns the document events of this document.
// Events returns the events of this document.
func (d *Document) Events() <-chan DocEvent {
return d.events
}
Expand All @@ -375,7 +375,7 @@ func (d *Document) BroadcastResponses() chan error {
return d.broadcastResponses
}

// Broadcast encodes the payload and makes a "Broadcast" type request.
// Broadcast encodes the given payload and sends a Broadcast request.
func (d *Document) Broadcast(topic string, payload any) error {
marshaled, err := gojson.Marshal(payload)
if err != nil {
Expand All @@ -389,24 +389,24 @@ func (d *Document) Broadcast(topic string, payload any) error {
return <-d.broadcastResponses
}

// SubscribeBroadcastEvent subscribes to the registers an event handler and makes
// a "Subscribe" type request.
// SubscribeBroadcastEvent subscribes to the given topic and registers
// an event handler
func (d *Document) SubscribeBroadcastEvent(
topic string,
handler func(topic, publisher string, payload []byte) error,
) {
d.broadcastEventHandlers[topic] = handler
}

// UnsubscribeBroadcastEvent deregisters the event handler and makes
// a "Unsubscribe" type request.
// UnsubscribeBroadcastEvent unsubscribes to the given topic and deregisters
// the event handler
func (d *Document) UnsubscribeBroadcastEvent(
topic string,
) {
delete(d.broadcastEventHandlers, topic)
}

// BroadcastEventHandlers returns registered event handlers for events.
// BroadcastEventHandlers returns the registered handlers for broadcast events.
func (d *Document) BroadcastEventHandlers() map[string](func(topic string,
publisher string, payload []byte) error) {
return d.broadcastEventHandlers
Expand Down
15 changes: 13 additions & 2 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ func TestDocument(t *testing.T) {

ctx := context.Background()
handler := func(topic, publisher string, payload []byte) error {
var mentionedBy string
assert.Equal(t, topic, "mention")
assert.NoError(t, gojson.Unmarshal(payload, &mentionedBy))
return ErrBroadcastEventHandlingError
}

Expand All @@ -631,7 +634,7 @@ func TestDocument(t *testing.T) {
assert.NoError(t, c2.Attach(ctx, d2))
rch2, err := c2.Watch(ctx, d2)
assert.NoError(t, err)
d1.SubscribeBroadcastEvent("mention", handler)
d2.SubscribeBroadcastEvent("mention", handler)

err = d2.Broadcast("mention", "yorkie")
assert.NoError(t, err)
Expand All @@ -640,14 +643,22 @@ func TestDocument(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
rcv := 0
for {
select {
case resp := <-rch1:
if resp.Err != nil {
assert.Equal(t, resp.Type, client.DocumentBroadcast)
assert.ErrorIs(t, resp.Err, ErrBroadcastEventHandlingError)
return
rcv++
}
case <-rch2:
case <-time.After(1 * time.Second):
// Assuming that every subscriber can receive the broadcast
// event within this timeout period, check if every subscriber
// successfully receives the event.
assert.Equal(t, 1, rcv)
return
case <-ctx.Done():
return
}
Expand Down

1 comment on commit 1985575

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: 1985575 Previous: 8b2a9a7 Ratio
BenchmarkDocument/constructor_test - ns/op 1762 ns/op 1645 ns/op 1.07
BenchmarkDocument/constructor_test - B/op 1240 B/op 1144 B/op 1.08
BenchmarkDocument/constructor_test - allocs/op 20 allocs/op 18 allocs/op 1.11
BenchmarkDocument/status_test - ns/op 1120 ns/op 1036 ns/op 1.08
BenchmarkDocument/status_test - B/op 1208 B/op 1112 B/op 1.09
BenchmarkDocument/status_test - allocs/op 18 allocs/op 16 allocs/op 1.13
BenchmarkDocument/equals_test - ns/op 9339 ns/op 9056 ns/op 1.03
BenchmarkDocument/equals_test - B/op 7008 B/op 6720 B/op 1.04
BenchmarkDocument/equals_test - allocs/op 119 allocs/op 113 allocs/op 1.05
BenchmarkDocument/nested_update_test - ns/op 22014 ns/op 23725 ns/op 0.93
BenchmarkDocument/nested_update_test - B/op 11993 B/op 11897 B/op 1.01
BenchmarkDocument/nested_update_test - allocs/op 253 allocs/op 251 allocs/op 1.01
BenchmarkDocument/delete_test - ns/op 29833 ns/op 29089 ns/op 1.03
BenchmarkDocument/delete_test - B/op 15218 B/op 15122 B/op 1.01
BenchmarkDocument/delete_test - allocs/op 332 allocs/op 330 allocs/op 1.01
BenchmarkDocument/object_test - ns/op 10563 ns/op 10382 ns/op 1.02
BenchmarkDocument/object_test - B/op 6752 B/op 6657 B/op 1.01
BenchmarkDocument/object_test - allocs/op 115 allocs/op 113 allocs/op 1.02
BenchmarkDocument/array_test - ns/op 36269 ns/op 34925 ns/op 1.04
BenchmarkDocument/array_test - B/op 11850 B/op 11754 B/op 1.01
BenchmarkDocument/array_test - allocs/op 269 allocs/op 267 allocs/op 1.01
BenchmarkDocument/text_test - ns/op 38636 ns/op 38657 ns/op 1.00
BenchmarkDocument/text_test - B/op 14922 B/op 14826 B/op 1.01
BenchmarkDocument/text_test - allocs/op 475 allocs/op 473 allocs/op 1.00
BenchmarkDocument/text_composition_test - ns/op 39545 ns/op 38208 ns/op 1.03
BenchmarkDocument/text_composition_test - B/op 18306 B/op 18210 B/op 1.01
BenchmarkDocument/text_composition_test - allocs/op 476 allocs/op 474 allocs/op 1.00
BenchmarkDocument/rich_text_test - ns/op 105048 ns/op 102400 ns/op 1.03
BenchmarkDocument/rich_text_test - B/op 38703 B/op 37016 B/op 1.05
BenchmarkDocument/rich_text_test - allocs/op 1154 allocs/op 1134 allocs/op 1.02
BenchmarkDocument/counter_test - ns/op 21509 ns/op 21823 ns/op 0.99
BenchmarkDocument/counter_test - B/op 10273 B/op 10178 B/op 1.01
BenchmarkDocument/counter_test - allocs/op 240 allocs/op 238 allocs/op 1.01
BenchmarkDocument/text_edit_gc_100 - ns/op 4117622 ns/op 3919058 ns/op 1.05
BenchmarkDocument/text_edit_gc_100 - B/op 1553138 B/op 1553500 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17167 allocs/op 17166 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 327704809 ns/op 305822693 ns/op 1.07
BenchmarkDocument/text_edit_gc_1000 - B/op 136612140 B/op 136666284 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 210618 allocs/op 210879 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 5207563 ns/op 4461948 ns/op 1.17
BenchmarkDocument/text_split_gc_100 - B/op 2217895 B/op 2218207 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16592 allocs/op 16595 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 382688216 ns/op 357452373 ns/op 1.07
BenchmarkDocument/text_split_gc_1000 - B/op 214885773 B/op 214840861 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 211574 allocs/op 211342 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 19490084 ns/op 17519738 ns/op 1.11
BenchmarkDocument/text_delete_all_10000 - B/op 5903327 B/op 5905240 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 41122 allocs/op 41131 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 267949490 ns/op 239749099 ns/op 1.12
BenchmarkDocument/text_delete_all_100000 - B/op 53840204 B/op 53858656 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 415994 allocs/op 416083 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 323287 ns/op 313643 ns/op 1.03
BenchmarkDocument/text_100 - B/op 118515 B/op 118421 B/op 1.00
BenchmarkDocument/text_100 - allocs/op 5079 allocs/op 5077 allocs/op 1.00
BenchmarkDocument/text_1000 - ns/op 3554211 ns/op 3457317 ns/op 1.03
BenchmarkDocument/text_1000 - B/op 1153123 B/op 1153025 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50083 allocs/op 50081 allocs/op 1.00
BenchmarkDocument/array_1000 - ns/op 1734037 ns/op 1713545 ns/op 1.01
BenchmarkDocument/array_1000 - B/op 1102996 B/op 1103084 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11872 allocs/op 11871 allocs/op 1.00
BenchmarkDocument/array_10000 - ns/op 19801181 ns/op 19152387 ns/op 1.03
BenchmarkDocument/array_10000 - B/op 9907456 B/op 9906093 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120725 allocs/op 120718 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 179431 ns/op 176825 ns/op 1.01
BenchmarkDocument/array_gc_100 - B/op 98477 B/op 98364 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1248 allocs/op 1246 allocs/op 1.00
BenchmarkDocument/array_gc_1000 - ns/op 1979131 ns/op 1937102 ns/op 1.02
BenchmarkDocument/array_gc_1000 - B/op 1170671 B/op 1170600 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12911 allocs/op 12909 allocs/op 1.00
BenchmarkDocument/counter_1000 - ns/op 290214 ns/op 289151 ns/op 1.00
BenchmarkDocument/counter_1000 - B/op 198836 B/op 198740 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 6508 allocs/op 6506 allocs/op 1.00
BenchmarkDocument/counter_10000 - ns/op 3097479 ns/op 3069618 ns/op 1.01
BenchmarkDocument/counter_10000 - B/op 2165759 B/op 2165685 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 69515 allocs/op 69513 allocs/op 1.00
BenchmarkDocument/object_1000 - ns/op 1840482 ns/op 1774782 ns/op 1.04
BenchmarkDocument/object_1000 - B/op 1451519 B/op 1451444 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9920 allocs/op 9918 allocs/op 1.00
BenchmarkDocument/object_10000 - ns/op 22809046 ns/op 22137040 ns/op 1.03
BenchmarkDocument/object_10000 - B/op 12368638 B/op 12370116 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 101222 allocs/op 101224 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1009527 ns/op
BenchmarkDocument/tree_100 - B/op 442918 B/op
BenchmarkDocument/tree_100 - allocs/op 4505 allocs/op
BenchmarkDocument/tree_1000 - ns/op 65851854 ns/op
BenchmarkDocument/tree_1000 - B/op 35223099 B/op
BenchmarkDocument/tree_1000 - allocs/op 44117 allocs/op
BenchmarkDocument/tree_10000 - ns/op 8620941121 ns/op
BenchmarkDocument/tree_10000 - B/op 3438891312 B/op
BenchmarkDocument/tree_10000 - allocs/op 440161 allocs/op
BenchmarkDocument/tree_delete_all_1000 - ns/op 66850068 ns/op
BenchmarkDocument/tree_delete_all_1000 - B/op 35692278 B/op
BenchmarkDocument/tree_delete_all_1000 - allocs/op 51769 allocs/op
BenchmarkDocument/tree_edit_gc_100 - ns/op 3494644 ns/op
BenchmarkDocument/tree_edit_gc_100 - B/op 2077431 B/op
BenchmarkDocument/tree_edit_gc_100 - allocs/op 11164 allocs/op
BenchmarkDocument/tree_edit_gc_1000 - ns/op 255645800 ns/op
BenchmarkDocument/tree_edit_gc_1000 - B/op 180291378 B/op
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 113378 allocs/op
BenchmarkDocument/tree_split_gc_100 - ns/op 2480528 ns/op
BenchmarkDocument/tree_split_gc_100 - B/op 1347902 B/op
BenchmarkDocument/tree_split_gc_100 - allocs/op 9060 allocs/op
BenchmarkDocument/tree_split_gc_1000 - ns/op 158124433 ns/op
BenchmarkDocument/tree_split_gc_1000 - B/op 113955574 B/op
BenchmarkDocument/tree_split_gc_1000 - allocs/op 93867 allocs/op
BenchmarkRPC/client_to_server - ns/op 396818916 ns/op 403586987 ns/op 0.98
BenchmarkRPC/client_to_server - B/op 12423464 B/op 12224802 B/op 1.02
BenchmarkRPC/client_to_server - allocs/op 177088 allocs/op 176918 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 675177322 ns/op 703134840 ns/op 0.96
BenchmarkRPC/client_to_client_via_server - B/op 22800380 B/op 22618076 B/op 1.01
BenchmarkRPC/client_to_client_via_server - allocs/op 331469 allocs/op 331134 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1492475484 ns/op 1359748878 ns/op 1.10
BenchmarkRPC/attach_large_document - B/op 1809880096 B/op 1799405880 B/op 1.01
BenchmarkRPC/attach_large_document - allocs/op 9638 allocs/op 9590 allocs/op 1.01
BenchmarkRPC/adminCli_to_server - ns/op 587818610 ns/op 598842152 ns/op 0.98
BenchmarkRPC/adminCli_to_server - B/op 20000268 B/op 21103788 B/op 0.95
BenchmarkRPC/adminCli_to_server - allocs/op 324120 allocs/op 333606 allocs/op 0.97
BenchmarkLocker - ns/op 127.7 ns/op 122.4 ns/op 1.04
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 140.3 ns/op 128.7 ns/op 1.09
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 382.3 ns/op 483.6 ns/op 0.79
BenchmarkLockerMoreKeys - B/op 14 B/op 13 B/op 1.08
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkSync/memory_sync_10_test - ns/op 7234 ns/op 7322 ns/op 0.99
BenchmarkSync/memory_sync_10_test - B/op 1283 B/op 1283 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 66913 ns/op 69765 ns/op 0.96
BenchmarkSync/memory_sync_100_test - B/op 8769 B/op 8737 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 281 allocs/op 279 allocs/op 1.01
BenchmarkSync/memory_sync_1000_test - ns/op 668115 ns/op 686582 ns/op 0.97
BenchmarkSync/memory_sync_1000_test - B/op 81940 B/op 81819 B/op 1.00
BenchmarkSync/memory_sync_1000_test - allocs/op 2596 allocs/op 2582 allocs/op 1.01
BenchmarkSync/memory_sync_10000_test - ns/op 7215260 ns/op 7139141 ns/op 1.01
BenchmarkSync/memory_sync_10000_test - B/op 854876 B/op 858288 B/op 1.00
BenchmarkSync/memory_sync_10000_test - allocs/op 26650 allocs/op 26871 allocs/op 0.99
BenchmarkTextEditing - ns/op 28039077934 ns/op 27047402085 ns/op 1.04
BenchmarkTextEditing - B/op 8456893632 B/op 8456806024 B/op 1.00
BenchmarkTextEditing - allocs/op 20615754 allocs/op 20613838 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.