From c7289556e5ff589fa5d52cf748b5538a5e66e30a Mon Sep 17 00:00:00 2001 From: sawka Date: Wed, 5 Apr 2023 15:09:51 -0700 Subject: [PATCH] bug fixes for prod version of webshare -- allow notify to reset backoff time --- pkg/cmdrunner/cmdrunner.go | 10 +------ pkg/pcloud/pcloud.go | 56 ++++++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index 180f2dec7..a7cc9b7cd 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -52,8 +52,6 @@ const MaxCommandLen = 4096 const MaxSignalLen = 12 const MaxSignalNum = 64 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 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 } -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) { metaCmd := SubMetaCmd(pk.MetaCmd) 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) { + pcloud.ResetUpdateWriterNumFailures() sstore.NotifyUpdateWriter() update := sstore.ModelUpdate{ Info: &sstore.InfoMsgType{ diff --git a/pkg/pcloud/pcloud.go b/pkg/pcloud/pcloud.go index 5ca5bbc1d..40a5fe08e 100644 --- a/pkg/pcloud/pcloud.go +++ b/pkg/pcloud/pcloud.go @@ -41,6 +41,7 @@ const WebShareUpdateUrl = "/auth/web-share-update" var updateWriterLock = &sync.Mutex{} var updateWriterRunning = false +var updateWriterNumFailures = 0 type AuthInfo struct { 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) { 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) if err != nil { return nil, fmt.Errorf("error contacting pcloud %q service: %v", apiUrl, err) @@ -473,10 +474,15 @@ func StartUpdateWriter() { go runWebShareUpdateWriter() } -func computeBackoff(numFailures int) time.Duration { +func computeUpdateWriterBackoff() time.Duration { + updateWriterLock.Lock() + numFailures := updateWriterNumFailures + updateWriterLock.Unlock() switch numFailures { + case 0: + return 0 case 1: - return 500 * time.Millisecond + return 1 * time.Second case 2: return 2 * time.Second 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 { ScreenId string LineId string @@ -526,7 +550,6 @@ func runWebShareUpdateWriter() { }() log.Printf("[pcloud] starting update writer\n") numErrors := 0 - numSendErrors := 0 for { if numErrors > MaxUpdateWriterErrors { log.Printf("[pcloud] update-writer, too many errors, exiting\n") @@ -568,10 +591,10 @@ func runWebShareUpdateWriter() { } err = DoWebUpdates(webUpdateArr) if err != nil { - numSendErrors++ - backoffTime := computeBackoff(numSendErrors) + incrementUpdateWriterNumFailures() + backoffTime := computeUpdateWriterBackoff() log.Printf("[pcloud] error processing %d web-updates (backoff=%v): %v\n", len(webUpdateArr), backoffTime, err) - time.Sleep(backoffTime) + updateBackoffSleep(backoffTime) continue } log.Printf("[pcloud] sent %d web-updates\n", len(webUpdateArr)) @@ -580,6 +603,23 @@ func runWebShareUpdateWriter() { debugStrs = append(debugStrs, webUpdate.String()) } 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 + } } }