bug fixes for prod version of webshare -- allow notify to reset backoff time

This commit is contained in:
sawka 2023-04-05 15:09:51 -07:00
parent 000d86b7af
commit c7289556e5
2 changed files with 49 additions and 17 deletions

View File

@ -52,8 +52,6 @@ const MaxCommandLen = 4096
const MaxSignalLen = 12 const MaxSignalLen = 12
const MaxSignalNum = 64 const MaxSignalNum = 64
const MaxEvalDepth = 5 const MaxEvalDepth = 5
const DevWebScreenUrlFmt = "http://devtest.getprompt.com:9001/static/index-dev.html?screenid=%s&viewkey=%s"
const ProdWebScreenUrlFmt = "https://share.getprompt.dev/s/%s?viewkey=%s"
var ColorNames = []string{"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} var ColorNames = []string{"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"}
var RemoteColorNames = []string{"red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"} var RemoteColorNames = []string{"red", "green", "yellow", "blue", "magenta", "cyan", "white", "orange"}
@ -225,13 +223,6 @@ func GetCmdStr(pk *scpacket.FeCommandPacketType) string {
return pk.MetaCmd + ":" + pk.MetaSubCmd return pk.MetaCmd + ":" + pk.MetaSubCmd
} }
func GetWebShareUrl(screenId string, viewKey string) string {
if scbase.IsDevMode() {
return fmt.Sprintf(DevWebScreenUrlFmt, screenId, viewKey)
}
return fmt.Sprintf(ProdWebScreenUrlFmt, screenId, viewKey)
}
func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { func HandleCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
metaCmd := SubMetaCmd(pk.MetaCmd) metaCmd := SubMetaCmd(pk.MetaCmd)
var cmdName string var cmdName string
@ -2801,6 +2792,7 @@ func ClientCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstor
} }
func ClientNotifyUpdateWriterCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) { func ClientNotifyUpdateWriterCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (sstore.UpdatePacket, error) {
pcloud.ResetUpdateWriterNumFailures()
sstore.NotifyUpdateWriter() sstore.NotifyUpdateWriter()
update := sstore.ModelUpdate{ update := sstore.ModelUpdate{
Info: &sstore.InfoMsgType{ Info: &sstore.InfoMsgType{

View File

@ -41,6 +41,7 @@ const WebShareUpdateUrl = "/auth/web-share-update"
var updateWriterLock = &sync.Mutex{} var updateWriterLock = &sync.Mutex{}
var updateWriterRunning = false var updateWriterRunning = false
var updateWriterNumFailures = 0
type AuthInfo struct { type AuthInfo struct {
UserId string `json:"userid"` UserId string `json:"userid"`
@ -106,7 +107,7 @@ func makeAnonPostReq(ctx context.Context, apiUrl string, data interface{}) (*htt
func doRequest(req *http.Request, outputObj interface{}) (*http.Response, error) { func doRequest(req *http.Request, outputObj interface{}) (*http.Response, error) {
apiUrl := req.Header.Get("X-PromptAPIUrl") apiUrl := req.Header.Get("X-PromptAPIUrl")
log.Printf("[pcloud] sending request %v\n", req.URL) log.Printf("[pcloud] sending request %s %v\n", req.Method, req.URL)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("error contacting pcloud %q service: %v", apiUrl, err) return nil, fmt.Errorf("error contacting pcloud %q service: %v", apiUrl, err)
@ -473,10 +474,15 @@ func StartUpdateWriter() {
go runWebShareUpdateWriter() go runWebShareUpdateWriter()
} }
func computeBackoff(numFailures int) time.Duration { func computeUpdateWriterBackoff() time.Duration {
updateWriterLock.Lock()
numFailures := updateWriterNumFailures
updateWriterLock.Unlock()
switch numFailures { switch numFailures {
case 0:
return 0
case 1: case 1:
return 500 * time.Millisecond return 1 * time.Second
case 2: case 2:
return 2 * time.Second return 2 * time.Second
case 3: case 3:
@ -492,6 +498,24 @@ func computeBackoff(numFailures int) time.Duration {
} }
} }
func incrementUpdateWriterNumFailures() {
updateWriterLock.Lock()
defer updateWriterLock.Unlock()
updateWriterNumFailures++
}
func ResetUpdateWriterNumFailures() {
updateWriterLock.Lock()
defer updateWriterLock.Unlock()
updateWriterNumFailures = 0
}
func GetUpdateWriterNumFailures() int {
updateWriterLock.Lock()
defer updateWriterLock.Unlock()
return updateWriterNumFailures
}
type updateKey struct { type updateKey struct {
ScreenId string ScreenId string
LineId string LineId string
@ -526,7 +550,6 @@ func runWebShareUpdateWriter() {
}() }()
log.Printf("[pcloud] starting update writer\n") log.Printf("[pcloud] starting update writer\n")
numErrors := 0 numErrors := 0
numSendErrors := 0
for { for {
if numErrors > MaxUpdateWriterErrors { if numErrors > MaxUpdateWriterErrors {
log.Printf("[pcloud] update-writer, too many errors, exiting\n") log.Printf("[pcloud] update-writer, too many errors, exiting\n")
@ -568,10 +591,10 @@ func runWebShareUpdateWriter() {
} }
err = DoWebUpdates(webUpdateArr) err = DoWebUpdates(webUpdateArr)
if err != nil { if err != nil {
numSendErrors++ incrementUpdateWriterNumFailures()
backoffTime := computeBackoff(numSendErrors) backoffTime := computeUpdateWriterBackoff()
log.Printf("[pcloud] error processing %d web-updates (backoff=%v): %v\n", len(webUpdateArr), backoffTime, err) log.Printf("[pcloud] error processing %d web-updates (backoff=%v): %v\n", len(webUpdateArr), backoffTime, err)
time.Sleep(backoffTime) updateBackoffSleep(backoffTime)
continue continue
} }
log.Printf("[pcloud] sent %d web-updates\n", len(webUpdateArr)) log.Printf("[pcloud] sent %d web-updates\n", len(webUpdateArr))
@ -580,6 +603,23 @@ func runWebShareUpdateWriter() {
debugStrs = append(debugStrs, webUpdate.String()) debugStrs = append(debugStrs, webUpdate.String())
} }
log.Printf("[pcloud] updates: %s\n", strings.Join(debugStrs, " ")) log.Printf("[pcloud] updates: %s\n", strings.Join(debugStrs, " "))
numSendErrors = 0 ResetUpdateWriterNumFailures()
}
}
// todo fix this, set deadline, check with condition variable, backoff then just needs to notify
func updateBackoffSleep(backoffTime time.Duration) {
var totalSleep time.Duration
for {
sleepTime := time.Second
totalSleep += sleepTime
time.Sleep(sleepTime)
if totalSleep >= backoffTime {
break
}
numFailures := GetUpdateWriterNumFailures()
if numFailures == 0 {
break
}
} }
} }