-
Notifications
You must be signed in to change notification settings - Fork 7
/
connect_options.go
99 lines (80 loc) · 2.04 KB
/
connect_options.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
package cable
import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/grafana/sobek"
)
type connectOptions struct {
Cookies string `json:"cookies"`
Headers map[string]string `json:"headers"`
Tags map[string]string `json:"tags"`
Codec string `json:"codec"`
HandshakeTimeoutS int `json:"handshakeTimeoutS"`
ReceiveTimeoutMs int `json:"receiveTimeoutMs"`
LogLevel string `json:"logLevel"`
}
const (
defaultHandshakeTimeout = 60
defaultReceiveTimeout = 1000
)
func parseOptions(rt *sobek.Runtime, inOpts sobek.Value) (*connectOptions, error) {
var outOpts connectOptions
if inOpts == nil || sobek.IsUndefined(inOpts) || sobek.IsNull(inOpts) {
return &outOpts, nil
}
data, err := json.Marshal(inOpts.ToObject(rt).Export())
if err != nil {
return nil, err
}
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
if err := dec.Decode(&outOpts); err != nil {
if uerr := json.Unmarshal(data, &outOpts); uerr != nil {
return nil, uerr
}
return nil, err
}
return &outOpts, nil
}
func (co *connectOptions) codec() *Codec {
if co.Codec == "msgpack" {
return MsgPackCodec
} else if co.Codec == "protobuf" {
return ProtobufCodec
}
return JSONCodec
}
func (co *connectOptions) handshakeTimeout() time.Duration {
if co.HandshakeTimeoutS == 0 {
return defaultHandshakeTimeout * time.Second
}
return time.Duration(co.HandshakeTimeoutS) * time.Second
}
func (co *connectOptions) receiveTimeout() time.Duration {
if co.ReceiveTimeoutMs == 0 {
return defaultReceiveTimeout * time.Millisecond
}
return time.Duration(co.ReceiveTimeoutMs) * time.Millisecond
}
func (co *connectOptions) appendTags(tags map[string]string) map[string]string {
if len(co.Tags) > 0 {
for k, v := range co.Tags {
tags[k] = v
}
}
return tags
}
func (co *connectOptions) header() http.Header {
header := http.Header{}
if len(co.Headers) > 0 {
for k, v := range co.Headers {
header.Set(k, v)
}
}
if co.Cookies != "" {
header.Set("COOKIE", co.Cookies)
}
return header
}