-
Notifications
You must be signed in to change notification settings - Fork 15
/
offers.go
103 lines (98 loc) · 2.55 KB
/
offers.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
/*
* Copyright 2017 LinkedIn Corporation. All rights reserved. Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
package main
import (
"context"
"fmt"
"reflect"
"github.com/satori/go.uuid"
)
// RebindOffer describes a offer to rebind
type RebindOffer struct {
ID uuid.UUID `json:"id"`
URL string `json:"url"`
}
// MakeOffer is responsible for setting up then "offering" multiple rebinds for a given request
func (m *RebindManager) MakeOffer(ctx context.Context, req WebSocketHostRequest) []RebindOffer {
// TODO: Choose slightly more intelligently
methods := []RebindMethod{
NewTTLRebind(ctx, m, req.Host, 1),
NewTTLRebind(ctx, m, req.Host, 2),
NewTTLRebind(ctx, m, req.Host, 4),
NewTTLRebind(ctx, m, req.Host, 8),
NewTTLRebind(ctx, m, req.Host, 16),
NewThresholdRebind(ctx, m, req.Host, 1, 2),
NewThresholdRebind(ctx, m, req.Host, 2, 2),
NewThresholdRebind(ctx, m, req.Host, 3, 4),
NewThresholdRebind(ctx, m, req.Host, 4, 4),
/*
&ThresholdRebind{
Target: req.Host,
TTL: 2,
Threshold: 1,
}, &ThresholdRebind{
Target: req.Host,
TTL: 2,
Threshold: 2,
}, &ThresholdRebind{
Target: req.Host,
TTL: 4,
Threshold: 1,
}, &ThresholdRebind{
Target: req.Host,
TTL: 4,
Threshold: 2,
}, &TTLRebind{
Target: req.Host,
TTL: 1,
}, &TTLRebind{
Target: req.Host,
TTL: 2,
}, &TTLRebind{
Target: req.Host,
TTL: 4,
}, &TTLRebind{
Target: req.Host,
TTL: 8,
}, &TTLRebind{
Target: req.Host,
TTL: 16,
}, &TTLRebind{
Target: req.Host,
TTL: 32,
},*/
/***** DISABLED: Not refusing requests correctly *******/
/*&MultiRecordRebind{
Target: req.Host,
TTL: 300,
},*/
}
/***** DISABLED: Not working correctly at the moment *******/
// If there was a cached req
/*for _, cache := range req.Cached {
log.Debug(cache, req.Host)
if cache.Port() == req.Host.Port() {
methods = append(methods, &AppCacheRebind{
Target: req.Host,
TTL: 1,
})
break
}
}*/
// Loop each method and configure
var offers []RebindOffer
for _, method := range methods {
id := uuid.NewV4()
offers = append(offers, RebindOffer{
ID: id,
URL: fmt.Sprintf("http://%s.%s:%s/.well-known/rebind/v1.frame", id, m.base, req.Host.Port),
})
m.RebindsLock.Lock()
m.Rebinds[id] = method
m.RebindsLock.Unlock()
log.Infof(`Created rebind offer "%s" of type "%s" for request "%s"`, id, reflect.TypeOf(method), requestID(ctx))
}
return offers
}