| 1 |
package httpsched |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"log" |
| 7 |
"net/http" |
| 8 |
"sync" |
| 9 |
|
| 10 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 11 |
"github.com/mesos/mesos-go/api/v1/lib/encoding" |
| 12 |
"github.com/mesos/mesos-go/api/v1/lib/httpcli" |
| 13 |
"github.com/mesos/mesos-go/api/v1/lib/httpcli/apierrors" |
| 14 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler" |
| 15 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler/calls" |
| 16 |
) |
| 17 |
|
| 18 |
const ( |
| 19 |
headerMesosStreamID = "Mesos-Stream-Id" |
| 20 |
debug = false |
| 21 |
) |
| 22 |
|
| 23 |
type StateError string |
| 24 |
|
| 25 |
func (err StateError) Error() string { return string(err) } |
| 26 |
|
| 27 |
var ( |
| 28 |
errMissingStreamID = httpcli.ProtocolError("missing Mesos-Stream-Id header expected with successful SUBSCRIBE") |
| 29 |
errAlreadySubscribed = StateError("already subscribed, cannot re-issue a SUBSCRIBE call") |
| 30 |
) |
| 31 |
|
| 32 |
type ( |
| 33 |
|
| 34 |
state struct { |
| 35 |
client *client |
| 36 |
|
| 37 |
m sync.Mutex |
| 38 |
fn stateFn |
| 39 |
caller calls.Caller |
| 40 |
|
| 41 |
call *scheduler.Call |
| 42 |
resp mesos.Response |
| 43 |
err error |
| 44 |
} |
| 45 |
|
| 46 |
stateFn func(context.Context, *state) stateFn |
| 47 |
) |
| 48 |
|
| 49 |
func maybeLogged(f httpcli.DoFunc) httpcli.DoFunc { |
| 50 |
if debug { |
| 51 |
return func(req *http.Request) (*http.Response, error) { |
| 52 |
log.Println("wrapping request", req.URL, req.Header) |
| 53 |
resp, err := f(req) |
| 54 |
if err == nil { |
| 55 |
log.Printf("status %d", resp.StatusCode) |
| 56 |
for k := range resp.Header { |
| 57 |
log.Println("header " + k + ": " + resp.Header.Get(k)) |
| 58 |
} |
| 59 |
} |
| 60 |
return resp, err |
| 61 |
} |
| 62 |
} |
| 63 |
return f |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
var DisconnectionDetector = func(disconnect func()) mesos.ResponseDecorator { |
| 79 |
return mesos.ResponseDecoratorFunc(func(resp mesos.Response) mesos.Response { |
| 80 |
return &mesos.ResponseWrapper{ |
| 81 |
Response: resp, |
| 82 |
Decoder: disconnectionDecoder(resp, disconnect), |
| 83 |
} |
| 84 |
}) |
| 85 |
} |
| 86 |
|
| 87 |
func disconnectionDecoder(decoder encoding.Decoder, disconnect func()) encoding.Decoder { |
| 88 |
return encoding.DecoderFunc(func(u encoding.Unmarshaler) (err error) { |
| 89 |
err = decoder.Decode(u) |
| 90 |
if err != nil { |
| 91 |
disconnect() |
| 92 |
return |
| 93 |
} |
| 94 |
switch e := u.(type) { |
| 95 |
case (*scheduler.Event): |
| 96 |
if e.GetType() == scheduler.Event_ERROR { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
disconnect() |
| 101 |
} |
| 102 |
default: |
| 103 |
|
| 104 |
err = httpcli.ProtocolError( |
| 105 |
fmt.Sprintf("unexpected object on subscription event stream: %v", e)) |
| 106 |
disconnect() |
| 107 |
} |
| 108 |
return |
| 109 |
}) |
| 110 |
} |
| 111 |
|
| 112 |
func disconnectedFn(ctx context.Context, state *state) stateFn { |
| 113 |
|
| 114 |
if state.call.GetType() != scheduler.Call_SUBSCRIBE { |
| 115 |
state.resp = nil |
| 116 |
state.err = apierrors.CodeUnsubscribed.Error("") |
| 117 |
return disconnectedFn |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
var ( |
| 122 |
mesosStreamID = "" |
| 123 |
undoable = httpcli.WrapDoer(func(f httpcli.DoFunc) httpcli.DoFunc { |
| 124 |
f = maybeLogged(f) |
| 125 |
return func(req *http.Request) (resp *http.Response, err error) { |
| 126 |
resp, err = f(req) |
| 127 |
if err == nil && resp.StatusCode == 200 { |
| 128 |
|
| 129 |
|
| 130 |
mesosStreamID = resp.Header.Get(headerMesosStreamID) |
| 131 |
if mesosStreamID == "" { |
| 132 |
resp.Body.Close() |
| 133 |
resp = nil |
| 134 |
err = errMissingStreamID |
| 135 |
} |
| 136 |
} |
| 137 |
return |
| 138 |
} |
| 139 |
}) |
| 140 |
subscribeCaller = &callerTemporary{ |
| 141 |
opt: undoable, |
| 142 |
callerInternal: state.client, |
| 143 |
requestOpts: []httpcli.RequestOpt{httpcli.Close(true)}, |
| 144 |
} |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
stateResp, stateErr := subscribeCaller.Call(ctx, state.call) |
| 149 |
state.err = stateErr |
| 150 |
|
| 151 |
|
| 152 |
if stateErr != nil { |
| 153 |
if stateResp != nil { |
| 154 |
stateResp.Close() |
| 155 |
} |
| 156 |
state.resp = nil |
| 157 |
return disconnectedFn |
| 158 |
} |
| 159 |
|
| 160 |
transitionToDisconnected := func() { |
| 161 |
state.m.Lock() |
| 162 |
defer state.m.Unlock() |
| 163 |
state.fn = disconnectedFn |
| 164 |
_ = stateResp.Close() |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
state.resp = DisconnectionDetector(transitionToDisconnected).Decorate(stateResp) |
| 170 |
|
| 171 |
|
| 172 |
state.caller = &callerTemporary{ |
| 173 |
opt: httpcli.DefaultHeader(headerMesosStreamID, mesosStreamID), |
| 174 |
callerInternal: state.client, |
| 175 |
} |
| 176 |
return connectedFn |
| 177 |
} |
| 178 |
|
| 179 |
func errorIndicatesSubscriptionLoss(err error) (result bool) { |
| 180 |
type lossy interface { |
| 181 |
SubscriptionLoss() bool |
| 182 |
} |
| 183 |
if lossyErr, ok := err.(lossy); ok { |
| 184 |
result = lossyErr.SubscriptionLoss() |
| 185 |
} |
| 186 |
return |
| 187 |
} |
| 188 |
|
| 189 |
func connectedFn(ctx context.Context, state *state) stateFn { |
| 190 |
|
| 191 |
if state.call.GetType() == scheduler.Call_SUBSCRIBE { |
| 192 |
if state.client.allowReconnect { |
| 193 |
|
| 194 |
|
| 195 |
state.caller = nil |
| 196 |
state.resp = nil |
| 197 |
state.err = nil |
| 198 |
state.fn = disconnectedFn |
| 199 |
|
| 200 |
return state.fn(ctx, state) |
| 201 |
} else { |
| 202 |
state.resp = nil |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
state.err = errAlreadySubscribed |
| 213 |
return connectedFn |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
state.resp, state.err = state.caller.Call(ctx, state.call) |
| 219 |
|
| 220 |
if errorIndicatesSubscriptionLoss(state.err) { |
| 221 |
|
| 222 |
return disconnectedFn |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
return connectedFn |
| 227 |
} |
| 228 |
|
| 229 |
func (state *state) Call(ctx context.Context, call *scheduler.Call) (resp mesos.Response, err error) { |
| 230 |
state.m.Lock() |
| 231 |
defer state.m.Unlock() |
| 232 |
state.call = call |
| 233 |
state.fn = state.fn(ctx, state) |
| 234 |
|
| 235 |
if debug && state.err != nil { |
| 236 |
log.Print(*call, state.err) |
| 237 |
} |
| 238 |
|
| 239 |
return state.resp, state.err |
| 240 |
} |