mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Make threadsafe, add cache
This commit is contained in:
		
							parent
							
								
									cdd2e7910a
								
							
						
					
					
						commit
						8b63e841ea
					
				
					 2 changed files with 40 additions and 11 deletions
				
			
		| 
						 | 
					@ -829,16 +829,13 @@ func (a *admin) admin_getMeta(keyString, coordString string) (metadataPayload, e
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	response := make(chan *metadataPayload, 1)
 | 
						response := make(chan *metadataPayload, 1)
 | 
				
			||||||
	sendMetaRequest := func() {
 | 
						sendMetaRequest := func() {
 | 
				
			||||||
		a.core.metadata.callbacks[key] = metadataCallback{
 | 
							a.core.metadata.addCallback(key, func(meta *metadataPayload) {
 | 
				
			||||||
			created: time.Now(),
 | 
					 | 
				
			||||||
			call: func(meta *metadataPayload) {
 | 
					 | 
				
			||||||
			defer func() { recover() }()
 | 
								defer func() { recover() }()
 | 
				
			||||||
			select {
 | 
								select {
 | 
				
			||||||
			case response <- meta:
 | 
								case response <- meta:
 | 
				
			||||||
			default:
 | 
								default:
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			},
 | 
							})
 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		a.core.metadata.sendMetadata(key, coords, false)
 | 
							a.core.metadata.sendMetadata(key, coords, false)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	a.core.router.doAdmin(sendMetaRequest)
 | 
						a.core.router.doAdmin(sendMetaRequest)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,7 +10,9 @@ type metadata struct {
 | 
				
			||||||
	myMetadata      metadataPayload
 | 
						myMetadata      metadataPayload
 | 
				
			||||||
	myMetadataMutex sync.RWMutex
 | 
						myMetadataMutex sync.RWMutex
 | 
				
			||||||
	callbacks       map[boxPubKey]metadataCallback
 | 
						callbacks       map[boxPubKey]metadataCallback
 | 
				
			||||||
 | 
						callbacksMutex  sync.Mutex
 | 
				
			||||||
	cache           map[boxPubKey]metadataPayload
 | 
						cache           map[boxPubKey]metadataPayload
 | 
				
			||||||
 | 
						cacheMutex      sync.RWMutex
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type metadataPayload []byte
 | 
					type metadataPayload []byte
 | 
				
			||||||
| 
						 | 
					@ -38,8 +40,20 @@ func (m *metadata) init(core *Core) {
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Add a callback
 | 
				
			||||||
 | 
					func (m *metadata) addCallback(sender boxPubKey, call func(meta *metadataPayload)) {
 | 
				
			||||||
 | 
						m.callbacksMutex.Lock()
 | 
				
			||||||
 | 
						defer m.callbacksMutex.Unlock()
 | 
				
			||||||
 | 
						m.callbacks[sender] = metadataCallback{
 | 
				
			||||||
 | 
							created: time.Now(),
 | 
				
			||||||
 | 
							call:    call,
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Handles the callback, if there is one
 | 
					// Handles the callback, if there is one
 | 
				
			||||||
func (m *metadata) callback(sender boxPubKey, meta metadataPayload) {
 | 
					func (m *metadata) callback(sender boxPubKey, meta metadataPayload) {
 | 
				
			||||||
 | 
						m.callbacksMutex.Lock()
 | 
				
			||||||
 | 
						defer m.callbacksMutex.Unlock()
 | 
				
			||||||
	if callback, ok := m.callbacks[sender]; ok {
 | 
						if callback, ok := m.callbacks[sender]; ok {
 | 
				
			||||||
		callback.call(&meta)
 | 
							callback.call(&meta)
 | 
				
			||||||
		delete(m.callbacks, sender)
 | 
							delete(m.callbacks, sender)
 | 
				
			||||||
| 
						 | 
					@ -60,10 +74,28 @@ func (m *metadata) setMetadata(meta metadataPayload) {
 | 
				
			||||||
	m.myMetadata = meta
 | 
						m.myMetadata = meta
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Add metadata into the cache for a node
 | 
				
			||||||
 | 
					func (m *metadata) addCachedMetadata(key boxPubKey, payload metadataPayload) {
 | 
				
			||||||
 | 
						m.cacheMutex.Lock()
 | 
				
			||||||
 | 
						defer m.cacheMutex.Unlock()
 | 
				
			||||||
 | 
						m.cache[key] = payload
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Get a metadata entry from the cache
 | 
				
			||||||
 | 
					func (m *metadata) getCachedMetadata(key boxPubKey) metadataPayload {
 | 
				
			||||||
 | 
						m.cacheMutex.RLock()
 | 
				
			||||||
 | 
						defer m.cacheMutex.RUnlock()
 | 
				
			||||||
 | 
						if meta, ok := m.cache[key]; ok {
 | 
				
			||||||
 | 
							return meta
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return metadataPayload{}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Handles a meta request/response.
 | 
					// Handles a meta request/response.
 | 
				
			||||||
func (m *metadata) handleMetadata(meta *sessionMeta) {
 | 
					func (m *metadata) handleMetadata(meta *sessionMeta) {
 | 
				
			||||||
	if meta.IsResponse {
 | 
						if meta.IsResponse {
 | 
				
			||||||
		m.core.metadata.callback(meta.SendPermPub, meta.Metadata)
 | 
							m.callback(meta.SendPermPub, meta.Metadata)
 | 
				
			||||||
 | 
							m.addCachedMetadata(meta.SendPermPub, meta.Metadata)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		m.sendMetadata(meta.SendPermPub, meta.SendCoords, true)
 | 
							m.sendMetadata(meta.SendPermPub, meta.SendCoords, true)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue