/
subscription.go
135 lines (117 loc) · 2.95 KB
/
subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ws
import (
"github.com/google/uuid"
csmap "github.com/mhmtszr/concurrent-swiss-map"
"github.com/orsinium-labs/enum"
)
type WsEvent enum.Member[string]
var (
wsEventSubscribed = WsEvent{"subscribed"}
wsEventUnsubscribed = WsEvent{"unsubscribed"}
wsEventCandles = WsEvent{"candle"}
wsEventTicker = WsEvent{"ticker"}
wsEventTicker24h = WsEvent{"ticker24h"}
wsEventTrades = WsEvent{"trade"}
wsEventBook = WsEvent{"book"}
wsEventAuth = WsEvent{"authenticate"}
wsEventOrder = WsEvent{"order"}
wsEventFill = WsEvent{"fill"}
wsEvents = enum.New(
wsEventSubscribed,
wsEventUnsubscribed,
wsEventCandles,
wsEventTicker,
wsEventTicker24h,
wsEventTrades,
wsEventBook,
wsEventAuth,
wsEventOrder,
wsEventFill,
)
)
type Action enum.Member[string]
var (
actionSubscribe = Action{"subscribe"}
actionUnsubscribe = Action{"unsubscribe"}
actionAuthenticate = Action{"authenticate"}
)
type ChannelName enum.Member[string]
var (
channelNameCandles = ChannelName{"candles"}
channelNameTicker = ChannelName{"ticker"}
channelNameTicker24h = ChannelName{"ticker24h"}
channelNameTrades = ChannelName{"trades"}
channelNameBook = ChannelName{"book"}
channelNameAccount = ChannelName{"account"}
)
type subscription[T any] struct {
id uuid.UUID
market string
outchn chan T
inchn chan<- T
}
func newSubscription[T any](id uuid.UUID, market string, inchn chan<- T, outchn chan T) *subscription[T] {
return &subscription[T]{
id: id,
market: market,
inchn: inchn,
outchn: outchn,
}
}
func getSubscriptionKeys[K comparable, V any](data *csmap.CsMap[K, V]) []K {
keys := make([]K, 0)
data.Range(func(key K, value V) (stop bool) {
keys = append(keys, key)
return false
})
return keys
}
func relayMessages[T any](in <-chan T, out chan<- T) {
for msg := range in {
out <- msg
}
}
func requireSubscription[T any](subs *csmap.CsMap[string, T], markets []string) error {
for _, market := range markets {
if !subs.Has(market) {
return errNoSubscriptionActive(market)
}
}
return nil
}
func requireNoSubscription[T any](subs *csmap.CsMap[string, T], markets []string) error {
for _, market := range markets {
if subs.Has(market) {
return errSubscriptionAlreadyActive(market)
}
}
return nil
}
func deleteSubscriptions[T any](
subs *csmap.CsMap[string, *subscription[T]],
keys []string,
) error {
counts := make(map[uuid.UUID]int)
subs.Range(func(key string, value *subscription[T]) (stop bool) {
counts[value.id]++
return false
})
idsWithKeys := make(map[uuid.UUID][]string)
for _, key := range keys {
if sub, found := subs.Load(key); found {
idsWithKeys[sub.id] = append(idsWithKeys[sub.id], key)
close(sub.inchn)
}
}
for id, keys := range idsWithKeys {
if counts[id] == len(keys) {
if item, found := subs.Load(keys[0]); found {
close(item.outchn)
}
}
for _, key := range keys {
subs.Delete(key)
}
}
return nil
}