-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hub to PublishJob, expose Notify
Adds a Hub field to PublishJob Exposes Notify to use in other workers
- Loading branch information
1 parent
de6dc77
commit e840207
Showing
7 changed files
with
120 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package model | ||
|
||
type Hub struct { | ||
Hasher string `json:"hasher"` | ||
URL string `json:"url"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package websub | ||
|
||
import ( | ||
"bytes" | ||
"crypto/hmac" | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"github.com/jpillora/backoff" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
func Notify(client *http.Client, job PublishJob) (bool, error) { | ||
req, err := http.NewRequest(http.MethodPost, job.Subscription.Callback, bytes.NewReader(job.Data)) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if job.Subscription.Secret != "" { | ||
mac := hmac.New(newHash(job.Hub.Hasher), []byte(job.Subscription.Secret)) | ||
mac.Write(job.Data) | ||
req.Header.Set("X-Hub-Signature", job.Hub.Hasher+"="+hex.EncodeToString(mac.Sum(nil))) | ||
} | ||
|
||
req.Header.Set("Content-Type", job.ContentType) | ||
req.Header.Set("Link", fmt.Sprintf("<%s>; rel=\"hub\", <%s>; rel=\"self\"", job.Hub.URL, job.Subscription.Topic)) | ||
|
||
b := &backoff.Backoff{ | ||
Min: 100 * time.Millisecond, | ||
Max: 10 * time.Minute, | ||
Factor: 2, | ||
Jitter: false, | ||
} | ||
|
||
var attempts int | ||
|
||
for { | ||
res, err := client.Do(req) | ||
|
||
if err == nil { | ||
res.Body.Close() | ||
|
||
if res.StatusCode >= 200 && res.StatusCode <= 299 { | ||
return true, nil | ||
} else if res.StatusCode == http.StatusGone { | ||
return false, nil | ||
} | ||
} | ||
|
||
attempts++ | ||
|
||
if attempts >= 3 { | ||
break | ||
} | ||
|
||
<-time.After(b.Duration()) | ||
} | ||
|
||
return false, errors.New("failed to publish after 3 attempts") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters