فهرست منبع

Use server sent events to maintain a client side view of the keyspace schemas #554 (#715)

* Use server sent events to maintain a client side view of the keyspace schemas #554

* remove debug log

* check if schemaDescriber is nil

* add Viktor Tonkol to AUTHORS
tonkolviktor 9 سال پیش
والد
کامیت
9c06bf7e0f
3فایلهای تغییر یافته به همراه20 افزوده شده و 2 حذف شده
  1. 1 0
      AUTHORS
  2. 11 0
      events.go
  3. 8 2
      metadata.go

+ 1 - 0
AUTHORS

@@ -67,3 +67,4 @@ nikandfor <nikandfor@gmail.com>
 Anthony Woods <awoods@raintank.io>
 Alexander Inozemtsev <alexander.inozemtsev@gmail.com>
 Rob McColl <rob@robmccoll.com>; <rmccoll@ionicsecurity.com>
+Viktor Tönköl <viktor.toenkoel@motionlogic.de>

+ 11 - 0
events.go

@@ -107,6 +107,17 @@ func (s *Session) handleEvent(framer *framer) {
 }
 
 func (s *Session) handleSchemaEvent(frames []frame) {
+	if s.schemaDescriber == nil {
+		return
+	}
+	for _, frame := range frames {
+		switch f := frame.(type) {
+		case *schemaChangeKeyspace:
+			s.schemaDescriber.clearSchema(f.keyspace)
+		case *schemaChangeTable:
+			s.schemaDescriber.clearSchema(f.keyspace)
+		}
+	}
 }
 
 func (s *Session) handleNodeEvent(frames []frame) {

+ 8 - 2
metadata.go

@@ -105,8 +105,6 @@ func (s *schemaDescriber) getSchema(keyspaceName string) (*KeyspaceMetadata, err
 	s.mu.Lock()
 	defer s.mu.Unlock()
 
-	// TODO handle schema change events
-
 	metadata, found := s.cache[keyspaceName]
 	if !found {
 		// refresh the cache for this keyspace
@@ -121,6 +119,14 @@ func (s *schemaDescriber) getSchema(keyspaceName string) (*KeyspaceMetadata, err
 	return metadata, nil
 }
 
+// clears the already cached keyspace metadata
+func (s *schemaDescriber) clearSchema(keyspaceName string) {
+	s.mu.Lock()
+	defer s.mu.Unlock()
+
+	delete(s.cache, keyspaceName)
+}
+
 // forcibly updates the current KeyspaceMetadata held by the schema describer
 // for a given named keyspace.
 func (s *schemaDescriber) refreshSchema(keyspaceName string) error {