Skip to content

Commit

Permalink
GH-165: Tidy up version handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Jun 15, 2020
1 parent 9a0c158 commit 0f20864
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion server/apiHandler.go
Expand Up @@ -173,7 +173,7 @@ func (s *Service) handleCall(w http.ResponseWriter, r *http.Request, rid string,
}

func (s *Service) temporaryConn(w http.ResponseWriter, r *http.Request, cb func(*wsConn, func([]byte, error))) {
c := s.newWSConn(nil, r, latestProtocol)
c := s.newWSConn(nil, r, versionLatest)
if c == nil {
httpError(w, reserr.ErrServiceUnavailable, s.enc)
return
Expand Down
8 changes: 4 additions & 4 deletions server/subscription.go
Expand Up @@ -266,7 +266,7 @@ func (s *Subscription) onLoaded(rcb *readyCallback) {
// It will lock the subscription and queue any events until ReleaseRPCResources is called.
func (s *Subscription) GetRPCResources() *rpc.Resources {
r := &rpc.Resources{}
if s.c.ProtocolVersion() <= versionSoftResourceReferenceAndDataValue {
if s.c.ProtocolVersion() < versionSoftResourceReferenceAndDataValue {
s.populateResourcesLegacy(r)
} else {
s.populateResources(r)
Expand Down Expand Up @@ -608,7 +608,7 @@ func (s *Subscription) processCollectionEvent(event *rescache.ResourceEvent) {
case codec.ValueTypeData:
fallthrough
case codec.ValueTypeSoftReference:
if s.c.ProtocolVersion() <= versionSoftResourceReferenceAndDataValue {
if s.c.ProtocolVersion() < versionSoftResourceReferenceAndDataValue {
s.c.Send(rpc.NewEvent(s.rid, event.Event, rpc.AddEvent{Idx: idx, Value: rescache.Legacy120Value(v)}))
break
}
Expand Down Expand Up @@ -669,7 +669,7 @@ func (s *Subscription) processModelEvent(event *rescache.ResourceEvent) {
// Quick exit if there are no new unsent subscriptions
if subs == nil {
// Legacy behavior
if s.c.ProtocolVersion() <= versionSoftResourceReferenceAndDataValue {
if s.c.ProtocolVersion() < versionSoftResourceReferenceAndDataValue {
s.c.Send(rpc.NewEvent(s.rid, event.Event, rpc.ChangeEvent{Values: rescache.Legacy120ValueMap(event.Changed)}))
} else {
s.c.Send(rpc.NewEvent(s.rid, event.Event, rpc.ChangeEvent{Values: event.Changed}))
Expand All @@ -695,7 +695,7 @@ func (s *Subscription) processModelEvent(event *rescache.ResourceEvent) {
r := &rpc.Resources{}

// Legacy behavior
if s.c.ProtocolVersion() <= versionSoftResourceReferenceAndDataValue {
if s.c.ProtocolVersion() < versionSoftResourceReferenceAndDataValue {
for _, sub := range subs {
sub.populateResourcesLegacy(r)
}
Expand Down
11 changes: 8 additions & 3 deletions server/version.go
@@ -1,7 +1,12 @@
package server

// Last protocol version where a specific feature was not supported.
// Protocol versions
const (
versionCallResourceResponse = 1001001
versionSoftResourceReferenceAndDataValue = 1002000
versionLatest = 1002001 // MAJOR * 1000000 + MINOR * 1000 + PATCH
versionLegacy = 1001001
)

const (
versionCallResourceResponse = 1002000
versionSoftResourceReferenceAndDataValue = versionLatest
)
8 changes: 1 addition & 7 deletions server/wsConn.go
Expand Up @@ -36,12 +36,6 @@ type wsConn struct {
mu sync.Mutex
}

// Protocol versions
const (
legacyProtocol = 1001001 // MAJOR * 1000000 + MINOR * 1000 + PATCH
latestProtocol = 1999999
)

var (
errInvalidNewResourceResponse = reserr.InternalError(errors.New("non-resource response on new request"))
)
Expand Down Expand Up @@ -412,7 +406,7 @@ func (c *wsConn) handleCallAuthResponse(result json.RawMessage, refRID string, e
}

// Legacy behavior
if c.protocolVer <= versionCallResourceResponse {
if c.protocolVer < versionCallResourceResponse {
// Handle resource response by just returning the resource ID without subscription
if refRID != "" {
cb(rpc.CallResourceResult{RID: refRID}, nil)
Expand Down
2 changes: 1 addition & 1 deletion server/wsHandler.go
Expand Up @@ -48,7 +48,7 @@ func (s *Service) wsHandler(w http.ResponseWriter, r *http.Request) {
return
}

conn := s.newWSConn(ws, r, legacyProtocol)
conn := s.newWSConn(ws, r, versionLegacy)
if conn == nil {
return
}
Expand Down

0 comments on commit 0f20864

Please sign in to comment.