Skip to content

Commit

Permalink
Always send full state payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Feb 24, 2024
1 parent c3cf4dd commit b13fb3c
Showing 1 changed file with 8 additions and 33 deletions.
41 changes: 8 additions & 33 deletions src/handlers/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {sleep} from '../utils/common'
import {CLOUD_AGENT_CONNECTION_ID} from '../utils/env'
import {reportError} from '../utils/errors'
import {client} from '../utils/grpc'
import {logger} from '../utils/logger'

export async function startStateStream(signal: AbortSignal) {
while (!signal.aborted) {
Expand Down Expand Up @@ -44,37 +43,6 @@ interface StateCache {
let stateCache: StateCache | null = null

export async function reportCurrentState(currentState: CurrentState) {
if (stateCache) {
const diff = compare(stateCache.state, currentState)

// If there is no difference, don't send a request
if (diff.length === 0) return

const request: PlainMessage<ReportCurrentStateRequest> = {
connectionId: CLOUD_AGENT_CONNECTION_ID,
state: {
case: 'patch',
value: {
generation: stateCache.generation,
patch: {
case: 'aws',
value: {
patch: JSON.stringify(diff),
},
},
},
},
}

try {
const res = await client.reportCurrentState(request)
stateCache = {state: currentState, generation: res.generation}
return
} catch {
// Ignore an error here and fall down to below
}
}

const request: PlainMessage<ReportCurrentStateRequest> = {
connectionId: CLOUD_AGENT_CONNECTION_ID,
state: {
Expand All @@ -90,8 +58,15 @@ export async function reportCurrentState(currentState: CurrentState) {
},
},
}

if (stateCache) {
const diff = compare(stateCache.state, currentState)

// If there is no difference, don't send a request
if (diff.length === 0) return
}

const res = await client.reportCurrentState(request)
logger.info('Saving state in cache')
stateCache = {state: currentState, generation: res.generation}
}

Expand Down

0 comments on commit b13fb3c

Please sign in to comment.