-
Notifications
You must be signed in to change notification settings - Fork 60
/
request_store.go
102 lines (90 loc) · 2.1 KB
/
request_store.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
package gosmpp
import (
"context"
"fmt"
"github.com/linxGnu/gosmpp/pdu"
cmap "github.com/orcaman/concurrent-map/v2"
"golang.org/x/exp/maps"
"strconv"
"time"
)
// Request represent a request tracked by the RequestStore
type Request struct {
pdu.PDU
TimeSent time.Time
}
// Response represents a response from a Request in the RequestStore
type Response struct {
pdu.PDU
OriginalRequest Request
}
// RequestStore interface used for WindowedRequestTracking
type RequestStore interface {
Set(ctx context.Context, request Request) error
Get(ctx context.Context, sequenceNumber int32) (Request, bool)
List(ctx context.Context) []Request
Delete(ctx context.Context, sequenceNumber int32) error
Clear(ctx context.Context) error
Length(ctx context.Context) (int, error)
}
type DefaultStore struct {
store cmap.ConcurrentMap[string, Request]
}
func NewDefaultStore() DefaultStore {
return DefaultStore{
store: cmap.New[Request](),
}
}
func (s DefaultStore) Set(ctx context.Context, request Request) error {
select {
case <-ctx.Done():
fmt.Println("Task cancelled")
return ctx.Err()
default:
s.store.Set(strconv.Itoa(int(request.PDU.GetSequenceNumber())), request)
return nil
}
}
func (s DefaultStore) Get(ctx context.Context, sequenceNumber int32) (Request, bool) {
select {
case <-ctx.Done():
fmt.Println("Task cancelled")
return Request{}, false
default:
return s.store.Get(strconv.Itoa(int(sequenceNumber)))
}
}
func (s DefaultStore) List(ctx context.Context) []Request {
select {
case <-ctx.Done():
return []Request{}
default:
return maps.Values(s.store.Items())
}
}
func (s DefaultStore) Delete(ctx context.Context, sequenceNumber int32) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
s.store.Remove(strconv.Itoa(int(sequenceNumber)))
return nil
}
}
func (s DefaultStore) Clear(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
s.store.Clear()
return nil
}
}
func (s DefaultStore) Length(ctx context.Context) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
return s.store.Count(), nil
}
}