Skip to content

Commit

Permalink
bump to Go 1.23.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Aug 23, 2024
1 parent 09b6d84 commit 97dc9ae
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 116 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
image: debian:bookworm-slim
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '>=1.22.0'
- run: |
apt-get update -qq
apt-get install -y --no-install-recommends ca-certificates gcc libc6-dev
- uses: actions/setup-go@v5
with:
go-version: '>=1.23.0'
- run: |
go generate ./migrations
go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:1.22-alpine AS build
FROM golang:1.23-alpine AS build
RUN apk add --no-cache gcc musl-dev openssl
COPY go.mod /src/
COPY go.sum /src/
Expand Down
2 changes: 1 addition & 1 deletion ap/audience.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (a Audience) MarshalJSON() ([]byte, error) {
return []byte("[]"), nil
}

return json.Marshal(a.Keys())
return json.Marshal(a.CollectKeys())
}

func (a *Audience) Scan(src any) error {
Expand Down
80 changes: 66 additions & 14 deletions data/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package data

import "iter"

type valueAndIndex[TV any] struct {
value TV
index int
Expand All @@ -37,25 +39,75 @@ func (m OrderedMap[TK, TV]) Store(key TK, value TV) {
}
}

// Keys returns a list of keys in the map.
// To do so, it iterates over keys and allocates memory.
func (m OrderedMap[TK, TV]) Keys() []TK {
l := make([]TK, len(m))
// Keys iterates over keys in the map.
// It allocates memory.
func (m OrderedMap[TK, TV]) Keys() iter.Seq[TK] {
have := len(m)
l := make([]*TK, have)

for k, v := range m {
l[v.index] = k
return func(yield func(TK) bool) {
next := 0

for k, v := range m {
if v.index == next {
if !yield(k) {
return
}
next++
continue
}

l[v.index] = &k

if l[next] != nil {
if !yield(*l[next]) {
return
}

next++
}
}

for next < have {
if !yield(*l[next]) {
break
}
next++
}
}
}

return l
// Values iterates over values in the map.
// Values calls [OrderedMap.Keys], therefore it allocates memory.
func (m OrderedMap[TK, TV]) Values() iter.Seq[TV] {
return func(yield func(TV) bool) {
for k := range m.Keys() {
if !yield(m[k].value) {
break
}
}
}
}

// Range iterates over the map and calls a callback for each key/value pair.
// Iteration stops if the callback returns false.
// Range calls [OrderedMap.Keys], therefore it allocates memory.
func (m OrderedMap[TK, TV]) Range(f func(key TK, value TV) bool) {
for _, k := range m.Keys() {
if !f(k, m[k].value) {
break
// All iterates over key/value pairs in the map.
// All calls [OrderedMap.Keys], therefore it allocates memory.
func (m OrderedMap[TK, TV]) All() iter.Seq2[TK, TV] {
return func(yield func(TK, TV) bool) {
for k := range m.Keys() {
if !yield(k, m[k].value) {
break
}
}
}
}

// CollectKeys returns a new slice of keys in the map.
func (m OrderedMap[TK, TV]) CollectKeys() []TK {
l := make([]TK, len(m))

for k, v := range m {
l[v.index] = k
}

return l
}
29 changes: 12 additions & 17 deletions fed/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,13 @@ func (q *Queue) queueTasks(ctx context.Context, job deliveryJob, rawActivity []b

// deduplicate recipients or skip if we're forwarding an activity
if job.Activity.Actor == job.Sender.ID {
job.Activity.To.Range(func(id string, _ struct{}) bool {
for id := range job.Activity.To.Keys() {
recipients.Add(id)
return true
})
}

job.Activity.CC.Range(func(id string, _ struct{}) bool {
for id := range job.Activity.CC.Keys() {
recipients.Add(id)
return true
})
}
}

actorIDs := ap.Audience{}
Expand All @@ -280,20 +278,19 @@ func (q *Queue) queueTasks(ctx context.Context, job deliveryJob, rawActivity []b
}

// assume that all other federated recipients are actors and not collections
recipients.Range(func(recipient string, _ struct{}) bool {
for recipient := range recipients.Keys() {
actorIDs.Add(recipient)
return true
})
}

var author string
if obj, ok := job.Activity.Object.(*ap.Object); ok {
author = obj.AttributedTo
}

actorIDs.Range(func(actorID string, _ struct{}) bool {
for actorID := range actorIDs.Keys() {
if actorID == author || actorID == ap.Public {
q.Log.Debug("Skipping recipient", "to", actorID, "activity", job.Activity.ID)
return true
continue
}

to, err := q.Resolver.ResolveID(ctx, q.Log, q.DB, key, actorID, ap.Offline)
Expand All @@ -302,7 +299,7 @@ func (q *Queue) queueTasks(ctx context.Context, job deliveryJob, rawActivity []b
if !errors.Is(err, ErrActorGone) && !errors.Is(err, ErrBlockedDomain) {
events <- deliveryEvent{job, false}
}
return true
continue
}

// if possible, use the recipients's shared inbox and skip other recipients with the same shared inbox
Expand All @@ -318,12 +315,12 @@ func (q *Queue) queueTasks(ctx context.Context, job deliveryJob, rawActivity []b
if err != nil {
q.Log.Warn("Failed to create new request", "to", actorID, "activity", job.Activity.ID, "inbox", inbox, "error", err)
events <- deliveryEvent{job, false}
return true
continue
}

if req.URL.Host == q.Domain {
q.Log.Debug("Skipping local recipient inbox", "to", actorID, "activity", job.Activity.ID, "inbox", inbox)
return true
continue
}

req.Header.Set("User-Agent", userAgent)
Expand All @@ -345,9 +342,7 @@ func (q *Queue) queueTasks(ctx context.Context, job deliveryJob, rawActivity []b
Request: req,
Inbox: inbox,
}

return true
})
}

return nil
}
24 changes: 10 additions & 14 deletions fed/followers.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ func (d *followersDigest) Sync(ctx context.Context, domain string, cfg *cfg.Conf
return err
}

local.Range(func(follower string, _ struct{}) bool {
for follower := range local.Keys() {
if remote.OrderedItems.Contains(follower) {
return true
continue
}

log.Info("Found unknown local follow", "followed", d.Followed, "follower", follower)
Expand All @@ -269,30 +269,28 @@ func (d *followersDigest) Sync(ctx context.Context, domain string, cfg *cfg.Conf
); err != nil {
log.Warn("Failed to remove local follow", "followed", d.Followed, "follower", follower, "error", err)
}

return true
})
}

prefix := fmt.Sprintf("https://%s/", domain)

remote.OrderedItems.Range(func(follower string, _ struct{}) bool {
for follower := range remote.OrderedItems.Keys() {
if local.Contains(follower) {
return true
continue
}

if !strings.HasPrefix(follower, prefix) {
return true
continue
}

log.Info("Found unknown remote follow", "followed", d.Followed, "follower", follower)

var exists int
if err := db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM persons WHERE id = ?)`, follower).Scan(&exists); err != nil {
log.Warn("Failed to check if follower exists", "followed", d.Followed, "follower", follower, "error", err)
return true
continue
} else if exists == 0 {
log.Info("Follower does not exist", "followed", d.Followed, "follower", follower)
return true
continue
}

var followID string
Expand All @@ -301,15 +299,13 @@ func (d *followersDigest) Sync(ctx context.Context, domain string, cfg *cfg.Conf
log.Warn("Using fake follow ID to remove unknown remote follow", "followed", d.Followed, "follower", follower, "id", followID)
} else if err != nil {
log.Warn("Failed to fetch follow ID of unknown remote follow", "followed", d.Followed, "follower", follower, "error", err)
return true
continue
}

if err := outbox.Unfollow(ctx, domain, log, db, follower, d.Followed, followID); err != nil {
log.Warn("Failed to remove remote follow", "followed", d.Followed, "follower", follower, "error", err)
}

return true
})
}

return nil
}
Expand Down
17 changes: 6 additions & 11 deletions front/finger/finger.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (fl *Listener) handle(ctx context.Context, conn net.Conn, wg *sync.WaitGrou
conn.Write([]byte{'\r', '\n'})
}

links.Range(func(link, alt string) bool {
for link, alt := range links.All() {
if !strings.Contains(summary, link) {
if alt == "" {
conn.Write([]byte(link))
Expand All @@ -140,17 +140,15 @@ func (fl *Listener) handle(ctx context.Context, conn net.Conn, wg *sync.WaitGrou
}
conn.Write([]byte{'\r', '\n'})
}

return true
})
}

if summary != "" || len(links) > 0 {
conn.Write([]byte{'\r', '\n'})
}

i := 0
last := len(posts) - 1
posts.Range(func(content string, inserted int64) bool {
for content, inserted := range posts.All() {
text, links := plain.FromHTML(content)

conn.Write([]byte(time.Unix(inserted, 0).Format(time.DateOnly)))
Expand All @@ -160,7 +158,7 @@ func (fl *Listener) handle(ctx context.Context, conn net.Conn, wg *sync.WaitGrou
conn.Write([]byte{'\r', '\n'})
}

links.Range(func(link, alt string) bool {
for link, alt := range links.All() {
if !strings.Contains(text, link) {
if alt == "" {
conn.Write([]byte(link))
Expand All @@ -169,17 +167,14 @@ func (fl *Listener) handle(ctx context.Context, conn net.Conn, wg *sync.WaitGrou
}
conn.Write([]byte{'\r', '\n'})
}

return true
})
}

if i < last {
conn.Write([]byte{'\r', '\n'})
}

i++
return true
})
}

if len(posts) == 0 && summary == "" && len(links) == 0 {
conn.Write([]byte("No Plan.\r\n"))
Expand Down
11 changes: 5 additions & 6 deletions front/outbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,13 @@ func (h *Handler) userOutbox(w text.Writer, r *request, args ...string) {
for _, line := range summary {
w.Quote(line)
}
links.Range(func(link, alt string) bool {
for link, alt := range links.All() {
if alt == "" {
w.Link(link, link)
} else {
w.Linkf(link, "%s [%s]", link, alt)
}
return true
})
}
}

if offset == 0 {
Expand Down Expand Up @@ -278,10 +277,10 @@ func (h *Handler) userOutbox(w text.Writer, r *request, args ...string) {
if len(links) == 0 {
w.Textf("%s: %s", prop.Name, raw)
} else {
links.Range(func(link string, _ string) bool {
for link := range links.Keys() {
w.Linkf(link, prop.Name)
return false
})
break
}
}

firstProperty = false
Expand Down
14 changes: 6 additions & 8 deletions front/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,19 @@ func (h *Handler) post(w text.Writer, r *request, oldNote *ap.Object, inReplyTo
}

anyRecipient := false
note.To.Range(func(actorID string, _ struct{}) bool {
for actorID := range note.To.Keys() {
if actorID != r.User.ID {
anyRecipient = true
return false
break
}
return true
})
}
if !anyRecipient {
note.CC.Range(func(actorID string, _ struct{}) bool {
for actorID := range note.CC.Keys() {
if actorID != r.User.ID {
anyRecipient = true
return false
break
}
return true
})
}
}
if !anyRecipient {
w.Status(40, "Post audience is empty")
Expand Down
Loading

0 comments on commit 97dc9ae

Please sign in to comment.